#!/bin/bash
rm -Rf build
mkdir build
# Use an array to preserve order, pretty darn important
FILES=("javascripts/InputHandler.js" "javascripts/Engine.js" "javascripts/LaserShot.js" "javascripts/Sprite.js" "javascripts/Player.js" "javascripts/Enemy.js" "javascripts/Collision.js" "javascripts/SquareInvaders.js" "javascripts/init.js")
SI_FILE=javascripts/square-invaders.txt
touch $SI_FILE

for f in ${FILES[@]}
do
  echo "Processing $f..."
  cat $f >> $SI_FILE
done

echo "Done!"
echo "Minifying JavaScript files..."
mv $SI_FILE "./javascripts/square-invaders.js"
minify "./javascripts/square-invaders.js" -o build/square-invaders.min.js
rm "./javascripts/square-invaders.js"

echo "\"Minify\" HTML as well"
HTML_FILE=build/index.html
tr -d "\011\012\015" < index.html | cat > $HTML_FILE
echo "Removing old script references"
perl -i.bak -0777ne 's|<script.*?</script>||gms;print' $HTML_FILE
sed -i ".bak" s/'<\/body>'/"<script type='application\/javascript' src='square-invaders.min.js'><\/script><\/body>"/ $HTML_FILE
rm build/index.html.bak
echo "Build complete :)"

#!/bin/bash
rm -Rf build
mkdir build
# Use an array to preserve order, pretty darn important
FILES=("javascripts/InputHandler.js" "javascripts/Engine.js" "javascripts/LaserShot.js" "javascripts/Sprite.js" "javascripts/Player.js" "javascripts/Enemy.js" "javascripts/Collision.js" "javascripts/SquareInvaders.js" "javascripts/init.js")
SI_FILE=javascripts/square-invaders.js
touch $SI_FILE

for f in ${FILES[@]}
do
  echo "Processing $f..."
  cat $f >> $SI_FILE
done

echo "Done!"
echo "Minifying JavaScript files..."
minify $SI_FILE -o build/square-invaders.min.js
rm $SI_FILE

"Creating HTML"
HTML_FILE=build/index.html
touch $HTML_FILE
cat index.html > $HTML_FILE
echo "Removing old script references"
perl -i.bak -0777ne 's|<script.*?</script>||gms;print' $HTML_FILE
sed -i ".bak" s/'<\/body>'/"<script type='application\/javascript' src='square-invaders.min.js'><\/script><\/body>"/ $HTML_FILE
echo "\"Minify\" HTML as well"
cat $HTML_FILE | tr -d "\n" |  tr -s " "  > $HTML_FILE
rm build/index.html.bak
echo "Build complete :)"

Improve minification - JS & HTML
Improve minification - JS & HTML

change variable SI_FILE to have .js extension instead of .txt as we now explicitly list the javascript files to be minified instead of using a directory full of .js files. Also improve HTML minification. Before the process was the remove all whitespace from index.html then add script tag. Now we replace script tags first then minify. The HTML now has no new line characters and everything is separated by one single space character, instead of multiple
