Duke in boxes... (CC-Attribution 4.0 Yannick Kirschhoffer)

Simple script to minify web resources

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

Easy one:

If the target already exists, it will be removed first (you will need to confirm).

The code

The basics

Much is transparent or commented. The minification is the only really interesting part. So let’s go at it!

The minification

I used the trick I wrote about yesterday.

Basically, the minification is all done here:

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

And yet, many tools were specifically designed for this purpose. See Grunt or Gulp plugins for JavaScript projects, or wro4j for Java projects. I imagine tools exist also for ASP or PHP projects.

I needed something working quickly and without effort. This is it.

Combine it

You can use it on your server to automatically deploy the latest version of your website. Imagine:

Published by

Cyrille Chopelet

Programming addict, UX philosopher, casual gamer, sci-fi enthusiast, hi-tech dilettante, ... Some people even call me a geek.

Leave a Reply