JavaScript Compression

Sep 22, 2008 at 11:24 PM
Does it make any sense to pack the js file in order to decrease its size?
Can we somehow use the same approach that Microsoft Ajax library uses: the Scripmanager ScriptMode property specifies that the debug or release version of client script will be rendered?
Sep 22, 2008 at 11:27 PM
Yes that would be good idea, having 2 versions of scripts one for release which is minified and another one for debugging. Good idea for production use
Coordinator
Sep 22, 2008 at 11:40 PM
That's definitely a good idea.

In one of my controls, I use this code to conditionally output the correct script:

// Register the JavaScript class include.
if (HttpContext.Current.IsDebuggingEnabled)
  Page.ClientScript.RegisterClientScriptResource(GetType(), "PostBackRitalin.PostBackRitalin.js");
else
  Page.ClientScript.RegisterClientScriptResource(GetType(), "PostBackRitalin.PostBackRitalin.min.js");

I haven't tested it extensively, but it appeared to work as intended without requiring any configuration on the user's end.  Maybe it would work for us as well.
Developer
Sep 23, 2008 at 4:45 AM
So assuming we supply the two scripts, the ASP.NET AJAX framework can handle switching between a release and debug version of the scripts automatically.

Using a ScriptReference, we supply the name of the release version of the script. Using MSFT naming conventions, it would be UsernameAvailabilityValidator.js. Then we also provide a debug version of the script, UsernameAvailabilityValidator.debug.js. The ScriptReference points to the release version of the script.

Then, when the ScriptManager's ScriptMode is set to auto, which it is by default, it will use the IsDebuggingEnabled flag, which is populated by the web.config compilation node, to determine which file to load from the assembly. We can force the ScriptManager to use a particular ScriptMode by setting the property to release, debug, or inherit or by setting the ScriptMode property on the ScriptReference.

This is how MSFT automatically switches between the release and debug version of their AJAX Library.

More information about this capability can be found here: ScriptMode.

I totally agree with this step as it makes the control look like a professional product. It would be nice if MSFT provided some automatic build step to minify and create the release version, but I don't think they're quite there yet. I would recommend doing this as a final build step as modifying the minified file as you go can be a real pain in the ass.
Coordinator
Sep 23, 2008 at 5:01 AM
For one-off minification, I often find myself at this page: http://fmarcia.info/jsmin/test.html
Developer
Sep 23, 2008 at 7:46 PM
Can we create an issue that won't be resolved until we're all done??
Coordinator
Sep 24, 2008 at 7:16 AM
We can create an issue targeted at release "1.0" for this.