views:

1028

answers:

4

Hello SO,

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.)

+1  A: 

I tried two ways:

  1. using a servlet filter. When in production mode, the filter is activated and it compress any data bounded to URL like *.css or *.js
  2. using maven and yuicompressor-maven-plugin; the compression is perfomed una-tantum, (when assembling the production war)

Of course the latter solution is better since it does not consume resources at runtime (my webapp is using google app engine) and it doesn't complicate your application code. So assume this latter case in the following answers:

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?

using maven

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.

you activate it only when assemblying the final war; in development mode you see the uncompressed version of your resources

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?

absolutely

Which minifier does it use?

YUI compressor

Does it lack any features that you can think of?

no, it is very complete and easy to use

What do you like about it?

it is integrated with my favourite tool (maven) and the plugin is in the central repository (a good maven citizen)

dfa
Maven plugin - nice. Too bad my current projects all use ant :)
gustafc
you can create a target that build a "production war file", using the YUI ant task
dfa
+1  A: 

Our project has handled it a number of ways but we have continued to use the YUI Compressor through our different iterations.

We initially had a servlet handle the compression for JavaScript the first time that particular file was accessed; it was then cached. We already had a system in place to handle custom property files so we simply updated our configuration files to support enabling or disabling the compressor depending on the environment we were working in.

Now the development environments never use compressed JavaScript for debugging purposes. Instead we handle the compression in our build process when exporting our application to a WAR file.

Our client has never raised concerns about the compression and the developers don't notice it until they decide to debug JavaScript. So I'd say it's rather transparent with minimal, if any, side affects.

Doomspork
How do you use the YUI compressor from your build process? The Maven plugin or something else?
gustafc
Sorry, we use Ant currently. Here is a useful link for the Ant Task : http://blog.gomilko.com/2007/11/29/yui-compression-tool-as-ant-task/
Doomspork
+2  A: 

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:

<target name="js.minify" depends="js.preprocess">
    <apply executable="java" parallel="false">
        <fileset dir="." includes="foo.js, bar.js"/>
        <arg line="-jar"/>
        <arg path="yuicompressor.jar"/>
        <srcfile/>
        <arg line="-o"/>
        <mapper type="glob" from="*.js" to="*-min.js"/>
        <targetfile/>
    </apply>
</target>
serg
Snippet; nice. Do you retarget your `script src` on dev builds or do you just copy non-minified files into the compressed/js directory?
gustafc
Just for production upload compressed files over original ones in public_html/js. Good thing is that there is no coding or path changes between local and production, the bad thing is that you have to do some manual upload and overwriting (I'm sure it can be automated, but for us it is not worth it, uploading few js files once in a while is not too big of a deal).
serg
+1  A: 

Round-up post

If you post something new in this thread, edit this post to link to yours.

gustafc