tag as possible
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 www.asp.net 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 a single JavaScript file is pretty straight forward. Simply type in the command prompt:
then add –clobber if you wish to override the outputfile.js if it exists, as in:
Minifying a single CSS file is equally simplistic as follows:
As before, add –clobber to override any existing output file.
Unfortunately there are no documentation on the asp.net 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"?>
<root>
<output path="outputfile.js">
<input path="inputfile1.js"/>
<input path="inputfile2.js"/>
-… ->
output>
root>
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:
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:
Tags: | javascript, css, optimization, front end, web developement | |
Categories: | JavaScript | CSS | User tip | |
Actions: | | Ping backs (1) |