DLL Version of Ajax Minifier

Modified on 2009/12/15 20:59 by Stephen Walther — Categorized as: Uncategorized

A DLL version of the code is also distributed. It does not contain any of the advanced switching or lint-style reporting that the EXE contains, but it does provide access to the abstract syntax tree produced by the JavaScript parser. This can be highly beneficial to projects that wish to do advanced modification or examination of the parsed code tree.

If all you need is to minify input code, you can simply use the supplied Microsoft.Ajax.Utilities.Minifier object. Create an instance of the object, and pass your input code to one of the MinifyJavaScript methods, depending on the language and whether you want to override the default settings. The return value is the minified code string.

public string MinifyJavaScript(string source);
public string MinifyJavaScript(string source, CodeSettings settings);

The various CodeSettings object properties modify the way the output JavaScript is generated:

function foo(a)
    a.b = 12345;
    a.c = 12345;
    a.d = 12345;
    a.e = 12345;

gets changed to:

function foo(a)
    var b=12345;

The savings are much more dramatic with large, frequently-used strings. Works with numbers, strings, nulls, and this-pointers. The this-pointers only get minified within the current scope, since child functions might have a different meaning for the pointer. It will also only pull out reused variables within function scopes – it won’t create a global variable with the constant, as that may interfere with other global variables. For the maximum minification, wrap all your code within a namespace function scope. Beware, though, that gzipped results may end up being larger when using this option due to the change in entropy of the resulting code.

If you wish to perform actions on the generated syntax tree directly, you can use the Microsoft.Ajax.Utilities.JSParser object directly. Create an instance of the object, passing in the source code you wish to parse. Calling the Parse method (passing in the same CodeSettings object) will return an abstract syntax tree node to the block representing your code.

      // create the parser from the source string.
      // pass null for the assumed globals array
      JSParser parser = new JSParser( source, null );
      string minified;

// hook the engine error event parser.CompilerError += new CompilerErrorHandler(OnCompilerError);

try { // parse the input Block scriptBlock = parser.Parse(settings); if (scriptBlock != null) { // we'll return the minified code minified = scriptBlock.ToCode(); } } catch(JScriptException e) { // other error handling }

There are two analogous methods for minifying CSS:

public string MinifyStyleSheet(string source);
public string MinifyStyleSheet(string source, CssSettings settings);

The CssSettings object contains all the settings switches available for CSS minification. Its properties are:

In addition to the Minifier object, developers can use the CssParser object directly. To minify CSS source, create an instance of the CssParser, set the appropriate Settings property values, and call the Parse method:

      CssParser parser = new CssParser();
      parser.CssError += new EventHandler<CssErrorEventArgs>(OnCssError);
      parser.FileContext = sourceFileName;
      string crunchedStyles = parser.Parse(source);