AjaxMin.DLL
The Microsoft Ajax Minifier also provides a DLL that can be used in your own applications. Classes are defined in the
Microsoft.Ajax.Utilities namespace. There are two main ways to access the functionality.
Minifier Class
Instantiate the Minifier class and call either the
MinifyJavaScript or MinifyStyleSheet methods, passing in the source to be minified as a string value. The return value from both those functions is the minified code as a string value. If there were any errors
or warnings minifying your code, they will be in the ErrorList property, which is an ICollection<ContextError>. This is the simplest way to minify your sources using the DLL.
using System;
using System.IO;
using Microsoft.Ajax.Utilities;
namespace AjaxMinSample
{
class Program
{
static void Main(string[] args)
{
if (args.Length == 1)
{
string source;
using (var inputFile = new StreamReader(args[0]))
{
source = inputFile.ReadToEnd();
}
var minifier = new Minifier();
Console.WriteLine(minifier.MinifyJavaScript(source));
foreach (var error in minifier.ErrorList)
{
Console.Error.WriteLine(error.ToString());
}
}
}
}
}
CssSettings
The MinifyStylesheet method can optionally take a
CssSettings object to specify various CSS minification settings. There are two ways to generate one: manually, by creating a default
CssSettings object and then setting all properties you wish to change from the default settings, or by creating an instance of the
SwitchParser object, parsing a command-line switch by calling the
Parse method on that object, and then passing the generated CssSettings property as the second parameter to the
MinifyStylesheet method.
var switchParser = new SwitchParser();
switchParser.Parse("-css:decls -colors:hex");
var minifier = new Minifier();
var minifiedStyle = minifier.MinifyStyleSheet(styleAttrValue, switchParser.CssSettings);
CodeSettings
The MinifyJavaScript method can optionally take a
CodeSettings object to specify various JavaScript minification settings. The
JSParser object’s
Parse method must be passed a CodeSettings object. There are two ways to generate one: manually, by creating a default
CodeSettings object and then setting all properties you wish to change from the default settings, or by creating an instance of the
SwitchParser object, parsing a command-line switch by calling the
Parse method on the object, and then passing the generated
JSSettings property as the second parameter to the
MinifyJavaScript method, or as the only parameter to the
Parse method on the
JSParser object.
JSParser Classes
If you wish to take advantage of the JavaScript parser and examine and/or manipulate the resulting abstract syntax tree (AST), you can instantiate an object of the type
JSParser, and utilize its methods and properties directly. The main advantage here is that the parser returns an object of type Block that represents the top-level set of statements for the input script. It is then
possible to traverse the tree, delete nodes, add nodes, move them around, rename variables – anything is possible once you have the AST. And once you are done, simply call the
ToCode method on the Block object returned from the parser to get the resulting JavaScript code.
using System;
using System.IO;
using Microsoft.Ajax.Utilities;
namespace AjaxMinSample
{
class Program
{
static void Main(string[] args)
{
if (args.Length == 1)
{
string source;
using (var inputFile = new StreamReader(args[0]))
{
source = inputFile.ReadToEnd();
}
JSParser parser = new JSParser(source);
parser.CompilerError += ErrorHandler;
CodeSettings settings = new CodeSettings();
Block block = parser.Parse(settings);
Console.WriteLine(block.ToCode());
}
}
static void ErrorHandler(object source, JScriptExceptionEventArgs ea)
{
Console.Error.WriteLine(ea.Error.ToString());
}
}
}
AST Traversal
If you would like to traverse the entire tree from root to leaf, you could start with the Block object and simply iterate over all the nodes in the Children collection recursively:
static void IterateChildren(AstNode node)
{
foreach (var child in node.Children)
{
IterateChildren(child);
}
}
Or you can use a
Visitor pattern. Define a new class derived from the Microsoft.Ajax.Utilities.TreeVisitor class. There are a number of virtual methods on TreeVisitor – one for each concrete AST node type – that can be overridden. The base TreeVisitor class
methods simply iterates over the node’s children nodes. For example, if the desire it to iterate over all the nodes in the tree and print out the name of all variables defined in var statements, a visitor can be define like this:
class MyVisitor : TreeVisitor
{
public MyVisitor() { }
public override void Visit(VariableDeclaration node)
{
base.Visit(node);
Console.WriteLine(node.Identifier);
}
}
To execute the visitor, instantiate an object and pass the Block node to the Visit method:
var myVisitor = new MyVisitor();
myVisitor.Visit(block);
Nodes in the AST tree can be created, inserted, replaced, deleted, and modified. Internally Ajax Minifier uses a number of visitor classes to modify the tree for maximum minification.
Unfortunately, currently only JavaScript is parsed into an AST that can be traversed. The CSS syntax tree is not available at this time.