Microsoft Ajax Minifier 介紹
是一個用於壓縮js和css檔案的免費工具
http://aspnet.codeplex.com/releases/view/40584
http://www.asp.net/ajaxLibrary/AjaxMinDocumentation.ashx
轉載請註明出處:http://surfsky.cnblogs.com/
--------------------------------------
使用
--------------------------------------
壓縮並覆蓋源檔案
ajaxmin inputfile.js
壓縮到新檔案(若檔案已經存在會報錯)
ajaxmin inputfile.js –out outputfile.js
用參數 -clobber 覆蓋目標檔案時不報錯
ajaxmin inputfile.js –out outputfile.js –clobber
預設狀態下,本工具將重新命名所有的本地變數和函數,若不想更改,可使用-rename:none參數
ajaxmin –rename:none inputfile.js –out outputfile.js
--------------------------------------
壓縮原理
--------------------------------------
- 去掉不必要的空格
- 去掉備忘(忽略標註了"important"的備忘)
- 去除不必要的分號
- Remove curly-braces around most single-statement blocks.
- 重新命名本地變數和函數
- Determine best string delimiters (single- or double-quotes) based on which option will generate the fewer escaped characters within the string.
- 合并多行連續的變數聲明
- 刪除建構函式中的空參數
- Remove unreferenced names for named function expressions.
- 刪除無引用的本地函數
- 刪除無法到達的代碼
--------------------------------------
樣本
--------------------------------------
去除可去除的空格和分號
if ( a == 0 )
{
a = 10;
alert(b/a);
}
--->
if(a==0){a=10;alert(b/a)}
去除括弧
if ( a == 0 )
{
a = 10;
}
--->
if(a==0)a=10;
綜合
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
縮減字串運算式
var g = "what's his \"name\"?";
--->
var g='what\'s his "name"?'
縮減變數聲明
var a = 0;
var b = "some string";
var c = 3.14;
--->
var a=0,b="some string",c=3.14;
簡化建構函式運算式
var img = new Image();
-->
var img = new Image;
簡化new代碼
var obj = new Object();
var arr = new Array();
var lst = new Array(1, 2, 3);
-->
var obj={},arr=[],lst=[1,2,3];
合并變數聲明
function foo(p)
{
var f = 10;
{
var g = 0;
f = p * g;
}
}
-->
function foo(p){var f=10,g=0;f=p*g}
去除無效的邏輯支路
if (a >= b) {} else { alert("a!=b") }
if (foo.bar()) {} else { alert("not foo.bar()") }
if (!a) {} else { alert("a") }
-->
if(a<b)alert("a!=b");
if(!foo.bar())alert("not foo.bar()");
if(a)alert("a");
將for迴圈用到的變數移到for運算式中
樣本1:
var i = 5;
for (; i > 0; --i)
{
alert(i);
}
-->
for(var i=5;i>0;--i)alert(i)
樣本2:
var n = 10;
for (var i=5; i > 0; --i)
{
n *= i;
}
-->
for(var n=10,i=5;i>0;--i)n*=i
樣本3:
var n=10, i;
for (i = 5; i > 0; --i)
{
n *= i;
}
-->
for(var n=10,i=5;i>0;--i)n*=i
樣本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 could possibly be a different context if placed into a var construct.
簡化方法調用
if (obj.method)
{
obj.method();
}
-->
obj.method&&obj.method()
簡化屬性調用
if (obj.prop)
{
i += obj.prop;
}
-->
obj.prop&&(i+=obj.prop)
if(obj.prop)i+=obj.prop
重新命名本地方法名及參數名
function DivideTwoNumbers(numerator, denominator, unsedparameter )
{
return numerator / denominator;
}
function a(a,b){return a/b}
提取重複變數
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=a }
--------------------------------------
編程介面
--------------------------------------
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);