Recommending yui-compressor over jsmin
27 June 2010The following is a response that I sent someone quite a while ago in response to questions about JSMin.java. Hopefully it will be useful to others also.
I don’t actually use JSMin any more, and haven’t been maintaining it. Douglas Crockford has made some changes to the original have I haven’t gotten around to including. It looks like there may also some other changes in the javascript version that you linked to (I only had a quick glance at it).
Have a look at http://developer.yahoo.com/yui/compressor/ - that is the compressor that I use now. There is quite a bit of info on that page to get you up and running. I’m not sure if it supports the conditional compilation comments that you want, but I’d suspect that it does.
You can use that compressor inside your own code.
e.g.
public String postprocess( String data, final String absolutePath ) {
try {
JavaScriptCompressor compressor = new JavaScriptCompressor(new StringReader(data),
new ErrorReporter() {
public void warning(String message, String sourceName,
int line, String lineSource, int lineOffset) {
if (line < 0) {
System.err.println("\n[WARNING] ” + message);
} else {
System.err.println( “\n[WARNING] ” + line + ‘:’ + lineOffset + ‘:’ +
message + “\nfile:”+absolutePath+”\nline causing error: ” +
lineSource );
}
}
public void error(String message, String sourceName,
int line, String lineSource, int lineOffset) {
if (line < 0) {
System.err.println(”\n[ERROR] ” + message);
} else {
System.err.println( “\n[WARNING] ” + line + ‘:’ + lineOffset + ‘:’ +
message + “\nfile:”+absolutePath+”\nline causing error: ” +
lineSource );
}
}
public EvaluatorException runtimeError(String message, String sourceName,
int line, String lineSource, int lineOffset) {
error(message, sourceName, line, lineSource, lineOffset);
return new EvaluatorException(message);
}
});
StringWriter writer = new StringWriter( );
compressor.compress( writer, -1,true, false, true, false );
return writer.toString();
} catch( Exception e ) {
LOG.warn( e.getMessage(), e );
return data;
}
}
No comments yet