Microsoft Ajax minifier Introduction

Source: Internet
Author: User

Microsoft Ajax minifier Introduction
Is a free tool for compressing JS and CSS files.
Http://aspnet.codeplex.com/releases/view/40584
Http://www.asp.net/ajaxLibrary/AjaxMinDocumentation.ashx
Reprinted please indicate the source: http://surfsky.cnblogs.com/

--------------------------------------
Use
--------------------------------------
Compress and overwrite source files
Ajaxmin inputfile. js

Compress to a new file (if the file already exists, an error is returned)
Ajaxmin inputfile. js-out outputfile. js

No error is reported when clobber is used to overwrite the target file.
Ajaxmin inputfile. js-out outputfile. js-clobber

By default, this tool renames all local variables and functions. If you do not want to change them, you can use the-Rename: None parameter.
Ajaxmin-Rename: None inputfile. js-out outputfile. js

--------------------------------------
Compression Principle
--------------------------------------
-Remove unnecessary spaces.
-Remove the remarks (ignore the remarks marked with "important)
-Remove unnecessary semicolons
-Remove curly-braces around most single-statement blocks.
-Rename local variables and functions
-Determine best string delimiters (single-or double-quotes) based on which option will generate the fewer escaped characters within the string.
-Merge multiple consecutive variable declarations.
-Delete null parameters in the constructor
-Remove unreferenced names for named function expressions.
-Delete A local function without reference
-Delete codes that cannot be reached

--------------------------------------
Example
--------------------------------------
Remove detachable spaces and semicolons
If (A = 0)
{
A = 10;
Alert (B/);
}
--->
If (A = 0) {A = 10; alert (B/)}

Remove brackets
If (A = 0)
{
A = 10;
}
--->
If (A = 0) A = 10;

Comprehensive
If (A = 0)
{
For (VAR o in opts)
{
If (O> 0)
{
A + = O;
}
}
}
Else
{
B = C/;
}
--->
If (A = 0) {for (VAR o in opts) if (O> 0) A + = O} else B = C/

Shrink string expression
VaR G = "What's his \" Name \"? ";
--->
VaR G = 'what \'s his "name "? '

Variable reduction Declaration
VaR a = 0;
VaR B = "some string ";
VaR c = 3.14;
--->
VaR a = 0, B = "some string", c = 3.14;

Simplified constructor expression
VaR IMG = new image ();
-->
VaR IMG = new image;

Simplify new code
VaR OBJ = new object ();
VaR arr = new array ();
VaR lst = new array (1, 2, 3 );
-->
VaR OBJ = {}, arr = [], LST = [1, 2, 3];

Merge variable Declaration
Function Foo (P)
{
VaR F = 10;
{
VaR G = 0;
F = p * g;
}
}
-->
Function Foo (p) {var F = 10, G = 0; F = p * g}

Remove Invalid logical branches
If (A> = B) {} else {alert ("! = B ")}
If (FOO. Bar () {} else {alert ("not Foo. Bar ()")}
If (! A) {} else {alert ("")}
-->
If (A <B) Alert ("! = B ");
If (! Foo. Bar () Alert ("not Foo. Bar ()");
If (a) Alert ("");

Move the variables used by the for loop to the for expression.
Example 1:
VaR I = 5;
For (; I> 0; -- I)
{
Alert (I );
}
-->
For (VAR I = 5; I> 0; -- I) Alert (I)
Example 2:
VaR n = 10;
For (VAR I = 5; I> 0; -- I)
{
N * = I;
}
-->
For (VAR n = 10, I = 5; I> 0; -- I) N * = I
Example 3:
VaR n = 10, I;
For (I = 5; I> 0; -- I)
{
N * = I;
}
-->
For (VAR n = 10, I = 5; I> 0; -- I) N * = I
Example 4:
VaR I;
For (I = 5, n = 10; I> 0; -- I)
{
N * = I;
}
-->
VaR I; for (I = 5, n = 10; I> 0; -- I) N * = I
Because the N variable being assigned to is not part of the preceding var statement, and cocould possibly be a different context if placed into a var construct.

Simplified method call
If (obj. Method)
{
OBJ. Method ();
}
-->
OBJ. Method & obj. Method ()

Simplify property calls
If (obj. Prop)
{
I + = obj. Prop;
}
-->
OBJ. Prop & (I + = obj. Prop)
If (obj. Prop) I + = obj. Prop

Rename the local method name and Parameter Name
Function dividetwonumbers (numerator, denominator, unsedparameter)
{
Return numerator/denominator;
}
Function A (a, B) {return a/B}

Extract repeated Variables
Function Foo (P)
{
P [0]. style. Display = "Block ";
P [1]. style. Display = "Block ";
P [2]. style. Display = "Block ";
P [3]. style. Display = "Block ";
P [4]. style. Display = "Block ";
}
-->
Function Foo (p) {var A = "Block"; P [0]. style. display = A; P [1]. style. display = A; P [2]. style. display = A; P [3]. style. display = A; P [4]. style. display =}

--------------------------------------
Programming Interface
--------------------------------------
Namespace
Using Microsoft. Ajax. utilities;

Jsparser
// 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
}

CSS
Cssparser parser = new cssparser ();
Parser. csserror + = new eventhandler <csserroreventargs> (oncsserror );
Parser. filecontext = sourcefilename;
String crunchedstyles = parser. parse (source );

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.