Labels

Tuesday, March 23, 2010

Microsoft Ajax Minifier

Introduction:

 

·         The Microsoft Ajax Minifier is a free tool that you can use to minify your JavaScript and Cascading Style Sheet files.

·         The minifier utility that allows you to shrink/minify both JavaScript and CSS files – which can improve the performance of your web applications. 

·         You can run this either manually as a command-line tool or now automatically integrate it using a Visual Studio build task. 

·         You can download it for free here.

 

There have been a number of utilities out there for “minifying” JavaScript. Read More..

 

Microsoft Ajax Minifier by default will try to reduce the code as much as possible: removal of comments, whitespace, and unnecessary semicolons and curly-braces; renaming of local variables and functions to shorter names; and removal of unused or unnecessary code. The -rename and –unused switches can be used to alter this default behavior.

 

Minifications:

 

Microsoft Ajax Minifier minifies script by parsing the source code into a JavaScript Syntax Parse Tree using code based on the ROTOR sources published by Microsoft. Once the original sources have been parsed, the tree is manipulated, and then walked to output the minimum amount of JavaScript code required to reproduce a comparable parse tree. Microsoft Ajax Minifier does not alter the source file.

Default minification are as:

  • Remove unnecessary white space.
  • Remove comments (except for statement-level “important” comments).
  • Remove unnecessary semicolons.
  • Remove curly-braces around most single-statement blocks.
  • Rename local variables and function.
  • Determine best string delimiters (single- or double-quotes) based on which option will generate the fewer escaped characters within the string.
  • Combine multiple adjacent variable declarations.
  • Remove empty parameter lists on constructors.
  • Remove unreferenced names for named function expressions.
  • Remove unreferenced local functions.
  • Remove many instances of unreachable code.

Few examples:

 

Origianl

Minified

if ( a == 0 )  

{  

a = 10;  

alert(b/a);  

 } 

if(a==0){a=10;alert(b/a)}

 

var a = 0;  

var b = "some string";  

var c = 3.14;  

 

var a=0,b="some string",c=3.14;

if ( a == 0 )  

 { 

     for(var o in opts) 

     { 

         if ( o > 0 ) 

         { 

             a += o; 

         } 

     } 

 } 

 else 

 { 

     b = c / a; 

 } 

if(a==0){for(var o in opts)if(o>0)a+=o}else b=c/a

function DivideTwoNumbers(numerator, denominator, unsedparameter ) 

    return numerator / denominator; 

}

function a(a,b){return a/b}  

 

 

 

Usage:

 

Two approaches:

 

·         Command Line

·         As a Build Task

 

Command Line: After you install the Microsoft Ajax Minifier, you can use the tool from the command line. Open the Microsoft Ajax Minifier Command Prompt from the Microsoft Ajax Minifier program group. Next, enter the name of an input file and the name of an output file like this:

 

ajaxmin test.js -o test.min.js

ajaxmin test.css -o test.min.css

 

Build Task:

 

·         You can integrate the Microsoft Ajax Minifier into your Visual Studio Build process.

·         Every time you perform a build in a Visual Studio ASP.NET project, you can minify all of your JavaScript and Cascading Style Sheet files automatically.

·         You can use the Ajax Minifier with both ASP.NET Web Forms and ASP.NET MVC Web Application Projects (WAPs).

 

Note: However, you cannot use the minifier with ASP.NET Web Forms Websites.

Read More to Follow the steps.

Hope this helps.

Arun

 

 

 

 

No comments:

Post a Comment