
名称:JsZIP(JavaScript压缩工具)
版本:2.0
特点:高压缩、汉字转汉、代码智能优化、采用微软提供的压缩库(安全、准确)
之前写一个Js的压缩工具,是通过正则进行的转换,得到压缩目的,但经过测试,部分会压缩出错,导致调用JS会代码出错。本次将采用微软提供的ajaxmin进行压缩,压缩率比TBCompressor还要高,而且不用安装Java虚拟机,这给.net用户带来极大的方便,而且压缩工具还会对你的代码进行优化,要压缩js很好的一个工具。
代码优化例子
优化前:
优化后:
也许你已经发现,在function()周围去掉了()了。以前是(function(){})() 优化后变成function(){}()了,同样代码仍然要执行,这里仅介绍了部分优化功能。
代化后还汉字转汉:
软件运行界面:

下载地址在最下面
下面是原版英文介绍ajaxmin的功能
A DLL version of the crunching code is also distributed. It does not contain any of the advanced switching or lint-style reporting that the EXE contains, but it does provide access to the abstract syntax tree produced by the JavaScript parser. This can be highly beneficial to projects that wish to do advanced modification or examination of the parsed code tree.
If all you need is to crunch input code, you can simply use the supplied Microsoft.Ajax.Utilities.ScriptCruncher object. Create an instance of the object, then pass your input code to the Crunch method. The return value is the crunched code string. There are two overloads:
The various CodeSettings object properties modify the way the output code is generated:
try
{
var e = 10;
e.ackgagbarf(); // throws error
}
catch(e)
{
// handle error
}
alert(e);
In IE, the alert displays the error (“[object Error]”) because the “e” variable in the catch is the same “e” variable defined with the var statement. Run this code in any other browser, and you just get an alert box with “10” in it, because the e variable in the catch is within its own special scope. Setting CatchAsLocal to true means that the Microsoft Ajax Minifier treats the e in the catch-statement as if it’s the same local variable. This is important when we are renaming variables. This one really is fuzzy, since most code doesn’t have this kind of weirdness that we could break when we rename these variables.
CombineDuplicateLiterals: combine duplicate literals into local variables. So the code:
function foo(a)
{
a.b = 12345;
a.c = 12345;
a.d = 12345;
a.e = 12345;
}
gets changed to:
function foo(a)
{
var b=12345;
a.b=b;
a.c=b;
a.d=b;
a.e=b
}
The savings are much more dramatic with large, frequently-used strings. Works with numbers, strings, nulls, and this-pointers. The this-pointers only get crunched within the current scope, since child functions might have a different meaning for the pointer. It will also only pull out reused variables within function scopes – it won’t create a global variable with the constant, as that may interfere with other global variables. For the maximum crunching, wrap all your code within a namespace function scope.
If you wish to perform actions on the generated syntax tree directly, you can use the Microsoft.Ajax.Utilities.JSParser object directly. Create an instance of the object, passing in the source code you wish to parse. Calling the Parse method (passing in the same CodeSettings object) will return an abstract syntax tree node to the block representing your code.
// create the parser from the source string.
// pass null for the assumed globals array
JSParser parser = new JSParser( source, null );
// 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 crunched code
crunched = scriptBlock.ToCode();
}
}
catch(Exception e)
{
// other error handling
}
public string Crunch(string source); public string Crunch(string source, CodeSettings settings);