JavaScript Minifier that doesn't break code (in Perl)

I searched for a JavaScript minification program that doesn't break working code but none appeared. I've written one in Perl (I hope!). If the input JavaScript code is missing semicolons or has code like a++ +b that is ok. Internet Explorer conditional comments are retained in the output. My code was inspired by Douglas Crockford's JSMin but is a complete rewrite to add these features.

I've uploaded the Perl module to CPAN JavaScript::Minifier

If you want to download the module right now from my subversion repository...

svn co http://dev.michaux.ca/svn/random/JavaScript-Minifier/
cd JavaScript-Minifier
perl Makefile.PL
make test

I'm also working on a CSS minification program in Perl that is not yet well tested: svn co http://dev.michaux.ca/svn/random/CSS-Minifier/

Why Perl you ask? Because that is the server-side language used where I work.

Update August 3, 2007 - I've uploaded the CSS::Minifier module to CPAN and it should appear there soon.

Comments

Have something to write? Comment on this article.

Vesa May 27, 2007

Thanks for sharing the code. An example where this is useful would be nice!

Peter Michaux May 27, 2007

Vesa,

Minification is useful during the build/release process of a website. It removes unnecessary whitespace and comments from JavaScript files so they are not served to the client. This enables verbose commenting and helpful whitespace for development but without burdening the production server and reduces client download times.

Gábor August 12, 2007

Very nice job. Recently I've just started to develop a same tool in ELisp that removes whitespaces and normalizes function and variable names both. With your script I reduced a 21K file to 17K, my Lisp code squeezeed it just to 15K-but it still has some bugs so I'll start using yours (:

I'm afraid in the documentation of JavaScript::Minifier there's a small mistake:

open(OUTFILE, 'myScript-min.js') or die;

is correctly written:

open(OUTFILE, '>myScript-min.js') or die;
Peter Michaux August 12, 2007

Hi Gábor, Thanks for the docs bug report. The missing '>' will appear in the next releases of both minifiers.

Graham September 19, 2007

FYI, am working on a Modperl2 output filter that makes use of JS::Minifier and CSS::Minifier to help make it easier to use them in production environments.

You'll find my initial RFC on the modules in the Mod_perl mailing list archives.

I've got an initial version here that works, and have a few people looking at it before I upload it to CPAN.

Graham September 21, 2007

FYI, released Apache2::Filter::Minifier::JavaScript and Apache2::Filter::Minifier::CSS onto CPAN yesterday...

Graham October 4, 2007

Peter, have tried to reach you via e-mail with some updates/enhancements to CSS::Minifier and JavaScript::Minifier... you around? :)

Jacky Brown November 12, 2007

Hi! Please see my new ajax webtools http://iframe.in/ This site will help you quickly encode and obfuscate your HTML/JavaScript code many different methods, as well as generate browser redirect or hidden iframe on JavaScript or Flash...

Peter Michaux November 12, 2007

It looks like your Minifier is based on Dean Edward's packer. Dean's program in based on regular expression analysis which, unfortunately, can be tricked quite easily with edge cases.

The objective of my Minifier is to provide something that can be used automatically in the build phase of deployment. A web interface is less convenient for this purpose.

Sphinx December 12, 2007

Works like a champ!

But did you miss a '>' in your Synopsis:

use JavaScript::Minifier qw(minify);
open(INFILE, 'myScript.js') or die;
open(OUTFILE, '>myScript-min.js') or die;

Without the '>', the outfile isn't written.

Have something to write? Comment on this article.