I often use the dojo javascript compressor system which is a very nice way to reduce the site of larger js libs. It uses the Rhino javascript interpreter to reduce the size of all local variables and removes all unessecary spaces and comments — but most importantly it keeps the external API of a javascript lib like it is.
A minor problem with that was that I like to use Internet Explorer specific conditional compilation to handle Internet Explorer’s non-standard js garbage. (I would really like to be able to fully ignore IE, but ignoring 70% of all Internet users is not really going to please my clients).
Since the compressor removes all comments from the js files it also removes conditional code sections. There are plans to make the compressor keep them but they’re postponed until at least dojo 0.4.
So for now I wrote myself a little bash script that replaces the conditional comments with javascript expressions that are not removed, compresses the js file and reinserts the conditional comments.
I have tested this only on linux but you should also be able to use it under OS-X or under cygwin for Windows.
- dojo compressor bash script ( needs java, expects custom_rhino.jar to be in the same directory )
- custom_rhino.jar (see dojo site for how to get the source)
Using this script
/* Javascript compression Test */alert("Js compression test");
function foo(arg)
{
var local=arg;
}
/*@cc_on @*/
/*@if (@_jscript_version >= 5)
alert("You are using Internet Explorer 5+");
function bar(arg)
{
var localIE=arg;
}
@end @*/
is compressed to
alert("Js compression test");
function foo(_1){
var _2=_1;
}
/*@cc_on @*/
/*@if (@_jscript_version >= 5)
alert("You are using Internet Explorer 5+");
function bar(_3){
var _4=_3;
}
@end @*/
That does not seem that much for this small example, but for larger libs the reduction is not that bad. The current version of my ff js lib e.g. compresses from 12005 bytes to 7644 bytes (36% reduction).
Update: corrected custom_rhino.jar URL