views:

656

answers:

13

Do you have a step in your deployment process that minifies JS? Do you have any sort of preprocessor for your JavaScript that allows you to leave in comments and console.logs and then have them automatically stripped out? Is your JavaScript machine generated by GWT or Script#? Do you use ANT or another tool to automate deployment?

I see a lot of JavaScript that looks like it comes right out of the editor, complete with lots of white space and comments. How much of that is due to not caring about the state of the deployed code, and how much is due to the spirit of the open web?

A: 

One word- packer

Alec Smart
Most of the libraries have moved away from packer and to a minifier with gzip. Packer uses up valuable time as the code unpacks itself.
Nosredna
+2  A: 

Check out YUI Compressor its a console app that you can use to minify (strip out comments, whitespace etc..) and also obfuscate your javascript files.

Rob
+11  A: 

I usually check it out with JSLint to make sure it is bug-free, then pack it/encode it with YUI compressor.

m_oLogin
Just to note, but JSLint doesn't actually check the script to ensure it is bug from from a operational standpoint (e.g. that range test is checking the right range) but to double check the syntax to make sure you didn't do anything wrong.
Rob
that's right. Thanks for pointing it out.
m_oLogin
+1  A: 

For one of our products, we concatenate all Javascript files together (most files are used on most pages, so this makes sense for us) and use Javascript::Minifier. This has given us a pretty nice speed boost.

Chris Simmons
+1  A: 

A lot of it is probably due to not caring about people that might be viewing your pages on slower machines with slower connections and assuming that everyone has a 50Mbps line and three Gigs of RAM.

We are minifying our (hand-written + plugins, jQuery, etc.) JS as a part of the build process in .NET environment. No preprocessor, this is something we should definitely be doing once time permits.

P.S. By the way, we're not using console.log, as this will break IE. Instead we have a simple wrapper function, something like:

function log(stuff) {
    if (window.console && window.console.log) {
        console.log(stuff);
    }
};
dalbaeb
What kind of JavaScript libraries are you writing, where minify would actually have a non-trivial effect on the end-user's free RAM?
Ben Dunlap
Yes, that's true, sorry. Please disregard the RAM bit. Minifying has no effect on RAM of course, I just threw in the matter of writing quality code for no reason.
dalbaeb
+2  A: 

JSMin it from Douglas Crockford. We've got it hooked up as a macro in Studio as well as a post build item for some of our larger projects

Mcbeev
A: 

Light a candle, whisper a prayer against IE6 errors, and click "go". Does that count? :)

Some Canuck
Yeah, but I think you need something stronger than a whisper to deal with IE6.
Nosredna
Have you tried sacrificing farm animals?
TokenMacGuy
A: 
  1. I don't minify my own javascript code since text tends to gzip/compress well.
  2. I would minify a very large (say > 100 kb) javascript library (but then again I probably would not want to be using such a large library (or just ship what i use)).

I tend to believe that a lot of the javascript-minification is (in reality) done to achieve some sort of (futile) obfuscation of javascript code instead of the proclaimed end-user performance gain.

ChristopheD
If there's a lot of comments in the code, one might use minify as a convenient stripper of comments.
Nosredna
+2  A: 

My steps include:

  1. I write Javascript using TextMate with the Javascript Tools bundle installed. This JSLint's my files on every save and notifies me when errors occur.
  2. I use Sprockets to automatically concatenate my various Javascript files.
  3. I run the resulting concatenation through jsmin to generate a minified version.

I end up with a concatenated lib.js file and a minified lib.min.js file. One I use for development, one for production. TextMate commands help automate it all.

I'm still looking for a good solution to actually (unit) test my scripts.

avdgaag
Do I have to know Ruby to use Sprockets? Or can I just install Ruby and learn Sprockets?
Nosredna
@Nosredna: You don't have to know Ruby, but it helps to customize it. I call Sprockets from a Ruby script, but it also has a command line tool.
avdgaag
On a side note, the windows "equivalent" of TextMate is e-texteditor, which also jslints the files.
m_oLogin
+1  A: 

I have a PHP script that does it on the server side and keeps a cache of whatever it pulls from the source folder(s).

Nolte Burke
+2  A: 
yacoob
+1 for some hard performance data.
Colonel Sponsz
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).
Pure.Krome
A: 

I thought I would share my approach to js deployments. Have a look at this blog post: http://www.picnet.com.au/blogs/Guido/post/2009/12/10/Javascript-runtime-compilation-using-AspNet-and-Googles-Closure-Compiler.aspx

This also includes code to compile (using google's closure compiler) at runtime (when needed).

Thanks Guido

gatapia