A couple of days ago somebody on the Apache Cocoon user list send a message to the mailing-list about on the fly minification of for instance Javascript files. This topic has been quite popular over the past years, since web application have become richer and Javascript files have become larger.
The ideal situation would be to compres your static files (CSS or Javascript) at build time, so this will not cost you any processing power, when your application is already running. I myself quite often use the Maven 2 YUI compressor plugin while building my projects, but in case you can’t use this plugin you could think about a different solution. Since I’ve been using Cocoon for over more then 5 years, I thought I gave it another try and write a nice Cocoon reader that does this minification for you.
There are multiple minification and obfuscation frameworks out there. One has a greater compression ratio then the other, but for me the most well know ones are probably:
- Dojo Shrinksafe - Rhino based compressor from the Dojo Toolkit
- YUI Compressor - Rhino based compressor by Yahoo
- JSMin - a whitespace compressor by Douglas Crockford
Since Apache Cocoon comes with a version of Rhino and both #1 and #2 have their own version of Rhino included, this could end up having nasty conflicts because of two different versions of the library on the same classpath. Therefore I chose to write a reader based on JSMin, which does a lot of whitespace compression for you.
The implementation of this reader was quite simple and if you’re interested, you can get the source here. Do keep in mind that you will have to have the JSMin.java file also on the classpath, otherwise it wil not work.