I'm interested in hearing how you prefer to automate Javascript minification for your Java web apps. Here are a few aspects I'm particularly interested in:
- How does it integrate? Is it part of your build tool, a servlet filter, a standalone program post-processing the WAR file, or something else?
- Is it easy to enable and disable? It's very unfunny to try and debug a minified script, but it's also useful for a developer to be able to test that the minification doesn't break anything.
- Does it work transparently, or does it have any side effects (apart from the ones inherent in minification) that I have to consider in my day-to-day work?
- Which minifier does it use?
- Does it lack any features that you can think of?
- What do you like about it?
- What don't you like about it?
This will mostly serve as a reference for my future projects (and hopefully other SOer's will find it informative, too), so all kinds of tools are interesting.
(Note that this is not a question about which minifier is best. We have plenty of those around already.)
I have written ant macros for Google Closure compiler and Yahoo compressor and include this file in different web projects.
Integration:
<import file="build-minifier.xml" />
in your build.xml, then invoke as usual ant tasks:<gc-js dir="${build.js.dir}" src="prototype" />
<yc-js-all dir="${build.js.dir}" />
Choice of two minifiers: Google Closure compiler and Yahoo compressor, you should download them manually and place near the xml file
Minifiers skip already compressed files (ending with
-min*
)Usually I make three versions of script: uncompressed (e.g.
prototype.js
) for debugging, compressed with closure compiler (prototype-min-gc.js
) for production server, compressed with Yahoo (prototype-min-yc.js
) for troubleshooting because closure compiler uses risky optimizations and sometimes produces invalid compressed file and Yahoo compressor is more safeYahoo compressor can minify all files in a dir with single macro, Closure compiler cannot
Round-up post
If you post something new in this thread, edit this post to link to yours.
apply
task (using YUI Compressor)We are using Ant task to minify js files with YUICompressor during production build and put result into a separated folder. Then we upload those files to a web server. You can find some good examples for YUI+Ant integration in this blog.
Here is an example:
This worked for me: https://bitbucket.org/m6_russell_francis/yui-compressor-ant-task/wiki/Home