Script comments

Jun 9, 2011 at 12:39 AM


When tetsting the minifier tool against the latest version of the jquery.js file, I was surprised to see that the output still contained comments (e.g. jQuery JavaScript Library v1.5.2)! Am I doing something wrong? Am I missing the proper option to use to remove the comments? If so, shouldn't that be on by default?


Jun 9, 2011 at 1:18 AM

What you need to know is that there is an informal industry standard for different levels of comments. I'm sure you are aware of the "regular" comment syntax: // single line and /* multi-line */. These are stripped from the sources by AjaxMin. There is also the concept of important comments, which most minification tools out there today respect. These are comments that the developer intends to not be removed from the sources -- things like copyright notices, licensing text, and such. The syntax for those comments have an exclamation mark as the third character, for example: /*! multiline important comment */ If you look at the sources for jQuery, you will notice that the copyright/licensing text at the top of the file follows the important-comment syntax, and therefore AjaxMin does not remove that comment by default. I would suggest leaving it in -- it's part of the license wording that the notice remain in the distributed file.

That said, if you still want to strip out important comments from the sources, assuming doing so does not violate any licensing agreement(s), you can use the [undocumented] -kill switch to turn off the feature that preserves important comments. The syntax is -kill:0xHEX or -kill:DEC, where HEX is a hexadecimal representation of the feature-bits you wish to turn off (and DEC is the decimal equivalent). The PreserveImportantComments feature bit is 1, so to turn off the feature that preserves important comments, add -kill:1 to your command line. Not that I recommend doing that; there's usually a good reason the developer marks a comment as important.

Jun 9, 2011 at 1:43 AM

I see, makes sense. It's just that I was trying to compare the output of Google's Closure Compiler to the Ajax minifier, and Google's tool removed the comments by default. As a side note, what compression level is the Ajax minifier using to estimate the gzip output size? Again, I'm asking because for roughly for the same size uncompressed, Google's claiming a considerably smaller compressed size.

Thanks again!

Jun 9, 2011 at 1:55 AM

I'd say Google Closure Compiler is doing the wrong thing in regard to important comments, but that's my [admittedly biased] opinion. :)

The gzip estimation is calculated by using the .NET GzipStream class, which uses a normal (fast) compression level. Improving gzipped size is something I am actively working on at the moment. There are lots of nifty things that can be done to decrease the entropy of the resulting output so the gzip algorithm will match more strings more often and therefore result in a smaller number of compressed bytes. Watch this space for better gzipped results soon.

Jun 9, 2011 at 2:17 AM

I agree, should at least be an option to keep the important comments. Maybe they have an undocumented option to keep them ;)

Looking forward for the gzip improvements. Keep up the good work!


Jun 10, 2011 at 4:10 PM

Heh, thanks! Well, you have an excellent point, so the next release (4.22) will include a command-line switch to turn off the preserve-important-comments feature so you don't have to dive into the -kill switch. The new switch (appropriated from the CSS minifier, so it's not really "new") is "-comments" and the two possible values are "important" (default) or "none" (remove important comments). For example: -comments:important is the default, and -comments:none would get rid of them. 

Jun 10, 2011 at 4:11 PM

Yeah, saw it originally for css and wondered why it didn't exist for JS :) Thanks mate.