Sunday, December 25, 2011

Minification of JS & CSS files using YuiCompressor

The process of minification includes not only to reduce the size of each of the JS or CSS files to it's bare minimum, hence by reducing the download size. But, also to stitch together multiple JS or CSS files into one JS and one CSS file. Hence reducing the number of HTTP requests made to the server for the appropriate files.


We happened to maintain several files for JS and CSS files and ensured that we used a number of files with some nomenclature like debug.01.file1.js / debug.01.file1.css
Reason, being we needed multiple files for modularization and also as we were using several third party plug-ins.
Another reason, was that ordering of processing the files does matter in some cases ex: jquerymobile js files needs to be preceded by jquery script files.


The final consolidated file will have a name of projectname.js / projectname.css


We used YuiCompressor for minification of js and css files.


Steps to setup:

Extract the zip folder to c:\   i.e. c:\yuicompressor-2.4.7


Setting Environment Variables to Java and Yuicompressor binaries.
Start > My Computer > Properties > Advanced System Settings > Environment Variable >

Append the below string to the PATH variable.
;C:\Program Files (x86)\Java\jre6\bin

type "$(ProjectDir)scripts\debug.*.js" | java -jar "C:\yuicompressor-2.4.7\build\yuicompressor-2.4.7.jar" --type js -o "$(ProjectDir)scripts\okig.mobile.js"
type "$(ProjectDir)styles\debug.*.css" | java -jar "C:\yuicompressor-2.4.7\build\yuicompressor-2.4.7.jar" --type css -o "$(ProjectDir)styles\okig.mobile.css"

Set the Post build event to run the following script. This will ensure that minification is done only in case of release mode. Version is done for both debug & release mode.
if $(ConfigurationName) == Release ( 
echo 'Minifying the JS and CSS'
type "$(ProjectDir)scripts\debug.*.js" | java -jar "C:\yuicompressor-2.4.7\build\yuicompressor-2.4.7.jar" --type js -o "$(ProjectDir)scripts\okig.mobile.js"
type "$(ProjectDir)styles\debug.*.css" | java -jar "C:\yuicompressor-2.4.7\build\yuicompressor-2.4.7.jar" --type css -o "$(ProjectDir)styles\okig.mobile.css"
)


2 comments:

  1. Might consider using the YUI Compressor for .net (http://yuicompressor.codeplex.com/) or the MS ajax minifier (http://ajaxmin.codeplex.com/) to avoid the dependency on the java run time since both provide a command line entry point. Both are available on nuget.

    Also, if you can afford turning off the sharepoint 2010 output caching, consider using http://RequestReduce.com which will automatically find, minify and bundle all scripts and css. It can also automatically sprite the css background images.

    ReplyDelete
  2. Thanks Matt. I agree with you on the JRE dependency for YuiCompressor. I had seen them but since, they did not have enough rating/downloads, I did'nt want to take a chance.
    I had not come across the RequestReduce project. May be will give it a shot.

    ReplyDelete