Compressing JS and CSS with Ant

Mike Henke sent me a great link to this article: Building Web Applications With Apache Ant.

It's a great article but one section - "Minify your JavaScript and CSS files" really caught my eye. I've been using more and more jQuery code and related plugins in my projects (as well as more CSS) and to be able to compress these during deployment seems like a great bonus. Julien provides this example target:

CODE:
  1. <target name="js.minify" depends="js.preprocess">
  2.     <java jar="yuicompressor.jar" fork="true">
  3.         <arg value="foo.js"/>
  4.     </java>
  5. </target>

That looks easy enough! :)

Stephen Beal on the jQuery mailing list provided an issue to be aware of however:

While minification/packing rarely leads to semantic changes in JS
code, it does on occasion happen. It is easy to write code which might
run in uncompressed form yet may not be correct once minification
occurs (e.g., forgetting to add a semicolon after: var x = function()
{...};).

So you would need to test your application after compression to make sure everything was still working as expected.

Update:
Mike Henke has been busy: Adding JS/CSS compression and JS validation using YUI Compressor, JSLint, and jsmin to ant script

Possibly Related:

One Comment

  1. Posted September 13, 2007 at 1:52 pm | Permalink

    I wonder if you run the JSLint before and after the compress if you could see any compression issues. Just a thought (maybe save as a file to compare). Thanks for the mention.

Creative Commons License

Creative Commons License This article: Compressing JS and CSS with Ant, unless otherwise expressly stated, is licensed under a Creative Commons Attribution-Noncommercial-Share Alike 3.0 United States License .

Copyright © 2007 thecrumb.com. All rights reserved.