Combining and minifying JavaScript and CSS files with Ajax Minifier

Why should you bother with combing and minifying JavaScript and CSS files? If you are a web developer working with sites with any significant traffic, you simply should because of the impact it will have on the load time of your site and how your visitors will perceive the page loading faster.

In the field of web site optimization, pioneered by Yahoo, there are many rules or guidelines which you should try to follow in hunt for milliseconds on the client side. In fact, it’s on the client that you will get the most value for your optimization work rather than on the server side [SOUDERS]. This is a subset of the rules regarding JavaScript and CSS.

Guidelines for web site optimization

  • Put all your CSS in separate .css files
  • Put your CSS links as early in the header section of the page as possible
  • Minify your CSS files, i.e. remove whitespace etc.
  • Try combining all of your CSS files into one

  • Put all your JavaScript code in separate .js files
  • Put your JavaScript imports as close to the </body> tag as possible
  • Minify your JavaScript files
  • Try combing all of your JavaScript files into one
  • If possible try loading large scripts asynchronously

  • Enable GZIP compression on all your static content, e.g. CSS and JavaScript files
  • Set far future expire headers on your static content

Microsoft Ajax Minifier

I have searched for a tool that could help me implement many of the optimization guidelines and found a few worth taking a closer look at, one of these is the Google Closure Compiler. It has loads of features including file combining, minification, “compiling” your JavaScript into better JavaScript, as well as an API for loading your scripts asynchronously.

However, being a pragmatic .NET developer, I chose the most easily accessible tool available for me, which is Microsoft’s Ajax Minifier. It’s a free tool available on Codeplex and documentation is provided on the site.

Ajax Minifier comes as a command-line application and after you have installed it you can use it through it’s own command prompt.

Minifying JavaScript

Minifying a single JavaScript file is pretty straight forward. Simply type in the command prompt:

  • ajaxmin inputfile.js –out outputfile.js

then add –clobber if you wish to override the outputfile.js if it exists, as in:

  • ajaxmin inputfile.js –out outputfile.js –clobber

Minifying CSS

Minifying a single CSS file is equally simplistic as follows:

  • ajaxmin inputfile.css –out outputfile.css –clobber

As before, add –clobber to override any existing output file.

Combining files

Unfortunately there are no documentation on the site about how to combine multiple files into one, however, the ajaxmin command itself provides a hint given the /? parameter:

As an alternative to the input file parameter you can use the –xml parameter and supplying it with the path to an xml file of the following format:

<?xml version="1.0" encoding="utf-8"?> 
    <output path="outputfile.js"> 
        <input path="inputfile1.js"/> 
        <input path="inputfile2.js"/> 

The Ajax Minifier will combine all of your input files into one, minify it, and produce the result in the output file. Using this approach you can also control in what order the files are loaded into the page and sort out any dependency issues, just as you would, had you listed includes in the regular html file (or .master or whatever).

The Ajax Minifier does not seem to have the smarts to deduce that this is JavaScript files so you need to supply it with a –js parameter like so:

  • ajaxmin –js –xml jsxmlfile.xml –clobber

I haven’t found a way to combine JavaScript and CSS in the same xml file so you need to make a separate file for you CSS files with the same format as above and instead of the –js parameter, use the –css dito:

  • ajaxmin –css –xml cssxmlfile.xml –clobber

Comments (4) -

  • Nenoufar

    2/9/2014 12:27:35 AM | Reply

    I'll give the Ajax Minifier a try. Might be worth creating a batch file to further automate the process. Thanks for the heads up Smile

Pingbacks and trackbacks (1)+

Disclaimer: The opinions expressed herein are my own personal opinions and do not represent my employer's view in any way.