A couple of years ago, I blogged about a way to minify HTML with sed. Now, I streamlined this in a script.
How it works
The script will copy the original directory (or single file) and leave it untouched, while removing any useless space and comments from any .htm, .html, .php, .css and .js file in the copy.
Running the script
chmod 755 deploy.sh
./deploy.sh <source> <target>
If the target already exists, it will be removed first (you will need to confirm).
Much is transparent or commented. The minification is the only really interesting part. So let’s go at it!
I used the trick I wrote about yesterday.
Basically, the minification is all done here:
echo "Minify file $1"
perl -pi -e "s#(?:^[ \t]+|[ \t]+$)##g" "$1" # leading & trailing spaces
substitute_escape_strings "//.*" "" "$1" # remove //-style comments
perl -pi -e 's#(?:\r?\n)+# #g' "$1" # new lines
substitute_escape_strings "<!-- .*?-->|/\*.*?\*/" "" "$1" # remove multiline comments
substitute_escape_strings "[\s]+" " " "$1" # make multiple spaces single
perl -pi -e "s#(?:'[^']+'|\"[^\"]+\")(*SKIP)(?!)|$1#$2#g" "$3"
This code is commented and should not require more than is already said.
Now, the essential enhancement I made since the sed era is the removal of all comments and correction of my code to remove all new lines.
I abandoned sed and went to Perl because sed does not support the non-greedy operator, which greatly simplified my work when removing delimited patterns (typically multiline comments).
Getting the full script
It is available on Github. Don’t hesitate to fork and make it better.
Note: the competition
There is none: this is not a tool I would recommend in production environment (for lack of test, for instance).
Why this script is good
It is a quick fix for simple and mostly static websites. It removes potentially sensitive information (many website have their .git repository deployed, containing everything in it, and comments can be a source of leak too, be it only be advertising the technology behind the scenes).
Why this script is bad
I needed something working quickly and without effort. This is it.
You can use it on your server to automatically deploy the latest version of your website. Imagine:
# Ensure the deploy script is up-to-date
# Update the site
./website-deploy-script/deploy.sh my-wonderful-site /var/sites/www/my-wonderful-site
echo "Deployment complete, site is up-to-date!"