Ben Pickles. Saying stuff about stuff.

Minify command-line tool

While creating the jQuery plugin "onScreen" I had a sudden burst of inspiration and hacked together a quick command-line script to minify JavaScript files. It's a really simple way to generate a .min.js version of a file and is particularly helpful when distributing JavaScript like the aforementioned jQuery plugin.

Here's the Gist.

At its most simple you can pass a single argument which is the path to the JavaScript file to minify, a .min.js version will be created for you in the same directory. So the following will create jquery.myplugin.min.js:

$ minify jquery.myplugin.js

You can also pass more paths and it will concat and minify them into a single file - the last argument is always the output file:

$ minify input.js output.min.js
$ minify a.js few.js files.js output.min.js

Internally it uses Google's Closure Compiler via the closure-compiler Gem (which requires Java and, obviously, Ruby) so all the hard work has already been done for me.

I've been writing a few command-line tools like this one, it feels great to scratch your own itch. I also recently wrote ding which - yes you may chuckle - I find surprisingly useful!

Posted on