views:

198

answers:

2

I tried out the online JS Minifier but it cuts out conditional comments like:

 var u = navigator.userAgent;var e=/*@cc_on!@*/false;

                           BECOMES

 var u=navigator.userAgent;var e=false;

This would affect the operation of the code, so instead of manually adding the stripped out comments manually, I'm looking for a minifier that intelligently preserves these and any such comments.

+1  A: 

YUICompressor reportedly does the needful. Here is an excerpt from a blog:

...
But fret not, it’s not as bad as you think. Unfortunately, JSMin removes conditional compilation comments, but I believe this is due to not having been updated in a while, and not up to speed with modern web development practices.

Using other tools such as YUI compressor or packer does indeed work fine with conditional compilation. ...

You can read the entire post at http://robertnyman.com/2008/05/26/conditional-compilation-in-javascript/

Vinnie
Note however that although YUI compressor will retain conditional comments, the presence of said comments will automatically reduce the level of compression such that variable names are no longer shortened/obfuscated. It has to do this because the comments themselves may refer to variables in scope, much like eval could. See http://www.julienlecomte.net/blog/2007/09/22/
Crescent Fresh
Packer http://dean.edwards.name/packer/ works great for me, and it has variable name shortening too (optional).
Jenko
A: 

There's also a .NET port of YUI Compressor which allows you to:-

  • intergrate the minification/file combining into Visual Studio post-build events
  • intergrate into a TFS Build (including CI)
  • if you wish to just use the dll's in your own code (eg. on the fly minification).

because this is a port of the (original) java version YUI Compressor, which Vinnie and Crescentfresh mention above, it should give you the same results BUT all in the .NET environment -- no need for java.

HTH.

Pure.Krome