Microsoft Ajax Minifier 介绍
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);
转载请注明出处:http://surfsky.cnblogs.com