JsZIP2.0(JavaScript压缩工具)

所属分类:[玄峰工具] | 发表于:2010-3-5 11:53:50 | 人气(709)

名称:JsZIP(JavaScript压缩工具)

版本:2.0

特点:高压缩、汉字转汉、代码智能优化、采用微软提供的压缩库(安全、准确)

 

之前写一个Js的压缩工具,是通过正则进行的转换,得到压缩目的,但经过测试,部分会压缩出错,导致调用JS会代码出错。本次将采用微软提供的ajaxmin进行压缩,压缩率比TBCompressor还要高,而且不用安装Java虚拟机,这给.net用户带来极大的方便,而且压缩工具还会对你的代码进行优化,要压缩js很好的一个工具。

 

代码优化例子

优化前

EXFCODE:
ExfTest=(function()
{
    alert("欢迎你来到玄峰软件");
})();

 

优化后

EXFCODE:
ExfTest=function(){alert("欢迎你来到玄峰软件")}();

 

也许你已经发现,在function()周围去掉了()了。以前是(function(){})() 优化后变成function(){}()了,同样代码仍然要执行,这里仅介绍了部分优化功能。

 

代化后还汉字转汉:

EXFCODE:
ExfTest=function(){alert("\u6B22\u8FCE\u4F60\u6765\u5230\u7384\u5CF0\u8F6F\u4EF6")}()

 

软件运行界面:

 

下载地址在最下面

 

下面是原版英文介绍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:

  • CatchAsLocal: treat the catch variable as if it’s local to the function scope. This is IE behavior; all other browsers seem to treat catch variables as their own scope. For instance:
    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.

  • CollapseToLiteral: convert “new Object()” to “{}” and “new Array()” to “[].” And of course, “new Array(1,2,3,4,5)” becomes “[1,2,3,4,5]” and “new Array(“foo”)” becomes “[“foo”]”. However, “new Array(5)” does not get crunched, because that makes an array with five initial slots – it’s not the same as [5].
  • 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.

  • EvalsAreSafe: Normally an eval statement can contain anything, including references to local variables and functions. Because of that, if we encounter an eval statement, that scope and all parent scopes cannot take advantage of local variable and function renaming because things could break when the eval is evaluated and the references are looked up. However, sometimes the developer knows that he’s not referencing local variables in his eval (like when only evaluating JSON objects), and this switch can be set to true to make sure you get the maximum crunching. Very dangerous setting; should only be used when you are certain that the eval won’t be referencing local variables or functions.
  • IndentSize: for the multi-line output feature, how many spaces to use when indenting a block (see OutputMode).
  • LocalRenaming: renaming of locals. There are a couple settings: KeepAll is the default and doesn’t rename variables or functions at all. CrunchAll renames everything it can. In between there is KeepLocalizationVars, which renames everything it can except for variables starting with L_. Those are left as-is so localization efforts can continue on the crunched code.
  • MacSafariQuirks: There was one quirk that Safari on the Mac (not the PC) needed that we were crunching out: throw statements always seem to require a terminating semicolon. Another Safari-specific quirk is that an if-statement only contains a function declaration, Safari will throw a syntax error if the declaration isn’t surrounded with curly-braces. Basically, if you want your code to always work in Safari, set this to true. If you don’t care about Safari, it might save a few bytes.
  • OutputMode: SingleLine crunches everything to a single line. MultipleLines breaks the crunched code into multiple lines for easier reading (won’t drive you insane trying to debug a single line). The only difference between the two outputs is whitespace. (see also: IndentSize).
  • RemoveUnneededCode: Should be set to true for maximum crunching. Removes unreferenced local functions (not global functions, though), unreferenced function parameters, quotes around object literal field names that won’t be confused with reserved words, and it does some interesting things with switch statements. For instance, if the default case is empty (just a break), it removes it altogether. If there is no default case, it also removes other empty case statements. It also removes break statements after return statements (unreachable code).
  • StripDebugStatements: removes “debugger” statements, any calls into certain namespaces like $Debug, Debug, Web.Debug or Msn.Debug. also strips calls to the WAssert function.

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);
1    1