Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
SASS + Coffee + Concatenation in prod
#!/bin/bash
set -exo pipefail
BUILD_ENV=$1
if [ `uname` == 'Darwin' ]; then
OSX=1
JSCOMPRESSOR="yuicompressor --type js"
else
OSX=
JSCOMPRESSOR="yui-compressor --type js"
fi
rm -Rf public
mkdir -p public/styles
function bower_components() {
cat app/index.html | \
grep bower_components | \
cut -d '"' -f 2 | \
sed -e 's/^/app/'
}
function file_hash() {
if [ "$OSX" == "1" ]; then
md5 -q $1
else
md5sum $1 | awk '{print $1}'
fi
}
function run_sed() {
if [ "$OSX" == "1" ]; then
sed -e "$1" -i '' "$2"
else
sed -e "$1" -i "$2"
fi
}
coffee -o public/scripts -c app/scripts
node-sass --include-path app/bower_components/foundation/scss \
app/styles/main.scss public/styles/main.css
cp app/index.html public
cp -r app/views public/views
cp -r app/fonts public/fonts
if [ "$BUILD_ENV" == 'production' ]; then
find public/scripts -type f | xargs cat | ngmin | $JSCOMPRESSOR > public/all.js
ALL_FILE="all-$(file_hash public/all.js).js"
mv public/all.js public/$ALL_FILE
mkdir -p public/vendor
for f in $(bower_components); do
minified=$(echo $f | sed -e 's/.js/.min.js/')
if [ -f $minified ]; then
cp $minified public/vendor
else
cat $f | $JSCOMPRESSOR > public/vendor/$(basename $f)
fi
done
find public/vendor -type f | xargs cat > public/vendor.js
VENDOR_FILE="vendor-$(file_hash public/vendor.js).js"
mv public/vendor.js public/$VENDOR_FILE
run_sed "/<script src=\"\/scripts/d" public/index.html
run_sed "/<script src=\"\/bower_components/d" public/index.html
run_sed "s/<\/body>/<script src=\"\/$VENDOR_FILE\"><\/script><script src=\"\/$ALL_FILE\"><\/script><\/body>/" \
public/index.html
rm -Rf public/scripts
rm -Rf public/vendor
else
mkdir -p public/bower_components
for f in $(bower_components); do
cp $f public/bower_components
done
fi
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment