From 307c272b02c46971f4add5a6c550b27a95359a0f Mon Sep 17 00:00:00 2001 From: Alex Sexton Date: Thu, 8 Nov 2012 16:52:58 -0600 Subject: [PATCH] Working build with AMD stripping and minification. --- LICENSE | 1 + build.sh | 7 +- modular-build-min.html | 11 + modular-build.html | 14 - modular.html | 3 - node_modules/.bin/uglifyjs | 1 + node_modules/uglify-js/.npmignore | 6 + node_modules/uglify-js/README.html | 1012 ++++++++ node_modules/uglify-js/README.org | 593 +++++ node_modules/uglify-js/bin/uglifyjs | 334 +++ node_modules/uglify-js/docstyle.css | 75 + node_modules/uglify-js/lib/consolidator.js | 1220 ++++++++++ node_modules/uglify-js/lib/parse-js.js | 1369 +++++++++++ node_modules/uglify-js/lib/process.js | 2117 +++++++++++++++++ node_modules/uglify-js/lib/squeeze-more.js | 78 + node_modules/uglify-js/package.json | 28 + node_modules/uglify-js/test/beautify.js | 28 + .../uglify-js/test/testconsolidator.js | 1391 +++++++++++ node_modules/uglify-js/test/testparser.js | 409 ++++ .../test/unit/compress/expected/1e21.js | 1 + .../test/unit/compress/expected/array1.js | 1 + .../test/unit/compress/expected/array2.js | 1 + .../test/unit/compress/expected/array3.js | 1 + .../test/unit/compress/expected/array4.js | 1 + .../test/unit/compress/expected/assignment.js | 1 + .../unit/compress/expected/concatstring.js | 1 + .../test/unit/compress/expected/const.js | 1 + .../unit/compress/expected/div-with-regexp.js | 1 + .../unit/compress/expected/empty-blocks.js | 1 + .../unit/compress/expected/forstatement.js | 1 + .../test/unit/compress/expected/if-not.js | 1 + .../test/unit/compress/expected/if.js | 1 + .../test/unit/compress/expected/ifreturn.js | 1 + .../test/unit/compress/expected/ifreturn2.js | 1 + .../test/unit/compress/expected/infinite.js | 1 + .../test/unit/compress/expected/issue10.js | 1 + .../test/unit/compress/expected/issue11.js | 1 + .../test/unit/compress/expected/issue13.js | 1 + .../test/unit/compress/expected/issue14.js | 1 + .../test/unit/compress/expected/issue16.js | 1 + .../test/unit/compress/expected/issue17.js | 1 + .../test/unit/compress/expected/issue20.js | 1 + .../test/unit/compress/expected/issue21.js | 1 + .../test/unit/compress/expected/issue25.js | 1 + .../test/unit/compress/expected/issue27.js | 1 + .../test/unit/compress/expected/issue278.js | 1 + .../test/unit/compress/expected/issue28.js | 1 + .../test/unit/compress/expected/issue29.js | 1 + .../test/unit/compress/expected/issue30.js | 1 + .../test/unit/compress/expected/issue34.js | 1 + .../test/unit/compress/expected/issue349.js | 1 + .../test/unit/compress/expected/issue353.js | 1 + .../test/unit/compress/expected/issue372.js | 1 + .../test/unit/compress/expected/issue4.js | 1 + .../test/unit/compress/expected/issue48.js | 1 + .../test/unit/compress/expected/issue50.js | 1 + .../test/unit/compress/expected/issue53.js | 1 + .../test/unit/compress/expected/issue54.1.js | 1 + .../test/unit/compress/expected/issue68.js | 1 + .../test/unit/compress/expected/issue69.js | 1 + .../test/unit/compress/expected/issue9.js | 1 + .../unit/compress/expected/issues222_324.js | 1 + .../test/unit/compress/expected/mangle.js | 1 + .../unit/compress/expected/null_string.js | 1 + .../unit/compress/expected/strict-equals.js | 1 + .../test/unit/compress/expected/var.js | 1 + .../test/unit/compress/expected/whitespace.js | 1 + .../test/unit/compress/expected/with.js | 1 + .../uglify-js/test/unit/compress/test/1e21.js | 1 + .../test/unit/compress/test/array1.js | 3 + .../test/unit/compress/test/array2.js | 4 + .../test/unit/compress/test/array3.js | 4 + .../test/unit/compress/test/array4.js | 6 + .../test/unit/compress/test/assignment.js | 20 + .../test/unit/compress/test/concatstring.js | 3 + .../test/unit/compress/test/const.js | 5 + .../unit/compress/test/div-with-regexp.js | 1 + .../test/unit/compress/test/empty-blocks.js | 4 + .../test/unit/compress/test/forstatement.js | 10 + .../test/unit/compress/test/if-not.js | 1 + .../uglify-js/test/unit/compress/test/if.js | 6 + .../test/unit/compress/test/ifreturn.js | 9 + .../test/unit/compress/test/ifreturn2.js | 16 + .../test/unit/compress/test/infinite.js | 1 + .../test/unit/compress/test/issue10.js | 1 + .../test/unit/compress/test/issue11.js | 3 + .../test/unit/compress/test/issue13.js | 1 + .../test/unit/compress/test/issue14.js | 1 + .../test/unit/compress/test/issue16.js | 1 + .../test/unit/compress/test/issue17.js | 4 + .../test/unit/compress/test/issue20.js | 1 + .../test/unit/compress/test/issue21.js | 6 + .../test/unit/compress/test/issue25.js | 7 + .../test/unit/compress/test/issue27.js | 1 + .../test/unit/compress/test/issue278.js | 1 + .../test/unit/compress/test/issue28.js | 3 + .../test/unit/compress/test/issue29.js | 1 + .../test/unit/compress/test/issue30.js | 3 + .../test/unit/compress/test/issue34.js | 3 + .../test/unit/compress/test/issue349.js | 25 + .../test/unit/compress/test/issue353.js | 4 + .../test/unit/compress/test/issue372.js | 32 + .../test/unit/compress/test/issue4.js | 3 + .../test/unit/compress/test/issue48.js | 1 + .../test/unit/compress/test/issue50.js | 9 + .../test/unit/compress/test/issue53.js | 1 + .../test/unit/compress/test/issue54.1.js | 3 + .../test/unit/compress/test/issue68.js | 5 + .../test/unit/compress/test/issue69.js | 1 + .../test/unit/compress/test/issue9.js | 4 + .../test/unit/compress/test/issues222_324.js | 82 + .../test/unit/compress/test/mangle.js | 5 + .../test/unit/compress/test/null_string.js | 1 + .../test/unit/compress/test/strict-equals.js | 3 + .../uglify-js/test/unit/compress/test/var.js | 3 + .../test/unit/compress/test/whitespace.js | 21 + .../uglify-js/test/unit/compress/test/with.js | 2 + node_modules/uglify-js/test/unit/scripts.js | 57 + node_modules/uglify-js/tmp/hoist.js | 33 + node_modules/uglify-js/tmp/instrument.js | 97 + node_modules/uglify-js/tmp/test.js | 30 + node_modules/uglify-js/uglify-js.js | 18 + processbuild.js | 20 + 123 files changed, 9319 insertions(+), 18 deletions(-) create mode 100644 LICENSE create mode 100644 modular-build-min.html create mode 120000 node_modules/.bin/uglifyjs create mode 100644 node_modules/uglify-js/.npmignore create mode 100644 node_modules/uglify-js/README.html create mode 100644 node_modules/uglify-js/README.org create mode 100755 node_modules/uglify-js/bin/uglifyjs create mode 100644 node_modules/uglify-js/docstyle.css create mode 100644 node_modules/uglify-js/lib/consolidator.js create mode 100644 node_modules/uglify-js/lib/parse-js.js create mode 100644 node_modules/uglify-js/lib/process.js create mode 100644 node_modules/uglify-js/lib/squeeze-more.js create mode 100644 node_modules/uglify-js/package.json create mode 100755 node_modules/uglify-js/test/beautify.js create mode 100755 node_modules/uglify-js/test/testconsolidator.js create mode 100755 node_modules/uglify-js/test/testparser.js create mode 100644 node_modules/uglify-js/test/unit/compress/expected/1e21.js create mode 100644 node_modules/uglify-js/test/unit/compress/expected/array1.js create mode 100644 node_modules/uglify-js/test/unit/compress/expected/array2.js create mode 100644 node_modules/uglify-js/test/unit/compress/expected/array3.js create mode 100644 node_modules/uglify-js/test/unit/compress/expected/array4.js create mode 100644 node_modules/uglify-js/test/unit/compress/expected/assignment.js create mode 100644 node_modules/uglify-js/test/unit/compress/expected/concatstring.js create mode 100644 node_modules/uglify-js/test/unit/compress/expected/const.js create mode 100644 node_modules/uglify-js/test/unit/compress/expected/div-with-regexp.js create mode 100644 node_modules/uglify-js/test/unit/compress/expected/empty-blocks.js create mode 100644 node_modules/uglify-js/test/unit/compress/expected/forstatement.js create mode 100644 node_modules/uglify-js/test/unit/compress/expected/if-not.js create mode 100644 node_modules/uglify-js/test/unit/compress/expected/if.js create mode 100644 node_modules/uglify-js/test/unit/compress/expected/ifreturn.js create mode 100644 node_modules/uglify-js/test/unit/compress/expected/ifreturn2.js create mode 100644 node_modules/uglify-js/test/unit/compress/expected/infinite.js create mode 100644 node_modules/uglify-js/test/unit/compress/expected/issue10.js create mode 100644 node_modules/uglify-js/test/unit/compress/expected/issue11.js create mode 100644 node_modules/uglify-js/test/unit/compress/expected/issue13.js create mode 100644 node_modules/uglify-js/test/unit/compress/expected/issue14.js create mode 100644 node_modules/uglify-js/test/unit/compress/expected/issue16.js create mode 100644 node_modules/uglify-js/test/unit/compress/expected/issue17.js create mode 100644 node_modules/uglify-js/test/unit/compress/expected/issue20.js create mode 100644 node_modules/uglify-js/test/unit/compress/expected/issue21.js create mode 100644 node_modules/uglify-js/test/unit/compress/expected/issue25.js create mode 100644 node_modules/uglify-js/test/unit/compress/expected/issue27.js create mode 100644 node_modules/uglify-js/test/unit/compress/expected/issue278.js create mode 100644 node_modules/uglify-js/test/unit/compress/expected/issue28.js create mode 100644 node_modules/uglify-js/test/unit/compress/expected/issue29.js create mode 100644 node_modules/uglify-js/test/unit/compress/expected/issue30.js create mode 100644 node_modules/uglify-js/test/unit/compress/expected/issue34.js create mode 100644 node_modules/uglify-js/test/unit/compress/expected/issue349.js create mode 100644 node_modules/uglify-js/test/unit/compress/expected/issue353.js create mode 100644 node_modules/uglify-js/test/unit/compress/expected/issue372.js create mode 100644 node_modules/uglify-js/test/unit/compress/expected/issue4.js create mode 100644 node_modules/uglify-js/test/unit/compress/expected/issue48.js create mode 100644 node_modules/uglify-js/test/unit/compress/expected/issue50.js create mode 100644 node_modules/uglify-js/test/unit/compress/expected/issue53.js create mode 100644 node_modules/uglify-js/test/unit/compress/expected/issue54.1.js create mode 100644 node_modules/uglify-js/test/unit/compress/expected/issue68.js create mode 100644 node_modules/uglify-js/test/unit/compress/expected/issue69.js create mode 100644 node_modules/uglify-js/test/unit/compress/expected/issue9.js create mode 100644 node_modules/uglify-js/test/unit/compress/expected/issues222_324.js create mode 100644 node_modules/uglify-js/test/unit/compress/expected/mangle.js create mode 100644 node_modules/uglify-js/test/unit/compress/expected/null_string.js create mode 100644 node_modules/uglify-js/test/unit/compress/expected/strict-equals.js create mode 100644 node_modules/uglify-js/test/unit/compress/expected/var.js create mode 100644 node_modules/uglify-js/test/unit/compress/expected/whitespace.js create mode 100644 node_modules/uglify-js/test/unit/compress/expected/with.js create mode 100644 node_modules/uglify-js/test/unit/compress/test/1e21.js create mode 100644 node_modules/uglify-js/test/unit/compress/test/array1.js create mode 100644 node_modules/uglify-js/test/unit/compress/test/array2.js create mode 100644 node_modules/uglify-js/test/unit/compress/test/array3.js create mode 100644 node_modules/uglify-js/test/unit/compress/test/array4.js create mode 100644 node_modules/uglify-js/test/unit/compress/test/assignment.js create mode 100644 node_modules/uglify-js/test/unit/compress/test/concatstring.js create mode 100644 node_modules/uglify-js/test/unit/compress/test/const.js create mode 100644 node_modules/uglify-js/test/unit/compress/test/div-with-regexp.js create mode 100644 node_modules/uglify-js/test/unit/compress/test/empty-blocks.js create mode 100644 node_modules/uglify-js/test/unit/compress/test/forstatement.js create mode 100644 node_modules/uglify-js/test/unit/compress/test/if-not.js create mode 100644 node_modules/uglify-js/test/unit/compress/test/if.js create mode 100644 node_modules/uglify-js/test/unit/compress/test/ifreturn.js create mode 100644 node_modules/uglify-js/test/unit/compress/test/ifreturn2.js create mode 100644 node_modules/uglify-js/test/unit/compress/test/infinite.js create mode 100644 node_modules/uglify-js/test/unit/compress/test/issue10.js create mode 100644 node_modules/uglify-js/test/unit/compress/test/issue11.js create mode 100644 node_modules/uglify-js/test/unit/compress/test/issue13.js create mode 100644 node_modules/uglify-js/test/unit/compress/test/issue14.js create mode 100644 node_modules/uglify-js/test/unit/compress/test/issue16.js create mode 100644 node_modules/uglify-js/test/unit/compress/test/issue17.js create mode 100644 node_modules/uglify-js/test/unit/compress/test/issue20.js create mode 100644 node_modules/uglify-js/test/unit/compress/test/issue21.js create mode 100644 node_modules/uglify-js/test/unit/compress/test/issue25.js create mode 100644 node_modules/uglify-js/test/unit/compress/test/issue27.js create mode 100644 node_modules/uglify-js/test/unit/compress/test/issue278.js create mode 100644 node_modules/uglify-js/test/unit/compress/test/issue28.js create mode 100644 node_modules/uglify-js/test/unit/compress/test/issue29.js create mode 100644 node_modules/uglify-js/test/unit/compress/test/issue30.js create mode 100644 node_modules/uglify-js/test/unit/compress/test/issue34.js create mode 100644 node_modules/uglify-js/test/unit/compress/test/issue349.js create mode 100644 node_modules/uglify-js/test/unit/compress/test/issue353.js create mode 100644 node_modules/uglify-js/test/unit/compress/test/issue372.js create mode 100644 node_modules/uglify-js/test/unit/compress/test/issue4.js create mode 100644 node_modules/uglify-js/test/unit/compress/test/issue48.js create mode 100644 node_modules/uglify-js/test/unit/compress/test/issue50.js create mode 100644 node_modules/uglify-js/test/unit/compress/test/issue53.js create mode 100644 node_modules/uglify-js/test/unit/compress/test/issue54.1.js create mode 100644 node_modules/uglify-js/test/unit/compress/test/issue68.js create mode 100644 node_modules/uglify-js/test/unit/compress/test/issue69.js create mode 100644 node_modules/uglify-js/test/unit/compress/test/issue9.js create mode 100644 node_modules/uglify-js/test/unit/compress/test/issues222_324.js create mode 100644 node_modules/uglify-js/test/unit/compress/test/mangle.js create mode 100644 node_modules/uglify-js/test/unit/compress/test/null_string.js create mode 100644 node_modules/uglify-js/test/unit/compress/test/strict-equals.js create mode 100644 node_modules/uglify-js/test/unit/compress/test/var.js create mode 100644 node_modules/uglify-js/test/unit/compress/test/whitespace.js create mode 100644 node_modules/uglify-js/test/unit/compress/test/with.js create mode 100644 node_modules/uglify-js/test/unit/scripts.js create mode 100644 node_modules/uglify-js/tmp/hoist.js create mode 100644 node_modules/uglify-js/tmp/instrument.js create mode 100755 node_modules/uglify-js/tmp/test.js create mode 100644 node_modules/uglify-js/uglify-js.js create mode 100644 processbuild.js diff --git a/LICENSE b/LICENSE new file mode 100644 index 00000000..b932df9b --- /dev/null +++ b/LICENSE @@ -0,0 +1 @@ +/* Modernizr 3.0.0pre (Custom Build) | MIT & BSD */ diff --git a/build.sh b/build.sh index 775289e3..b2982bb5 100755 --- a/build.sh +++ b/build.sh @@ -5,4 +5,9 @@ rm -rf dist mkdir dist cp build/src/modernizr-build.js dist/modernizr-build.js rm -rf build -node stripdefine.js +node processbuild.js +m=$(stat -f "%z" dist/modernizr-build.min.js) +gzip -nfc --best dist/modernizr-build.min.js > dist/modernizr-build.min.js.gz +g=$(stat -f "%z" dist/modernizr-build.min.js.gz) +rm -f dist/modernizr-build.min.js.gz +echo "$m bytes minified, $g bytes gzipped" diff --git a/modular-build-min.html b/modular-build-min.html new file mode 100644 index 00000000..0cc6e642 --- /dev/null +++ b/modular-build-min.html @@ -0,0 +1,11 @@ + + + + Modular Modernizr Demo Page + + +Console, yo. + + + + diff --git a/modular-build.html b/modular-build.html index d825d210..23504c7a 100644 --- a/modular-build.html +++ b/modular-build.html @@ -7,19 +7,5 @@ Console, yo. - diff --git a/modular.html b/modular.html index b2824f3c..3d76ac64 100644 --- a/modular.html +++ b/modular.html @@ -15,8 +15,5 @@ Console, yo. }); - diff --git a/node_modules/.bin/uglifyjs b/node_modules/.bin/uglifyjs new file mode 120000 index 00000000..fef3468b --- /dev/null +++ b/node_modules/.bin/uglifyjs @@ -0,0 +1 @@ +../uglify-js/bin/uglifyjs \ No newline at end of file diff --git a/node_modules/uglify-js/.npmignore b/node_modules/uglify-js/.npmignore new file mode 100644 index 00000000..83c3dac1 --- /dev/null +++ b/node_modules/uglify-js/.npmignore @@ -0,0 +1,6 @@ +.DS_Store +.tmp*~ +*.local.* +.pinf-* +node_modules/ +npm-debug.log diff --git a/node_modules/uglify-js/README.html b/node_modules/uglify-js/README.html new file mode 100644 index 00000000..abefdf11 --- /dev/null +++ b/node_modules/uglify-js/README.html @@ -0,0 +1,1012 @@ + + + + +UglifyJS – a JavaScript parser/compressor/beautifier + + + + + + + + + + + + + +
+ +
+ +
+

UglifyJS – a JavaScript parser/compressor/beautifier

+ + + + +
+

1 NEW: UglifyJS2

+
+ + +

+I started working on UglifyJS's successor, version 2. It's almost a full +rewrite (except for the parser which is heavily modified, everything else +starts from scratch). I've detailed my reasons in the README, see the +project page. +

+

+https://github.com/mishoo/UglifyJS2 +

+

+Version 1 will continue to be maintained for fixing show-stopper bugs, but +no new features should be expected. +

+

+Please help me focus on version 2 by making a donation! +

+
+ +
+ +
+

2 UglifyJS — a JavaScript parser/compressor/beautifier

+
+ + +

+This package implements a general-purpose JavaScript +parser/compressor/beautifier toolkit. It is developed on NodeJS, but it +should work on any JavaScript platform supporting the CommonJS module system +(and if your platform of choice doesn't support CommonJS, you can easily +implement it, or discard the exports.* lines from UglifyJS sources). +

+

+The tokenizer/parser generates an abstract syntax tree from JS code. You +can then traverse the AST to learn more about the code, or do various +manipulations on it. This part is implemented in parse-js.js and it's a +port to JavaScript of the excellent parse-js Common Lisp library from Marijn Haverbeke. +

+

+( See cl-uglify-js if you're looking for the Common Lisp version of +UglifyJS. ) +

+

+The second part of this package, implemented in process.js, inspects and +manipulates the AST generated by the parser to provide the following: +

+
    +
  • ability to re-generate JavaScript code from the AST. Optionally + indented—you can use this if you want to “beautify” a program that has + been compressed, so that you can inspect the source. But you can also run + our code generator to print out an AST without any whitespace, so you + achieve compression as well. + +
  • +
  • shorten variable names (usually to single characters). Our mangler will + analyze the code and generate proper variable names, depending on scope + and usage, and is smart enough to deal with globals defined elsewhere, or + with eval() calls or with{} statements. In short, if eval() or + with{} are used in some scope, then all variables in that scope and any + variables in the parent scopes will remain unmangled, and any references + to such variables remain unmangled as well. + +
  • +
  • various small optimizations that may lead to faster code but certainly + lead to smaller code. Where possible, we do the following: + +
      +
    • foo["bar"] ==> foo.bar + +
    • +
    • remove block brackets {} + +
    • +
    • join consecutive var declarations: + var a = 10; var b = 20; ==> var a=10,b=20; + +
    • +
    • resolve simple constant expressions: 1 +2 * 3 ==> 7. We only do the + replacement if the result occupies less bytes; for example 1/3 would + translate to 0.333333333333, so in this case we don't replace it. + +
    • +
    • consecutive statements in blocks are merged into a sequence; in many + cases, this leaves blocks with a single statement, so then we can remove + the block brackets. + +
    • +
    • various optimizations for IF statements: + +
        +
      • if (foo) bar(); else baz(); ==> foo?bar():baz(); +
      • +
      • if (!foo) bar(); else baz(); ==> foo?baz():bar(); +
      • +
      • if (foo) bar(); ==> foo&&bar(); +
      • +
      • if (!foo) bar(); ==> foo||bar(); +
      • +
      • if (foo) return bar(); else return baz(); ==> return foo?bar():baz(); +
      • +
      • if (foo) return bar(); else something(); ==> {if(foo)return bar();something()} + +
      • +
      + +
    • +
    • remove some unreachable code and warn about it (code that follows a + return, throw, break or continue statement, except + function/variable declarations). + +
    • +
    • act a limited version of a pre-processor (c.f. the pre-processor of + C/C++) to allow you to safely replace selected global symbols with + specified values. When combined with the optimisations above this can + make UglifyJS operate slightly more like a compilation process, in + that when certain symbols are replaced by constant values, entire code + blocks may be optimised away as unreachable. +
    • +
    + +
  • +
+ + + +
+ +
+

2.1 Unsafe transformations

+
+ + +

+The following transformations can in theory break code, although they're +probably safe in most practical cases. To enable them you need to pass the +--unsafe flag. +

+ +
+ +
+

2.1.1 Calls involving the global Array constructor

+
+ + +

+The following transformations occur: +

+ + + +
new Array(1, 2, 3, 4)  => [1,2,3,4]
+Array(a, b, c)         => [a,b,c]
+new Array(5)           => Array(5)
+new Array(a)           => Array(a)
+
+ + +

+These are all safe if the Array name isn't redefined. JavaScript does allow +one to globally redefine Array (and pretty much everything, in fact) but I +personally don't see why would anyone do that. +

+

+UglifyJS does handle the case where Array is redefined locally, or even +globally but with a function or var declaration. Therefore, in the +following cases UglifyJS doesn't touch calls or instantiations of Array: +

+ + + +
// case 1.  globally declared variable
+  var Array;
+  new Array(1, 2, 3);
+  Array(a, b);
+
+  // or (can be declared later)
+  new Array(1, 2, 3);
+  var Array;
+
+  // or (can be a function)
+  new Array(1, 2, 3);
+  function Array() { ... }
+
+// case 2.  declared in a function
+  (function(){
+    a = new Array(1, 2, 3);
+    b = Array(5, 6);
+    var Array;
+  })();
+
+  // or
+  (function(Array){
+    return Array(5, 6, 7);
+  })();
+
+  // or
+  (function(){
+    return new Array(1, 2, 3, 4);
+    function Array() { ... }
+  })();
+
+  // etc.
+
+ + +
+ +
+ +
+

2.1.2 obj.toString() ==> obj+“”

+
+ + +
+
+ +
+ +
+

2.2 Install (NPM)

+
+ + +

+UglifyJS is now available through NPM — npm install uglify-js should do +the job. +

+
+ +
+ +
+

2.3 Install latest code from GitHub

+
+ + + + + +
## clone the repository
+mkdir -p /where/you/wanna/put/it
+cd /where/you/wanna/put/it
+git clone git://github.com/mishoo/UglifyJS.git
+
+## make the module available to Node
+mkdir -p ~/.node_libraries/
+cd ~/.node_libraries/
+ln -s /where/you/wanna/put/it/UglifyJS/uglify-js.js
+
+## and if you want the CLI script too:
+mkdir -p ~/bin
+cd ~/bin
+ln -s /where/you/wanna/put/it/UglifyJS/bin/uglifyjs
+  # (then add ~/bin to your $PATH if it's not there already)
+
+ + +
+ +
+ +
+

2.4 Usage

+
+ + +

+There is a command-line tool that exposes the functionality of this library +for your shell-scripting needs: +

+ + + +
uglifyjs [ options... ] [ filename ]
+
+ + +

+filename should be the last argument and should name the file from which +to read the JavaScript code. If you don't specify it, it will read code +from STDIN. +

+

+Supported options: +

+
    +
  • -b or --beautify — output indented code; when passed, additional + options control the beautifier: + +
      +
    • -i N or --indent N — indentation level (number of spaces) + +
    • +
    • -q or --quote-keys — quote keys in literal objects (by default, + only keys that cannot be identifier names will be quotes). + +
    • +
    + +
  • +
  • -c or ----consolidate-primitive-values — consolidates null, Boolean, + and String values. Known as aliasing in the Closure Compiler. Worsens the + data compression ratio of gzip. + +
  • +
  • --ascii — pass this argument to encode non-ASCII characters as + \uXXXX sequences. By default UglifyJS won't bother to do it and will + output Unicode characters instead. (the output is always encoded in UTF8, + but if you pass this option you'll only get ASCII). + +
  • +
  • -nm or --no-mangle — don't mangle names. + +
  • +
  • -nmf or --no-mangle-functions – in case you want to mangle variable + names, but not touch function names. + +
  • +
  • -ns or --no-squeeze — don't call ast_squeeze() (which does various + optimizations that result in smaller, less readable code). + +
  • +
  • -mt or --mangle-toplevel — mangle names in the toplevel scope too + (by default we don't do this). + +
  • +
  • --no-seqs — when ast_squeeze() is called (thus, unless you pass + --no-squeeze) it will reduce consecutive statements in blocks into a + sequence. For example, "a = 10; b = 20; foo();" will be written as + "a=10,b=20,foo();". In various occasions, this allows us to discard the + block brackets (since the block becomes a single statement). This is ON + by default because it seems safe and saves a few hundred bytes on some + libs that I tested it on, but pass --no-seqs to disable it. + +
  • +
  • --no-dead-code — by default, UglifyJS will remove code that is + obviously unreachable (code that follows a return, throw, break or + continue statement and is not a function/variable declaration). Pass + this option to disable this optimization. + +
  • +
  • -nc or --no-copyright — by default, uglifyjs will keep the initial + comment tokens in the generated code (assumed to be copyright information + etc.). If you pass this it will discard it. + +
  • +
  • -o filename or --output filename — put the result in filename. If + this isn't given, the result goes to standard output (or see next one). + +
  • +
  • --overwrite — if the code is read from a file (not from STDIN) and you + pass --overwrite then the output will be written in the same file. + +
  • +
  • --ast — pass this if you want to get the Abstract Syntax Tree instead + of JavaScript as output. Useful for debugging or learning more about the + internals. + +
  • +
  • -v or --verbose — output some notes on STDERR (for now just how long + each operation takes). + +
  • +
  • -d SYMBOL[=VALUE] or --define SYMBOL[=VALUE] — will replace + all instances of the specified symbol where used as an identifier + (except where symbol has properly declared by a var declaration or + use as function parameter or similar) with the specified value. This + argument may be specified multiple times to define multiple + symbols - if no value is specified the symbol will be replaced with + the value true, or you can specify a numeric value (such as + 1024), a quoted string value (such as ="object"= or + ='https://github.com'), or the name of another symbol or keyword (such as =null or document). + This allows you, for example, to assign meaningful names to key + constant values but discard the symbolic names in the uglified + version for brevity/efficiency, or when used wth care, allows + UglifyJS to operate as a form of conditional compilation + whereby defining appropriate values may, by dint of the constant + folding and dead code removal features above, remove entire + superfluous code blocks (e.g. completely remove instrumentation or + trace code for production use). + Where string values are being defined, the handling of quotes are + likely to be subject to the specifics of your command shell + environment, so you may need to experiment with quoting styles + depending on your platform, or you may find the option + --define-from-module more suitable for use. + +
  • +
  • -define-from-module SOMEMODULE — will load the named module (as + per the NodeJS require() function) and iterate all the exported + properties of the module defining them as symbol names to be defined + (as if by the --define option) per the name of each property + (i.e. without the module name prefix) and given the value of the + property. This is a much easier way to handle and document groups of + symbols to be defined rather than a large number of --define + options. + +
  • +
  • --unsafe — enable other additional optimizations that are known to be + unsafe in some contrived situations, but could still be generally useful. + For now only these: + +
      +
    • foo.toString() ==> foo+"" +
    • +
    • new Array(x,…) ==> [x,…] +
    • +
    • new Array(x) ==> Array(x) + +
    • +
    + +
  • +
  • --max-line-len (default 32K characters) — add a newline after around + 32K characters. I've seen both FF and Chrome croak when all the code was + on a single line of around 670K. Pass –max-line-len 0 to disable this + safety feature. + +
  • +
  • --reserved-names — some libraries rely on certain names to be used, as + pointed out in issue #92 and #81, so this option allow you to exclude such + names from the mangler. For example, to keep names require and $super + intact you'd specify –reserved-names "require,$super". + +
  • +
  • --inline-script – when you want to include the output literally in an + HTML <script> tag you can use this option to prevent </script from + showing up in the output. + +
  • +
  • --lift-vars – when you pass this, UglifyJS will apply the following + transformations (see the notes in API, ast_lift_variables): + +
      +
    • put all var declarations at the start of the scope +
    • +
    • make sure a variable is declared only once +
    • +
    • discard unused function arguments +
    • +
    • discard unused inner (named) functions +
    • +
    • finally, try to merge assignments into that one var declaration, if + possible. +
    • +
    + +
  • +
+ + + +
+ +
+

2.4.1 API

+
+ + +

+To use the library from JavaScript, you'd do the following (example for +NodeJS): +

+ + + +
var jsp = require("uglify-js").parser;
+var pro = require("uglify-js").uglify;
+
+var orig_code = "... JS code here";
+var ast = jsp.parse(orig_code); // parse code and get the initial AST
+ast = pro.ast_mangle(ast); // get a new AST with mangled names
+ast = pro.ast_squeeze(ast); // get an AST with compression optimizations
+var final_code = pro.gen_code(ast); // compressed code here
+
+ + +

+The above performs the full compression that is possible right now. As you +can see, there are a sequence of steps which you can apply. For example if +you want compressed output but for some reason you don't want to mangle +variable names, you would simply skip the line that calls +pro.ast_mangle(ast). +

+

+Some of these functions take optional arguments. Here's a description: +

+
    +
  • jsp.parse(code, strict_semicolons) – parses JS code and returns an AST. + strict_semicolons is optional and defaults to false. If you pass + true then the parser will throw an error when it expects a semicolon and + it doesn't find it. For most JS code you don't want that, but it's useful + if you want to strictly sanitize your code. + +
  • +
  • pro.ast_lift_variables(ast) – merge and move var declarations to the + scop of the scope; discard unused function arguments or variables; discard + unused (named) inner functions. It also tries to merge assignments + following the var declaration into it. + +

    + If your code is very hand-optimized concerning var declarations, this + lifting variable declarations might actually increase size. For me it + helps out. On jQuery it adds 865 bytes (243 after gzip). YMMV. Also + note that (since it's not enabled by default) this operation isn't yet + heavily tested (please report if you find issues!). +

    +

    + Note that although it might increase the image size (on jQuery it gains + 865 bytes, 243 after gzip) it's technically more correct: in certain + situations, dead code removal might drop variable declarations, which + would not happen if the variables are lifted in advance. +

    +

    + Here's an example of what it does: +

  • +
+ + + + + +
function f(a, b, c, d, e) {
+    var q;
+    var w;
+    w = 10;
+    q = 20;
+    for (var i = 1; i < 10; ++i) {
+        var boo = foo(a);
+    }
+    for (var i = 0; i < 1; ++i) {
+        var boo = bar(c);
+    }
+    function foo(){ ... }
+    function bar(){ ... }
+    function baz(){ ... }
+}
+
+// transforms into ==>
+
+function f(a, b, c) {
+    var i, boo, w = 10, q = 20;
+    for (i = 1; i < 10; ++i) {
+        boo = foo(a);
+    }
+    for (i = 0; i < 1; ++i) {
+        boo = bar(c);
+    }
+    function foo() { ... }
+    function bar() { ... }
+}
+
+ + +
    +
  • pro.ast_mangle(ast, options) – generates a new AST containing mangled + (compressed) variable and function names. It supports the following + options: + +
      +
    • toplevel – mangle toplevel names (by default we don't touch them). +
    • +
    • except – an array of names to exclude from compression. +
    • +
    • defines – an object with properties named after symbols to + replace (see the --define option for the script) and the values + representing the AST replacement value. + +
    • +
    + +
  • +
  • pro.ast_squeeze(ast, options) – employs further optimizations designed + to reduce the size of the code that gen_code would generate from the + AST. Returns a new AST. options can be a hash; the supported options + are: + +
      +
    • make_seqs (default true) which will cause consecutive statements in a + block to be merged using the "sequence" (comma) operator + +
    • +
    • dead_code (default true) which will remove unreachable code. + +
    • +
    + +
  • +
  • pro.gen_code(ast, options) – generates JS code from the AST. By + default it's minified, but using the options argument you can get nicely + formatted output. options is, well, optional :-) and if you pass it it + must be an object and supports the following properties (below you can see + the default values): + +
      +
    • beautify: false – pass true if you want indented output +
    • +
    • indent_start: 0 (only applies when beautify is true) – initial + indentation in spaces +
    • +
    • indent_level: 4 (only applies when beautify is true) -- + indentation level, in spaces (pass an even number) +
    • +
    • quote_keys: false – if you pass true it will quote all keys in + literal objects +
    • +
    • space_colon: false (only applies when beautify is true) – wether + to put a space before the colon in object literals +
    • +
    • ascii_only: false – pass true if you want to encode non-ASCII + characters as \uXXXX. +
    • +
    • inline_script: false – pass true to escape occurrences of + </script in strings +
    • +
    + +
  • +
+ + +
+ +
+ +
+

2.4.2 Beautifier shortcoming – no more comments

+
+ + +

+The beautifier can be used as a general purpose indentation tool. It's +useful when you want to make a minified file readable. One limitation, +though, is that it discards all comments, so you don't really want to use it +to reformat your code, unless you don't have, or don't care about, comments. +

+

+In fact it's not the beautifier who discards comments — they are dumped at +the parsing stage, when we build the initial AST. Comments don't really +make sense in the AST, and while we could add nodes for them, it would be +inconvenient because we'd have to add special rules to ignore them at all +the processing stages. +

+
+ +
+ +
+

2.4.3 Use as a code pre-processor

+
+ + +

+The --define option can be used, particularly when combined with the +constant folding logic, as a form of pre-processor to enable or remove +particular constructions, such as might be used for instrumenting +development code, or to produce variations aimed at a specific +platform. +

+

+The code below illustrates the way this can be done, and how the +symbol replacement is performed. +

+ + + +
CLAUSE1: if (typeof DEVMODE === 'undefined') {
+    DEVMODE = true;
+}
+
+CLAUSE2: function init() {
+    if (DEVMODE) {
+        console.log("init() called");
+    }
+    ....
+    DEVMODE &amp;&amp; console.log("init() complete");
+}
+
+CLAUSE3: function reportDeviceStatus(device) {
+    var DEVMODE = device.mode, DEVNAME = device.name;
+    if (DEVMODE === 'open') {
+        ....
+    }
+}
+
+ + +

+When the above code is normally executed, the undeclared global +variable DEVMODE will be assigned the value true (see CLAUSE1) +and so the init() function (CLAUSE2) will write messages to the +console log when executed, but in CLAUSE3 a locally declared +variable will mask access to the DEVMODE global symbol. +

+

+If the above code is processed by UglifyJS with an argument of +--define DEVMODE=false then UglifyJS will replace DEVMODE with the +boolean constant value false within CLAUSE1 and CLAUSE2, but it +will leave CLAUSE3 as it stands because there DEVMODE resolves to +a validly declared variable. +

+

+And more so, the constant-folding features of UglifyJS will recognise +that the if condition of CLAUSE1 is thus always false, and so will +remove the test and body of CLAUSE1 altogether (including the +otherwise slightly problematical statement false = true; which it +will have formed by replacing DEVMODE in the body). Similarly, +within CLAUSE2 both calls to console.log() will be removed +altogether. +

+

+In this way you can mimic, to a limited degree, the functionality of +the C/C++ pre-processor to enable or completely remove blocks +depending on how certain symbols are defined - perhaps using UglifyJS +to generate different versions of source aimed at different +environments +

+

+It is recommmended (but not made mandatory) that symbols designed for +this purpose are given names consisting of UPPER_CASE_LETTERS to +distinguish them from other (normal) symbols and avoid the sort of +clash that CLAUSE3 above illustrates. +

+
+
+ +
+ +
+

2.5 Compression – how good is it?

+
+ + +

+Here are updated statistics. (I also updated my Google Closure and YUI +installations). +

+

+We're still a lot better than YUI in terms of compression, though slightly +slower. We're still a lot faster than Closure, and compression after gzip +is comparable. +

+ + ++ + + + + + + + + + +
FileUglifyJSUglifyJS+gzipClosureClosure+gzipYUIYUI+gzip
jquery-1.6.2.js91001 (0:01.59)3189690678 (0:07.40)31979101527 (0:01.82)34646
paper.js142023 (0:01.65)43334134301 (0:07.42)42495173383 (0:01.58)48785
prototype.js88544 (0:01.09)2668086955 (0:06.97)2632692130 (0:00.79)28624
thelib-full.js (DynarchLIB)251939 (0:02.55)72535249911 (0:09.05)72696258869 (0:01.94)76584
+ + +
+ +
+ +
+

2.6 Bugs?

+
+ + +

+Unfortunately, for the time being there is no automated test suite. But I +ran the compressor manually on non-trivial code, and then I tested that the +generated code works as expected. A few hundred times. +

+

+DynarchLIB was started in times when there was no good JS minifier. +Therefore I was quite religious about trying to write short code manually, +and as such DL contains a lot of syntactic hacks1 such as “foo == bar ? a += 10 : b = 20”, though the more readable version would clearly be to use +“if/else”. +

+

+Since the parser/compressor runs fine on DL and jQuery, I'm quite confident +that it's solid enough for production use. If you can identify any bugs, +I'd love to hear about them (use the Google Group or email me directly). +

+
+ +
+ +
+

2.7 Links

+
+ + + + + +
+ +
+ +
+

2.8 License

+
+ + +

+UglifyJS is released under the BSD license: +

+ + + +
Copyright 2010 (c) Mihai Bazon <mihai.bazon@gmail.com>
+Based on parse-js (http://marijn.haverbeke.nl/parse-js/).
+
+Redistribution and use in source and binary forms, with or without
+modification, are permitted provided that the following conditions
+are met:
+
+    * Redistributions of source code must retain the above
+      copyright notice, this list of conditions and the following
+      disclaimer.
+
+    * Redistributions in binary form must reproduce the above
+      copyright notice, this list of conditions and the following
+      disclaimer in the documentation and/or other materials
+      provided with the distribution.
+
+THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDER “AS IS” AND ANY
+EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
+IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
+PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER BE
+LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY,
+OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
+PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
+PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
+THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR
+TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF
+THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
+SUCH DAMAGE.
+
+ + +
+

Footnotes:

+
+

1 I even reported a few bugs and suggested some fixes in the original + parse-js library, and Marijn pushed fixes literally in minutes. +

+
+
+ +
+
+
+ +
+

Date: 2012-08-27 12:38:12 EEST

+

Author: Mihai Bazon

+

Org version 7.7 with Emacs version 23

+Validate XHTML 1.0 + +
+ + diff --git a/node_modules/uglify-js/README.org b/node_modules/uglify-js/README.org new file mode 100644 index 00000000..25880884 --- /dev/null +++ b/node_modules/uglify-js/README.org @@ -0,0 +1,593 @@ +#+TITLE: UglifyJS -- a JavaScript parser/compressor/beautifier +#+KEYWORDS: javascript, js, parser, compiler, compressor, mangle, minify, minifier +#+DESCRIPTION: a JavaScript parser/compressor/beautifier in JavaScript +#+STYLE: +#+AUTHOR: Mihai Bazon +#+EMAIL: mihai.bazon@gmail.com + +* NEW: UglifyJS2 + +I started working on UglifyJS's successor, version 2. It's almost a full +rewrite (except for the parser which is heavily modified, everything else +starts from scratch). I've detailed my reasons in the README, see the +project page. + +[[https://github.com/mishoo/UglifyJS2][https://github.com/mishoo/UglifyJS2]] + +Version 1 will continue to be maintained for fixing show-stopper bugs, but +no new features should be expected. + +Please help me focus on version 2 by [[http://pledgie.com/campaigns/18110][making a donation]]! + +* UglifyJS --- a JavaScript parser/compressor/beautifier + +This package implements a general-purpose JavaScript +parser/compressor/beautifier toolkit. It is developed on [[http://nodejs.org/][NodeJS]], but it +should work on any JavaScript platform supporting the CommonJS module system +(and if your platform of choice doesn't support CommonJS, you can easily +implement it, or discard the =exports.*= lines from UglifyJS sources). + +The tokenizer/parser generates an abstract syntax tree from JS code. You +can then traverse the AST to learn more about the code, or do various +manipulations on it. This part is implemented in [[../lib/parse-js.js][parse-js.js]] and it's a +port to JavaScript of the excellent [[http://marijn.haverbeke.nl/parse-js/][parse-js]] Common Lisp library from [[http://marijn.haverbeke.nl/][Marijn +Haverbeke]]. + +( See [[http://github.com/mishoo/cl-uglify-js][cl-uglify-js]] if you're looking for the Common Lisp version of +UglifyJS. ) + +The second part of this package, implemented in [[../lib/process.js][process.js]], inspects and +manipulates the AST generated by the parser to provide the following: + +- ability to re-generate JavaScript code from the AST. Optionally + indented---you can use this if you want to “beautify” a program that has + been compressed, so that you can inspect the source. But you can also run + our code generator to print out an AST without any whitespace, so you + achieve compression as well. + +- shorten variable names (usually to single characters). Our mangler will + analyze the code and generate proper variable names, depending on scope + and usage, and is smart enough to deal with globals defined elsewhere, or + with =eval()= calls or =with{}= statements. In short, if =eval()= or + =with{}= are used in some scope, then all variables in that scope and any + variables in the parent scopes will remain unmangled, and any references + to such variables remain unmangled as well. + +- various small optimizations that may lead to faster code but certainly + lead to smaller code. Where possible, we do the following: + + - foo["bar"] ==> foo.bar + + - remove block brackets ={}= + + - join consecutive var declarations: + var a = 10; var b = 20; ==> var a=10,b=20; + + - resolve simple constant expressions: 1 +2 * 3 ==> 7. We only do the + replacement if the result occupies less bytes; for example 1/3 would + translate to 0.333333333333, so in this case we don't replace it. + + - consecutive statements in blocks are merged into a sequence; in many + cases, this leaves blocks with a single statement, so then we can remove + the block brackets. + + - various optimizations for IF statements: + + - if (foo) bar(); else baz(); ==> foo?bar():baz(); + - if (!foo) bar(); else baz(); ==> foo?baz():bar(); + - if (foo) bar(); ==> foo&&bar(); + - if (!foo) bar(); ==> foo||bar(); + - if (foo) return bar(); else return baz(); ==> return foo?bar():baz(); + - if (foo) return bar(); else something(); ==> {if(foo)return bar();something()} + + - remove some unreachable code and warn about it (code that follows a + =return=, =throw=, =break= or =continue= statement, except + function/variable declarations). + + - act a limited version of a pre-processor (c.f. the pre-processor of + C/C++) to allow you to safely replace selected global symbols with + specified values. When combined with the optimisations above this can + make UglifyJS operate slightly more like a compilation process, in + that when certain symbols are replaced by constant values, entire code + blocks may be optimised away as unreachable. + +** <> + +The following transformations can in theory break code, although they're +probably safe in most practical cases. To enable them you need to pass the +=--unsafe= flag. + +*** Calls involving the global Array constructor + +The following transformations occur: + +#+BEGIN_SRC js +new Array(1, 2, 3, 4) => [1,2,3,4] +Array(a, b, c) => [a,b,c] +new Array(5) => Array(5) +new Array(a) => Array(a) +#+END_SRC + +These are all safe if the Array name isn't redefined. JavaScript does allow +one to globally redefine Array (and pretty much everything, in fact) but I +personally don't see why would anyone do that. + +UglifyJS does handle the case where Array is redefined locally, or even +globally but with a =function= or =var= declaration. Therefore, in the +following cases UglifyJS *doesn't touch* calls or instantiations of Array: + +#+BEGIN_SRC js +// case 1. globally declared variable + var Array; + new Array(1, 2, 3); + Array(a, b); + + // or (can be declared later) + new Array(1, 2, 3); + var Array; + + // or (can be a function) + new Array(1, 2, 3); + function Array() { ... } + +// case 2. declared in a function + (function(){ + a = new Array(1, 2, 3); + b = Array(5, 6); + var Array; + })(); + + // or + (function(Array){ + return Array(5, 6, 7); + })(); + + // or + (function(){ + return new Array(1, 2, 3, 4); + function Array() { ... } + })(); + + // etc. +#+END_SRC + +*** =obj.toString()= ==> =obj+“”= + +** Install (NPM) + +UglifyJS is now available through NPM --- =npm install uglify-js= should do +the job. + +** Install latest code from GitHub + +#+BEGIN_SRC sh +## clone the repository +mkdir -p /where/you/wanna/put/it +cd /where/you/wanna/put/it +git clone git://github.com/mishoo/UglifyJS.git + +## make the module available to Node +mkdir -p ~/.node_libraries/ +cd ~/.node_libraries/ +ln -s /where/you/wanna/put/it/UglifyJS/uglify-js.js + +## and if you want the CLI script too: +mkdir -p ~/bin +cd ~/bin +ln -s /where/you/wanna/put/it/UglifyJS/bin/uglifyjs + # (then add ~/bin to your $PATH if it's not there already) +#+END_SRC + +** Usage + +There is a command-line tool that exposes the functionality of this library +for your shell-scripting needs: + +#+BEGIN_SRC sh +uglifyjs [ options... ] [ filename ] +#+END_SRC + +=filename= should be the last argument and should name the file from which +to read the JavaScript code. If you don't specify it, it will read code +from STDIN. + +Supported options: + +- =-b= or =--beautify= --- output indented code; when passed, additional + options control the beautifier: + + - =-i N= or =--indent N= --- indentation level (number of spaces) + + - =-q= or =--quote-keys= --- quote keys in literal objects (by default, + only keys that cannot be identifier names will be quotes). + +- =-c= or =----consolidate-primitive-values= --- consolidates null, Boolean, + and String values. Known as aliasing in the Closure Compiler. Worsens the + data compression ratio of gzip. + +- =--ascii= --- pass this argument to encode non-ASCII characters as + =\uXXXX= sequences. By default UglifyJS won't bother to do it and will + output Unicode characters instead. (the output is always encoded in UTF8, + but if you pass this option you'll only get ASCII). + +- =-nm= or =--no-mangle= --- don't mangle names. + +- =-nmf= or =--no-mangle-functions= -- in case you want to mangle variable + names, but not touch function names. + +- =-ns= or =--no-squeeze= --- don't call =ast_squeeze()= (which does various + optimizations that result in smaller, less readable code). + +- =-mt= or =--mangle-toplevel= --- mangle names in the toplevel scope too + (by default we don't do this). + +- =--no-seqs= --- when =ast_squeeze()= is called (thus, unless you pass + =--no-squeeze=) it will reduce consecutive statements in blocks into a + sequence. For example, "a = 10; b = 20; foo();" will be written as + "a=10,b=20,foo();". In various occasions, this allows us to discard the + block brackets (since the block becomes a single statement). This is ON + by default because it seems safe and saves a few hundred bytes on some + libs that I tested it on, but pass =--no-seqs= to disable it. + +- =--no-dead-code= --- by default, UglifyJS will remove code that is + obviously unreachable (code that follows a =return=, =throw=, =break= or + =continue= statement and is not a function/variable declaration). Pass + this option to disable this optimization. + +- =-nc= or =--no-copyright= --- by default, =uglifyjs= will keep the initial + comment tokens in the generated code (assumed to be copyright information + etc.). If you pass this it will discard it. + +- =-o filename= or =--output filename= --- put the result in =filename=. If + this isn't given, the result goes to standard output (or see next one). + +- =--overwrite= --- if the code is read from a file (not from STDIN) and you + pass =--overwrite= then the output will be written in the same file. + +- =--ast= --- pass this if you want to get the Abstract Syntax Tree instead + of JavaScript as output. Useful for debugging or learning more about the + internals. + +- =-v= or =--verbose= --- output some notes on STDERR (for now just how long + each operation takes). + +- =-d SYMBOL[=VALUE]= or =--define SYMBOL[=VALUE]= --- will replace + all instances of the specified symbol where used as an identifier + (except where symbol has properly declared by a var declaration or + use as function parameter or similar) with the specified value. This + argument may be specified multiple times to define multiple + symbols - if no value is specified the symbol will be replaced with + the value =true=, or you can specify a numeric value (such as + =1024=), a quoted string value (such as ="object"= or + ='https://github.com'=), or the name of another symbol or keyword + (such as =null= or =document=). + This allows you, for example, to assign meaningful names to key + constant values but discard the symbolic names in the uglified + version for brevity/efficiency, or when used wth care, allows + UglifyJS to operate as a form of *conditional compilation* + whereby defining appropriate values may, by dint of the constant + folding and dead code removal features above, remove entire + superfluous code blocks (e.g. completely remove instrumentation or + trace code for production use). + Where string values are being defined, the handling of quotes are + likely to be subject to the specifics of your command shell + environment, so you may need to experiment with quoting styles + depending on your platform, or you may find the option + =--define-from-module= more suitable for use. + +- =-define-from-module SOMEMODULE= --- will load the named module (as + per the NodeJS =require()= function) and iterate all the exported + properties of the module defining them as symbol names to be defined + (as if by the =--define= option) per the name of each property + (i.e. without the module name prefix) and given the value of the + property. This is a much easier way to handle and document groups of + symbols to be defined rather than a large number of =--define= + options. + +- =--unsafe= --- enable other additional optimizations that are known to be + unsafe in some contrived situations, but could still be generally useful. + For now only these: + + - foo.toString() ==> foo+"" + - new Array(x,...) ==> [x,...] + - new Array(x) ==> Array(x) + +- =--max-line-len= (default 32K characters) --- add a newline after around + 32K characters. I've seen both FF and Chrome croak when all the code was + on a single line of around 670K. Pass --max-line-len 0 to disable this + safety feature. + +- =--reserved-names= --- some libraries rely on certain names to be used, as + pointed out in issue #92 and #81, so this option allow you to exclude such + names from the mangler. For example, to keep names =require= and =$super= + intact you'd specify --reserved-names "require,$super". + +- =--inline-script= -- when you want to include the output literally in an + HTML =\n\n\n\n\n
\n\n
\n\n
\n

UglifyJS – a JavaScript parser/compressor/beautifier

\n\n\n\n\n
\n

1 NEW: UglifyJS2

\n
\n\n\n

\nI started working on UglifyJS's successor, version 2. It's almost a full\nrewrite (except for the parser which is heavily modified, everything else\nstarts from scratch). I've detailed my reasons in the README, see the\nproject page.\n

\n

\nhttps://github.com/mishoo/UglifyJS2\n

\n

\nVersion 1 will continue to be maintained for fixing show-stopper bugs, but\nno new features should be expected.\n

\n

\nPlease help me focus on version 2 by making a donation!\n

\n
\n\n
\n\n
\n

2 UglifyJS — a JavaScript parser/compressor/beautifier

\n
\n\n\n

\nThis package implements a general-purpose JavaScript\nparser/compressor/beautifier toolkit. It is developed on NodeJS, but it\nshould work on any JavaScript platform supporting the CommonJS module system\n(and if your platform of choice doesn't support CommonJS, you can easily\nimplement it, or discard the exports.* lines from UglifyJS sources).\n

\n

\nThe tokenizer/parser generates an abstract syntax tree from JS code. You\ncan then traverse the AST to learn more about the code, or do various\nmanipulations on it. This part is implemented in parse-js.js and it's a\nport to JavaScript of the excellent parse-js Common Lisp library from Marijn Haverbeke.\n

\n

\n( See cl-uglify-js if you're looking for the Common Lisp version of\nUglifyJS. )\n

\n

\nThe second part of this package, implemented in process.js, inspects and\nmanipulates the AST generated by the parser to provide the following:\n

\n
    \n
  • ability to re-generate JavaScript code from the AST. Optionally\n indented—you can use this if you want to “beautify” a program that has\n been compressed, so that you can inspect the source. But you can also run\n our code generator to print out an AST without any whitespace, so you\n achieve compression as well.\n\n
  • \n
  • shorten variable names (usually to single characters). Our mangler will\n analyze the code and generate proper variable names, depending on scope\n and usage, and is smart enough to deal with globals defined elsewhere, or\n with eval() calls or with{} statements. In short, if eval() or\n with{} are used in some scope, then all variables in that scope and any\n variables in the parent scopes will remain unmangled, and any references\n to such variables remain unmangled as well.\n\n
  • \n
  • various small optimizations that may lead to faster code but certainly\n lead to smaller code. Where possible, we do the following:\n\n
      \n
    • foo[\"bar\"] ==> foo.bar\n\n
    • \n
    • remove block brackets {}\n\n
    • \n
    • join consecutive var declarations:\n var a = 10; var b = 20; ==> var a=10,b=20;\n\n
    • \n
    • resolve simple constant expressions: 1 +2 * 3 ==> 7. We only do the\n replacement if the result occupies less bytes; for example 1/3 would\n translate to 0.333333333333, so in this case we don't replace it.\n\n
    • \n
    • consecutive statements in blocks are merged into a sequence; in many\n cases, this leaves blocks with a single statement, so then we can remove\n the block brackets.\n\n
    • \n
    • various optimizations for IF statements:\n\n
        \n
      • if (foo) bar(); else baz(); ==> foo?bar():baz();\n
      • \n
      • if (!foo) bar(); else baz(); ==> foo?baz():bar();\n
      • \n
      • if (foo) bar(); ==> foo&&bar();\n
      • \n
      • if (!foo) bar(); ==> foo||bar();\n
      • \n
      • if (foo) return bar(); else return baz(); ==> return foo?bar():baz();\n
      • \n
      • if (foo) return bar(); else something(); ==> {if(foo)return bar();something()}\n\n
      • \n
      \n\n
    • \n
    • remove some unreachable code and warn about it (code that follows a\n return, throw, break or continue statement, except\n function/variable declarations).\n\n
    • \n
    • act a limited version of a pre-processor (c.f. the pre-processor of\n C/C++) to allow you to safely replace selected global symbols with\n specified values. When combined with the optimisations above this can\n make UglifyJS operate slightly more like a compilation process, in\n that when certain symbols are replaced by constant values, entire code\n blocks may be optimised away as unreachable.\n
    • \n
    \n\n
  • \n
\n\n\n\n
\n\n
\n

2.1 Unsafe transformations

\n
\n\n\n

\nThe following transformations can in theory break code, although they're\nprobably safe in most practical cases. To enable them you need to pass the\n--unsafe flag.\n

\n\n
\n\n
\n

2.1.1 Calls involving the global Array constructor

\n
\n\n\n

\nThe following transformations occur:\n

\n\n\n\n
new Array(1, 2, 3, 4)  => [1,2,3,4]\nArray(a, b, c)         => [a,b,c]\nnew Array(5)           => Array(5)\nnew Array(a)           => Array(a)\n
\n\n\n

\nThese are all safe if the Array name isn't redefined. JavaScript does allow\none to globally redefine Array (and pretty much everything, in fact) but I\npersonally don't see why would anyone do that.\n

\n

\nUglifyJS does handle the case where Array is redefined locally, or even\nglobally but with a function or var declaration. Therefore, in the\nfollowing cases UglifyJS doesn't touch calls or instantiations of Array:\n

\n\n\n\n
// case 1.  globally declared variable\n  var Array;\n  new Array(1, 2, 3);\n  Array(a, b);\n\n  // or (can be declared later)\n  new Array(1, 2, 3);\n  var Array;\n\n  // or (can be a function)\n  new Array(1, 2, 3);\n  function Array() { ... }\n\n// case 2.  declared in a function\n  (function(){\n    a = new Array(1, 2, 3);\n    b = Array(5, 6);\n    var Array;\n  })();\n\n  // or\n  (function(Array){\n    return Array(5, 6, 7);\n  })();\n\n  // or\n  (function(){\n    return new Array(1, 2, 3, 4);\n    function Array() { ... }\n  })();\n\n  // etc.\n
\n\n\n
\n\n
\n\n
\n

2.1.2 obj.toString() ==> obj+“”

\n
\n\n\n
\n
\n\n
\n\n
\n

2.2 Install (NPM)

\n
\n\n\n

\nUglifyJS is now available through NPM — npm install uglify-js should do\nthe job.\n

\n
\n\n
\n\n
\n

2.3 Install latest code from GitHub

\n
\n\n\n\n\n\n
## clone the repository\nmkdir -p /where/you/wanna/put/it\ncd /where/you/wanna/put/it\ngit clone git://github.com/mishoo/UglifyJS.git\n\n## make the module available to Node\nmkdir -p ~/.node_libraries/\ncd ~/.node_libraries/\nln -s /where/you/wanna/put/it/UglifyJS/uglify-js.js\n\n## and if you want the CLI script too:\nmkdir -p ~/bin\ncd ~/bin\nln -s /where/you/wanna/put/it/UglifyJS/bin/uglifyjs\n  # (then add ~/bin to your $PATH if it's not there already)\n
\n\n\n
\n\n
\n\n
\n

2.4 Usage

\n
\n\n\n

\nThere is a command-line tool that exposes the functionality of this library\nfor your shell-scripting needs:\n

\n\n\n\n
uglifyjs [ options... ] [ filename ]\n
\n\n\n

\nfilename should be the last argument and should name the file from which\nto read the JavaScript code. If you don't specify it, it will read code\nfrom STDIN.\n

\n

\nSupported options:\n

\n
    \n
  • -b or --beautify — output indented code; when passed, additional\n options control the beautifier:\n\n
      \n
    • -i N or --indent N — indentation level (number of spaces)\n\n
    • \n
    • -q or --quote-keys — quote keys in literal objects (by default,\n only keys that cannot be identifier names will be quotes).\n\n
    • \n
    \n\n
  • \n
  • -c or ----consolidate-primitive-values — consolidates null, Boolean,\n and String values. Known as aliasing in the Closure Compiler. Worsens the\n data compression ratio of gzip.\n\n
  • \n
  • --ascii — pass this argument to encode non-ASCII characters as\n \\uXXXX sequences. By default UglifyJS won't bother to do it and will\n output Unicode characters instead. (the output is always encoded in UTF8,\n but if you pass this option you'll only get ASCII).\n\n
  • \n
  • -nm or --no-mangle — don't mangle names.\n\n
  • \n
  • -nmf or --no-mangle-functions – in case you want to mangle variable\n names, but not touch function names.\n\n
  • \n
  • -ns or --no-squeeze — don't call ast_squeeze() (which does various\n optimizations that result in smaller, less readable code).\n\n
  • \n
  • -mt or --mangle-toplevel — mangle names in the toplevel scope too\n (by default we don't do this).\n\n
  • \n
  • --no-seqs — when ast_squeeze() is called (thus, unless you pass\n --no-squeeze) it will reduce consecutive statements in blocks into a\n sequence. For example, \"a = 10; b = 20; foo();\" will be written as\n \"a=10,b=20,foo();\". In various occasions, this allows us to discard the\n block brackets (since the block becomes a single statement). This is ON\n by default because it seems safe and saves a few hundred bytes on some\n libs that I tested it on, but pass --no-seqs to disable it.\n\n
  • \n
  • --no-dead-code — by default, UglifyJS will remove code that is\n obviously unreachable (code that follows a return, throw, break or\n continue statement and is not a function/variable declaration). Pass\n this option to disable this optimization.\n\n
  • \n
  • -nc or --no-copyright — by default, uglifyjs will keep the initial\n comment tokens in the generated code (assumed to be copyright information\n etc.). If you pass this it will discard it.\n\n
  • \n
  • -o filename or --output filename — put the result in filename. If\n this isn't given, the result goes to standard output (or see next one).\n\n
  • \n
  • --overwrite — if the code is read from a file (not from STDIN) and you\n pass --overwrite then the output will be written in the same file.\n\n
  • \n
  • --ast — pass this if you want to get the Abstract Syntax Tree instead\n of JavaScript as output. Useful for debugging or learning more about the\n internals.\n\n
  • \n
  • -v or --verbose — output some notes on STDERR (for now just how long\n each operation takes).\n\n
  • \n
  • -d SYMBOL[=VALUE] or --define SYMBOL[=VALUE] — will replace\n all instances of the specified symbol where used as an identifier\n (except where symbol has properly declared by a var declaration or\n use as function parameter or similar) with the specified value. This\n argument may be specified multiple times to define multiple\n symbols - if no value is specified the symbol will be replaced with\n the value true, or you can specify a numeric value (such as\n 1024), a quoted string value (such as =\"object\"= or\n ='https://github.com'), or the name of another symbol or keyword (such as =null or document).\n This allows you, for example, to assign meaningful names to key\n constant values but discard the symbolic names in the uglified\n version for brevity/efficiency, or when used wth care, allows\n UglifyJS to operate as a form of conditional compilation\n whereby defining appropriate values may, by dint of the constant\n folding and dead code removal features above, remove entire\n superfluous code blocks (e.g. completely remove instrumentation or\n trace code for production use).\n Where string values are being defined, the handling of quotes are\n likely to be subject to the specifics of your command shell\n environment, so you may need to experiment with quoting styles\n depending on your platform, or you may find the option\n --define-from-module more suitable for use.\n\n
  • \n
  • -define-from-module SOMEMODULE — will load the named module (as\n per the NodeJS require() function) and iterate all the exported\n properties of the module defining them as symbol names to be defined\n (as if by the --define option) per the name of each property\n (i.e. without the module name prefix) and given the value of the\n property. This is a much easier way to handle and document groups of\n symbols to be defined rather than a large number of --define\n options.\n\n
  • \n
  • --unsafe — enable other additional optimizations that are known to be\n unsafe in some contrived situations, but could still be generally useful.\n For now only these:\n\n
      \n
    • foo.toString() ==> foo+\"\"\n
    • \n
    • new Array(x,…) ==> [x,…]\n
    • \n
    • new Array(x) ==> Array(x)\n\n
    • \n
    \n\n
  • \n
  • --max-line-len (default 32K characters) — add a newline after around\n 32K characters. I've seen both FF and Chrome croak when all the code was\n on a single line of around 670K. Pass –max-line-len 0 to disable this\n safety feature.\n\n
  • \n
  • --reserved-names — some libraries rely on certain names to be used, as\n pointed out in issue #92 and #81, so this option allow you to exclude such\n names from the mangler. For example, to keep names require and $super\n intact you'd specify –reserved-names \"require,$super\".\n\n
  • \n
  • --inline-script – when you want to include the output literally in an\n HTML <script> tag you can use this option to prevent </script from\n showing up in the output.\n\n
  • \n
  • --lift-vars – when you pass this, UglifyJS will apply the following\n transformations (see the notes in API, ast_lift_variables):\n\n
      \n
    • put all var declarations at the start of the scope\n
    • \n
    • make sure a variable is declared only once\n
    • \n
    • discard unused function arguments\n
    • \n
    • discard unused inner (named) functions\n
    • \n
    • finally, try to merge assignments into that one var declaration, if\n possible.\n
    • \n
    \n\n
  • \n
\n\n\n\n
\n\n
\n

2.4.1 API

\n
\n\n\n

\nTo use the library from JavaScript, you'd do the following (example for\nNodeJS):\n

\n\n\n\n
var jsp = require(\"uglify-js\").parser;\nvar pro = require(\"uglify-js\").uglify;\n\nvar orig_code = \"... JS code here\";\nvar ast = jsp.parse(orig_code); // parse code and get the initial AST\nast = pro.ast_mangle(ast); // get a new AST with mangled names\nast = pro.ast_squeeze(ast); // get an AST with compression optimizations\nvar final_code = pro.gen_code(ast); // compressed code here\n
\n\n\n

\nThe above performs the full compression that is possible right now. As you\ncan see, there are a sequence of steps which you can apply. For example if\nyou want compressed output but for some reason you don't want to mangle\nvariable names, you would simply skip the line that calls\npro.ast_mangle(ast).\n

\n

\nSome of these functions take optional arguments. Here's a description:\n

\n
    \n
  • jsp.parse(code, strict_semicolons) – parses JS code and returns an AST.\n strict_semicolons is optional and defaults to false. If you pass\n true then the parser will throw an error when it expects a semicolon and\n it doesn't find it. For most JS code you don't want that, but it's useful\n if you want to strictly sanitize your code.\n\n
  • \n
  • pro.ast_lift_variables(ast) – merge and move var declarations to the\n scop of the scope; discard unused function arguments or variables; discard\n unused (named) inner functions. It also tries to merge assignments\n following the var declaration into it.\n\n

    \n If your code is very hand-optimized concerning var declarations, this\n lifting variable declarations might actually increase size. For me it\n helps out. On jQuery it adds 865 bytes (243 after gzip). YMMV. Also\n note that (since it's not enabled by default) this operation isn't yet\n heavily tested (please report if you find issues!).\n

    \n

    \n Note that although it might increase the image size (on jQuery it gains\n 865 bytes, 243 after gzip) it's technically more correct: in certain\n situations, dead code removal might drop variable declarations, which\n would not happen if the variables are lifted in advance.\n

    \n

    \n Here's an example of what it does:\n

  • \n
\n\n\n\n\n\n
function f(a, b, c, d, e) {\n    var q;\n    var w;\n    w = 10;\n    q = 20;\n    for (var i = 1; i < 10; ++i) {\n        var boo = foo(a);\n    }\n    for (var i = 0; i < 1; ++i) {\n        var boo = bar(c);\n    }\n    function foo(){ ... }\n    function bar(){ ... }\n    function baz(){ ... }\n}\n\n// transforms into ==>\n\nfunction f(a, b, c) {\n    var i, boo, w = 10, q = 20;\n    for (i = 1; i < 10; ++i) {\n        boo = foo(a);\n    }\n    for (i = 0; i < 1; ++i) {\n        boo = bar(c);\n    }\n    function foo() { ... }\n    function bar() { ... }\n}\n
\n\n\n
    \n
  • pro.ast_mangle(ast, options) – generates a new AST containing mangled\n (compressed) variable and function names. It supports the following\n options:\n\n
      \n
    • toplevel – mangle toplevel names (by default we don't touch them).\n
    • \n
    • except – an array of names to exclude from compression.\n
    • \n
    • defines – an object with properties named after symbols to\n replace (see the --define option for the script) and the values\n representing the AST replacement value.\n\n
    • \n
    \n\n
  • \n
  • pro.ast_squeeze(ast, options) – employs further optimizations designed\n to reduce the size of the code that gen_code would generate from the\n AST. Returns a new AST. options can be a hash; the supported options\n are:\n\n
      \n
    • make_seqs (default true) which will cause consecutive statements in a\n block to be merged using the \"sequence\" (comma) operator\n\n
    • \n
    • dead_code (default true) which will remove unreachable code.\n\n
    • \n
    \n\n
  • \n
  • pro.gen_code(ast, options) – generates JS code from the AST. By\n default it's minified, but using the options argument you can get nicely\n formatted output. options is, well, optional :-) and if you pass it it\n must be an object and supports the following properties (below you can see\n the default values):\n\n
      \n
    • beautify: false – pass true if you want indented output\n
    • \n
    • indent_start: 0 (only applies when beautify is true) – initial\n indentation in spaces\n
    • \n
    • indent_level: 4 (only applies when beautify is true) --\n indentation level, in spaces (pass an even number)\n
    • \n
    • quote_keys: false – if you pass true it will quote all keys in\n literal objects\n
    • \n
    • space_colon: false (only applies when beautify is true) – wether\n to put a space before the colon in object literals\n
    • \n
    • ascii_only: false – pass true if you want to encode non-ASCII\n characters as \\uXXXX.\n
    • \n
    • inline_script: false – pass true to escape occurrences of\n </script in strings\n
    • \n
    \n\n
  • \n
\n\n\n
\n\n
\n\n
\n

2.4.2 Beautifier shortcoming – no more comments

\n
\n\n\n

\nThe beautifier can be used as a general purpose indentation tool. It's\nuseful when you want to make a minified file readable. One limitation,\nthough, is that it discards all comments, so you don't really want to use it\nto reformat your code, unless you don't have, or don't care about, comments.\n

\n

\nIn fact it's not the beautifier who discards comments — they are dumped at\nthe parsing stage, when we build the initial AST. Comments don't really\nmake sense in the AST, and while we could add nodes for them, it would be\ninconvenient because we'd have to add special rules to ignore them at all\nthe processing stages.\n

\n
\n\n
\n\n
\n

2.4.3 Use as a code pre-processor

\n
\n\n\n

\nThe --define option can be used, particularly when combined with the\nconstant folding logic, as a form of pre-processor to enable or remove\nparticular constructions, such as might be used for instrumenting\ndevelopment code, or to produce variations aimed at a specific\nplatform.\n

\n

\nThe code below illustrates the way this can be done, and how the\nsymbol replacement is performed.\n

\n\n\n\n
CLAUSE1: if (typeof DEVMODE === 'undefined') {\n    DEVMODE = true;\n}\n\nCLAUSE2: function init() {\n    if (DEVMODE) {\n        console.log(\"init() called\");\n    }\n    ....\n    DEVMODE &amp;&amp; console.log(\"init() complete\");\n}\n\nCLAUSE3: function reportDeviceStatus(device) {\n    var DEVMODE = device.mode, DEVNAME = device.name;\n    if (DEVMODE === 'open') {\n        ....\n    }\n}\n
\n\n\n

\nWhen the above code is normally executed, the undeclared global\nvariable DEVMODE will be assigned the value true (see CLAUSE1)\nand so the init() function (CLAUSE2) will write messages to the\nconsole log when executed, but in CLAUSE3 a locally declared\nvariable will mask access to the DEVMODE global symbol.\n

\n

\nIf the above code is processed by UglifyJS with an argument of\n--define DEVMODE=false then UglifyJS will replace DEVMODE with the\nboolean constant value false within CLAUSE1 and CLAUSE2, but it\nwill leave CLAUSE3 as it stands because there DEVMODE resolves to\na validly declared variable.\n

\n

\nAnd more so, the constant-folding features of UglifyJS will recognise\nthat the if condition of CLAUSE1 is thus always false, and so will\nremove the test and body of CLAUSE1 altogether (including the\notherwise slightly problematical statement false = true; which it\nwill have formed by replacing DEVMODE in the body). Similarly,\nwithin CLAUSE2 both calls to console.log() will be removed\naltogether.\n

\n

\nIn this way you can mimic, to a limited degree, the functionality of\nthe C/C++ pre-processor to enable or completely remove blocks\ndepending on how certain symbols are defined - perhaps using UglifyJS\nto generate different versions of source aimed at different\nenvironments\n

\n

\nIt is recommmended (but not made mandatory) that symbols designed for\nthis purpose are given names consisting of UPPER_CASE_LETTERS to\ndistinguish them from other (normal) symbols and avoid the sort of\nclash that CLAUSE3 above illustrates.\n

\n
\n
\n\n
\n\n
\n

2.5 Compression – how good is it?

\n
\n\n\n

\nHere are updated statistics. (I also updated my Google Closure and YUI\ninstallations).\n

\n

\nWe're still a lot better than YUI in terms of compression, though slightly\nslower. We're still a lot faster than Closure, and compression after gzip\nis comparable.\n

\n\n\n\n\n\n\n\n\n\n\n\n\n\n
FileUglifyJSUglifyJS+gzipClosureClosure+gzipYUIYUI+gzip
jquery-1.6.2.js91001 (0:01.59)3189690678 (0:07.40)31979101527 (0:01.82)34646
paper.js142023 (0:01.65)43334134301 (0:07.42)42495173383 (0:01.58)48785
prototype.js88544 (0:01.09)2668086955 (0:06.97)2632692130 (0:00.79)28624
thelib-full.js (DynarchLIB)251939 (0:02.55)72535249911 (0:09.05)72696258869 (0:01.94)76584
\n\n\n
\n\n
\n\n
\n

2.6 Bugs?

\n
\n\n\n

\nUnfortunately, for the time being there is no automated test suite. But I\nran the compressor manually on non-trivial code, and then I tested that the\ngenerated code works as expected. A few hundred times.\n

\n

\nDynarchLIB was started in times when there was no good JS minifier.\nTherefore I was quite religious about trying to write short code manually,\nand as such DL contains a lot of syntactic hacks1 such as “foo == bar ? a\n= 10 : b = 20”, though the more readable version would clearly be to use\n“if/else”.\n

\n

\nSince the parser/compressor runs fine on DL and jQuery, I'm quite confident\nthat it's solid enough for production use. If you can identify any bugs,\nI'd love to hear about them (use the Google Group or email me directly).\n

\n
\n\n
\n\n
\n

2.7 Links

\n
\n\n\n\n\n\n
\n\n
\n\n
\n

2.8 License

\n
\n\n\n

\nUglifyJS is released under the BSD license:\n

\n\n\n\n
Copyright 2010 (c) Mihai Bazon <mihai.bazon@gmail.com>\nBased on parse-js (http://marijn.haverbeke.nl/parse-js/).\n\nRedistribution and use in source and binary forms, with or without\nmodification, are permitted provided that the following conditions\nare met:\n\n    * Redistributions of source code must retain the above\n      copyright notice, this list of conditions and the following\n      disclaimer.\n\n    * Redistributions in binary form must reproduce the above\n      copyright notice, this list of conditions and the following\n      disclaimer in the documentation and/or other materials\n      provided with the distribution.\n\nTHIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDER “AS IS” AND ANY\nEXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE\nIMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR\nPURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER BE\nLIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY,\nOR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,\nPROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR\nPROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY\nTHEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR\nTORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF\nTHE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF\nSUCH DAMAGE.\n
\n\n\n
\n

Footnotes:

\n
\n

1 I even reported a few bugs and suggested some fixes in the original\n parse-js library, and Marijn pushed fixes literally in minutes.\n

\n
\n
\n\n
\n
\n
\n\n
\n

Date: 2012-08-27 12:38:12 EEST

\n

Author: Mihai Bazon

\n

Org version 7.7 with Emacs version 23

\nValidate XHTML 1.0\n\n
\n\n\n", + "readmeFilename": "README.html", + "_id": "uglify-js@1.3.4", + "_from": "uglify-js" +} diff --git a/node_modules/uglify-js/test/beautify.js b/node_modules/uglify-js/test/beautify.js new file mode 100755 index 00000000..f19369e3 --- /dev/null +++ b/node_modules/uglify-js/test/beautify.js @@ -0,0 +1,28 @@ +#! /usr/bin/env node + +global.sys = require("sys"); +var fs = require("fs"); + +var jsp = require("../lib/parse-js"); +var pro = require("../lib/process"); + +var filename = process.argv[2]; +fs.readFile(filename, "utf8", function(err, text){ + try { + var ast = time_it("parse", function(){ return jsp.parse(text); }); + ast = time_it("mangle", function(){ return pro.ast_mangle(ast); }); + ast = time_it("squeeze", function(){ return pro.ast_squeeze(ast); }); + var gen = time_it("generate", function(){ return pro.gen_code(ast, false); }); + sys.puts(gen); + } catch(ex) { + sys.debug(ex.stack); + sys.debug(sys.inspect(ex)); + sys.debug(JSON.stringify(ex)); + } +}); + +function time_it(name, cont) { + var t1 = new Date().getTime(); + try { return cont(); } + finally { sys.debug("// " + name + ": " + ((new Date().getTime() - t1) / 1000).toFixed(3) + " sec."); } +}; diff --git a/node_modules/uglify-js/test/testconsolidator.js b/node_modules/uglify-js/test/testconsolidator.js new file mode 100755 index 00000000..08030129 --- /dev/null +++ b/node_modules/uglify-js/test/testconsolidator.js @@ -0,0 +1,1391 @@ +#! /usr/bin/env node +global.DIGITS_OVERRIDE_FOR_TESTING = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ$_0123456789"; + +'use strict'; +/*jshint bitwise:true, curly:true, eqeqeq:true, forin:true, immed:true, + latedef:true, newcap:true, noarge:true, noempty:true, nonew:true, + onevar:true, plusplus:true, regexp:true, undef:true, strict:true, + sub:false, trailing:true */ + +var _, + /** + * NodeJS module for unit testing. + * @namespace + * @type {!TAssert} + * @see http://nodejs.org/docs/v0.6.10/api/all.html#assert + */ + oAssert = (/** @type {!TAssert} */ require('assert')), + /** + * Consolidates null, Boolean, and String values found inside an + * AST. The object under test. + * @namespace + * @type {!TConsolidator} + */ + oConsolidator = (/** @type {!TConsolidator} */ require('../lib/consolidator')), + /** + * The parser of ECMA-262 found in UglifyJS. + * @namespace + * @type {!TParser} + */ + oParser = (/** @type {!TParser} */ require('../lib/parse-js')), + /** + * The processor of ASTs + * found in UglifyJS. + * @namespace + * @type {!TProcessor} + */ + oProcessor = (/** @type {!TProcessor} */ require('../lib/process')), + /** + * An instance of an object that allows the traversal of an AST. + * @type {!TWalker} + */ + oWalker, + /** + * A collection of functions for the removal of the scope information + * during the traversal of an AST. + * @namespace + * @type {!Object.} + */ + oWalkersPurifiers = { + /**#nocode+*/ // JsDoc Toolkit 2.4.0 hides some of the keys. + /** + * Deletes the scope information from the branch of the abstract + * syntax tree representing the encountered function declaration. + * @param {string} sIdentifier The identifier of the function. + * @param {!Array.} aFormalParameterList Formal parameters. + * @param {!TSyntacticCodeUnit} oFunctionBody Function code. + */ + 'defun': function( + sIdentifier, + aFormalParameterList, + oFunctionBody) { + delete oFunctionBody.scope; + }, + /** + * Deletes the scope information from the branch of the abstract + * syntax tree representing the encountered function expression. + * @param {?string} sIdentifier The optional identifier of the + * function. + * @param {!Array.} aFormalParameterList Formal parameters. + * @param {!TSyntacticCodeUnit} oFunctionBody Function code. + */ + 'function': function( + sIdentifier, + aFormalParameterList, + oFunctionBody) { + delete oFunctionBody.scope; + } + /**#nocode-*/ // JsDoc Toolkit 2.4.0 hides some of the keys. + }, + /** + * Initiates the traversal of a source element. + * @param {!TWalker} oWalker An instance of an object that allows the + * traversal of an abstract syntax tree. + * @param {!TSyntacticCodeUnit} oSourceElement A source element from + * which the traversal should commence. + * @return {function(): !TSyntacticCodeUnit} A function that is able to + * initiate the traversal from a given source element. + */ + cContext = function(oWalker, oSourceElement) { + /** + * @return {!TSyntacticCodeUnit} A function that is able to + * initiate the traversal from a given source element. + */ + var fLambda = function() { + return oWalker.walk(oSourceElement); + }; + + return fLambda; + }, + /** + * A record consisting of configuration for the code generation phase. + * @type {!Object} + */ + oCodeGenerationOptions = { + beautify: true + }, + /** + * A boolean to track whether all tests have passed. + * @type {boolean} + */ + bTestsPassed = true, + /** + * Tests whether consolidation of an ECMAScript program yields expected + * results. + * @param {{ + * sTitle: string, + * sInput: string, + * sOutput: string + * }} oUnitTest A record consisting of data about a unit test: its + * name, an ECMAScript program, and, if consolidation is to take + * place, the resulting ECMAScript program. + */ + cAssert = function(oUnitTest) { + var _, + /** + * An array-like object representing the AST obtained after consolidation. + * @type {!TSyntacticCodeUnit} + */ + oSyntacticCodeUnitActual = + oConsolidator.ast_consolidate(oParser.parse(oUnitTest.sInput)), + /** + * An array-like object representing the expected AST. + * @type {!TSyntacticCodeUnit} + */ + oSyntacticCodeUnitExpected = oParser.parse( + oUnitTest.hasOwnProperty('sOutput') ? + oUnitTest.sOutput : oUnitTest.sInput); + + delete oSyntacticCodeUnitActual.scope; + oWalker = oProcessor.ast_walker(); + oWalker.with_walkers( + oWalkersPurifiers, + cContext(oWalker, oSyntacticCodeUnitActual)); + try { + oAssert.deepEqual( + oSyntacticCodeUnitActual, + oSyntacticCodeUnitExpected); + } catch (oException) { + console.error( + '########## A unit test has failed.\n' + + oUnitTest.sTitle + '\n' + + '##### actual code (' + + oProcessor.gen_code(oSyntacticCodeUnitActual).length + + ' bytes)\n' + + oProcessor.gen_code( + oSyntacticCodeUnitActual, + oCodeGenerationOptions) + '\n' + + '##### expected code (' + + oProcessor.gen_code(oSyntacticCodeUnitExpected).length + + ' bytes)\n' + + oProcessor.gen_code( + oSyntacticCodeUnitExpected, + oCodeGenerationOptions)); + bTestsPassed = false; + } + }; + +[ + // 7.6.1 Reserved Words. + { + sTitle: + 'Omission of keywords while choosing an identifier name.', + sInput: + '(function() {' + + ' var a, b, c, d, e, f, g, h, i, j, k, l, m,' + + ' n, o, p, q, r, s, t, u, v, w, x, y, z,' + + ' A, B, C, D, E, F, G, H, I, J, K, L, M,' + + ' N, O, P, Q, R, S, T, U, V, W, X, Y, Z,' + + ' $, _,' + + ' aa, ab, ac, ad, ae, af, ag, ah, ai, aj, ak, al, am,' + + ' an, ao, ap, aq, ar, as, at, au, av, aw, ax, ay, az,' + + ' aA, aB, aC, aD, aE, aF, aG, aH, aI, aJ, aK, aL, aM,' + + ' aN, aO, aP, aQ, aR, aS, aT, aU, aV, aW, aX, aY, aZ,' + + ' a$, a_,' + + ' ba, bb, bc, bd, be, bf, bg, bh, bi, bj, bk, bl, bm,' + + ' bn, bo, bp, bq, br, bs, bt, bu, bv, bw, bx, by, bz,' + + ' bA, bB, bC, bD, bE, bF, bG, bH, bI, bJ, bK, bL, bM,' + + ' bN, bO, bP, bQ, bR, bS, bT, bU, bV, bW, bX, bY, bZ,' + + ' b$, b_,' + + ' ca, cb, cc, cd, ce, cf, cg, ch, ci, cj, ck, cl, cm,' + + ' cn, co, cp, cq, cr, cs, ct, cu, cv, cw, cx, cy, cz,' + + ' cA, cB, cC, cD, cE, cF, cG, cH, cI, cJ, cK, cL, cM,' + + ' cN, cO, cP, cQ, cR, cS, cT, cU, cV, cW, cX, cY, cZ,' + + ' c$, c_,' + + ' da, db, dc, dd, de, df, dg, dh, di, dj, dk, dl, dm,' + + ' dn, dq, dr, ds, dt, du, dv, dw, dx, dy, dz,' + + ' dA, dB, dC, dD, dE, dF, dG, dH, dI, dJ, dK, dL, dM,' + + ' dN, dO, dP, dQ, dR, dS, dT, dU, dV, dW, dX, dY, dZ,' + + ' d$, d_;' + + ' void ["abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ",' + + ' "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ"];' + + '}());', + sOutput: + '(function() {' + + ' var eb =' + + ' "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ",' + + ' a, b, c, d, e, f, g, h, i, j, k, l, m,' + + ' n, o, p, q, r, s, t, u, v, w, x, y, z,' + + ' A, B, C, D, E, F, G, H, I, J, K, L, M,' + + ' N, O, P, Q, R, S, T, U, V, W, X, Y, Z,' + + ' $, _,' + + ' aa, ab, ac, ad, ae, af, ag, ah, ai, aj, ak, al, am,' + + ' an, ao, ap, aq, ar, as, at, au, av, aw, ax, ay, az,' + + ' aA, aB, aC, aD, aE, aF, aG, aH, aI, aJ, aK, aL, aM,' + + ' aN, aO, aP, aQ, aR, aS, aT, aU, aV, aW, aX, aY, aZ,' + + ' a$, a_,' + + ' ba, bb, bc, bd, be, bf, bg, bh, bi, bj, bk, bl, bm,' + + ' bn, bo, bp, bq, br, bs, bt, bu, bv, bw, bx, by, bz,' + + ' bA, bB, bC, bD, bE, bF, bG, bH, bI, bJ, bK, bL, bM,' + + ' bN, bO, bP, bQ, bR, bS, bT, bU, bV, bW, bX, bY, bZ,' + + ' b$, b_,' + + ' ca, cb, cc, cd, ce, cf, cg, ch, ci, cj, ck, cl, cm,' + + ' cn, co, cp, cq, cr, cs, ct, cu, cv, cw, cx, cy, cz,' + + ' cA, cB, cC, cD, cE, cF, cG, cH, cI, cJ, cK, cL, cM,' + + ' cN, cO, cP, cQ, cR, cS, cT, cU, cV, cW, cX, cY, cZ,' + + ' c$, c_,' + + ' da, db, dc, dd, de, df, dg, dh, di, dj, dk, dl, dm,' + + ' dn, dq, dr, ds, dt, du, dv, dw, dx, dy, dz,' + + ' dA, dB, dC, dD, dE, dF, dG, dH, dI, dJ, dK, dL, dM,' + + ' dN, dO, dP, dQ, dR, dS, dT, dU, dV, dW, dX, dY, dZ,' + + ' d$, d_;' + + ' void [eb, eb];' + + '}());' + }, + // 7.8.1 Null Literals. + { + sTitle: + 'Evaluation with regard to the null value.', + sInput: + '/*jshint evil:true */' + + '(function() {' + + ' var foo;' + + ' void [null, null, null];' + + '}());' + + 'eval("");' + + '(function() {' + + ' var foo;' + + ' void [null, null];' + + '}());', + sOutput: + '/*jshint evil:true */' + + '(function() {' + + ' var a = null, foo;' + + ' void [a, a, a];' + + '}());' + + 'eval("");' + + '(function() {' + + ' var foo;' + + ' void [null, null];' + + '}());' + }, + // 7.8.2 Boolean Literals. + { + sTitle: + 'Evaluation with regard to the false value.', + sInput: + '/*jshint evil:true */' + + '(function() {' + + ' var foo;' + + ' void [false, false, false];' + + '}());' + + 'eval("");' + + '(function() {' + + ' var foo;' + + ' void [false, false];' + + '}());', + sOutput: + '/*jshint evil:true */' + + '(function() {' + + ' var a = false, foo;' + + ' void [a, a, a];' + + '}());' + + 'eval("");' + + '(function() {' + + ' var foo;' + + ' void [false, false];' + + '}());' + }, + { + sTitle: + 'Evaluation with regard to the true value.', + sInput: + '/*jshint evil:true */' + + '(function() {' + + ' var foo;' + + ' void [true, true, true];' + + '}());' + + 'eval("");' + + '(function() {' + + ' var foo;' + + ' void [true, true];' + + '}());', + sOutput: + '/*jshint evil:true */' + + '(function() {' + + ' var a = true, foo;' + + ' void [a, a, a];' + + '}());' + + 'eval("");' + + '(function() {' + + ' var foo;' + + ' void [true, true];' + + '}());' + }, + // 7.8.4 String Literals. + { + sTitle: + 'Evaluation with regard to the String value of a string literal.', + sInput: + '(function() {' + + ' var foo;' + + ' void ["abcd", "abcd", "abc", "abc"];' + + '}());', + sOutput: + '(function() {' + + ' var a = "abcd", foo;' + + ' void [a, a, "abc", "abc"];' + + '}());' + }, + // 7.8.5 Regular Expression Literals. + { + sTitle: + 'Preservation of the pattern of a regular expression literal.', + sInput: + 'void [/abcdefghijklmnopqrstuvwxyz/, /abcdefghijklmnopqrstuvwxyz/];' + }, + { + sTitle: + 'Preservation of the flags of a regular expression literal.', + sInput: + 'void [/(?:)/gim, /(?:)/gim, /(?:)/gim, /(?:)/gim, /(?:)/gim,' + + ' /(?:)/gim, /(?:)/gim, /(?:)/gim, /(?:)/gim, /(?:)/gim,' + + ' /(?:)/gim, /(?:)/gim, /(?:)/gim, /(?:)/gim, /(?:)/gim];' + }, + // 10.2 Lexical Environments. + { + sTitle: + 'Preservation of identifier names in the same scope.', + sInput: + '/*jshint shadow:true */' + + 'var a;' + + 'function b(i) {' + + '}' + + 'for (var c; 0 === Math.random(););' + + 'for (var d in {});' + + 'void ["abcdefghijklmnopqrstuvwxyz"];' + + 'void [b(a), b(c), b(d)];' + + 'void [typeof e];' + + 'i: for (; 0 === Math.random();) {' + + ' if (42 === (new Date()).getMinutes()) {' + + ' continue i;' + + ' } else {' + + ' break i;' + + ' }' + + '}' + + 'try {' + + '} catch (f) {' + + '} finally {' + + '}' + + '(function g(h) {' + + '}());' + + 'void [{' + + ' i: 42,' + + ' "j": 42,' + + ' \'k\': 42' + + '}];' + + 'void ["abcdefghijklmnopqrstuvwxyz"];', + sOutput: + '/*jshint shadow:true */' + + 'var a;' + + 'function b(i) {' + + '}' + + 'for (var c; 0 === Math.random(););' + + 'for (var d in {});' + + '(function() {' + + ' var i = "abcdefghijklmnopqrstuvwxyz";' + + ' void [i];' + + ' void [b(a), b(c), b(d)];' + + ' void [typeof e];' + + ' i: for (; 0 === Math.random();) {' + + ' if (42 === (new Date()).getMinutes()) {' + + ' continue i;' + + ' } else {' + + ' break i;' + + ' }' + + ' }' + + ' try {' + + ' } catch (f) {' + + ' } finally {' + + ' }' + + ' (function g(h) {' + + ' }());' + + ' void [{' + + ' i: 42,' + + ' "j": 42,' + + ' \'k\': 42' + + ' }];' + + ' void [i];' + + '}());' + }, + { + sTitle: + 'Preservation of identifier names in nested function code.', + sInput: + '(function() {' + + ' void ["abcdefghijklmnopqrstuvwxyz"];' + + ' (function() {' + + ' var a;' + + ' for (var b; 0 === Math.random(););' + + ' for (var c in {});' + + ' void [typeof d];' + + ' h: for (; 0 === Math.random();) {' + + ' if (42 === (new Date()).getMinutes()) {' + + ' continue h;' + + ' } else {' + + ' break h;' + + ' }' + + ' }' + + ' try {' + + ' } catch (e) {' + + ' } finally {' + + ' }' + + ' (function f(g) {' + + ' }());' + + ' void [{' + + ' h: 42,' + + ' "i": 42,' + + ' \'j\': 42' + + ' }];' + + ' }());' + + ' void ["abcdefghijklmnopqrstuvwxyz"];' + + '}());', + sOutput: + '(function() {' + + ' var h = "abcdefghijklmnopqrstuvwxyz";' + + ' void [h];' + + ' (function() {' + + ' var a;' + + ' for (var b; 0 === Math.random(););' + + ' for (var c in {});' + + ' void [typeof d];' + + ' h: for (; 0 === Math.random();) {' + + ' if (42 === (new Date()).getMinutes()) {' + + ' continue h;' + + ' } else {' + + ' break h;' + + ' }' + + ' }' + + ' try {' + + ' } catch (e) {' + + ' } finally {' + + ' }' + + ' (function f(g) {' + + ' }());' + + ' void [{' + + ' h: 42,' + + ' "i": 42,' + + ' \'j\': 42' + + ' }];' + + ' }());' + + ' void [h];' + + '}());' + }, + { + sTitle: + 'Consolidation of a closure with other source elements.', + sInput: + '(function(foo) {' + + '}("abcdefghijklmnopqrstuvwxyz"));' + + 'void ["abcdefghijklmnopqrstuvwxyz"];', + sOutput: + '(function() {' + + ' var a = "abcdefghijklmnopqrstuvwxyz";' + + ' (function(foo) {' + + ' })(a);' + + ' void [a];' + + '}());' + }, + { + sTitle: + 'Consolidation of function code instead of a sole closure.', + sInput: + '(function(foo, bar) {' + + ' void ["abcdefghijklmnopqrstuvwxyz",' + + ' "abcdefghijklmnopqrstuvwxyz"];' + + '}("abcdefghijklmnopqrstuvwxyz", "abcdefghijklmnopqrstuvwxyz"));', + sOutput: + '(function(foo, bar) {' + + ' var a = "abcdefghijklmnopqrstuvwxyz";' + + ' void [a, a];' + + '}("abcdefghijklmnopqrstuvwxyz", "abcdefghijklmnopqrstuvwxyz"));' + }, + // 11.1.5 Object Initialiser. + { + sTitle: + 'Preservation of property names of an object initialiser.', + sInput: + 'var foo = {' + + ' abcdefghijklmnopqrstuvwxyz: 42,' + + ' "zyxwvutsrqponmlkjihgfedcba": 42,' + + ' \'mlkjihgfedcbanopqrstuvwxyz\': 42' + + '};' + + 'void [' + + ' foo.abcdefghijklmnopqrstuvwxyz,' + + ' "zyxwvutsrqponmlkjihgfedcba",' + + ' \'mlkjihgfedcbanopqrstuvwxyz\'' + + '];' + }, + { + sTitle: + 'Evaluation with regard to String values derived from identifier ' + + 'names used as property accessors.', + sInput: + '(function() {' + + ' var foo;' + + ' void [' + + ' Math.abcdefghij,' + + ' Math.abcdefghij,' + + ' Math.abcdefghi,' + + ' Math.abcdefghi' + + ' ];' + + '}());', + sOutput: + '(function() {' + + ' var a = "abcdefghij", foo;' + + ' void [' + + ' Math[a],' + + ' Math[a],' + + ' Math.abcdefghi,' + + ' Math.abcdefghi' + + ' ];' + + '}());' + }, + // 11.2.1 Property Accessors. + { + sTitle: + 'Preservation of identifiers in the nonterminal MemberExpression.', + sInput: + 'void [' + + ' Math.E,' + + ' Math.LN10,' + + ' Math.LN2,' + + ' Math.LOG2E,' + + ' Math.LOG10E,' + + ' Math.PI,' + + ' Math.SQRT1_2,' + + ' Math.SQRT2,' + + ' Math.abs,' + + ' Math.acos' + + '];' + }, + // 12.2 Variable Statement. + { + sTitle: + 'Preservation of the identifier of a variable that is being ' + + 'declared in a variable statement.', + sInput: + '(function() {' + + ' var abcdefghijklmnopqrstuvwxyz;' + + ' void [abcdefghijklmnopqrstuvwxyz];' + + '}());' + }, + { + sTitle: + 'Exclusion of a variable statement in global code.', + sInput: + 'void ["abcdefghijklmnopqrstuvwxyz"];' + + 'var foo = "abcdefghijklmnopqrstuvwxyz",' + + ' bar = "abcdefghijklmnopqrstuvwxyz";' + + 'void ["abcdefghijklmnopqrstuvwxyz"];' + }, + { + sTitle: + 'Exclusion of a variable statement in function code that ' + + 'contains a with statement.', + sInput: + '(function() {' + + ' with ({});' + + ' void ["abcdefghijklmnopqrstuvwxyz"];' + + ' var foo;' + + ' void ["abcdefghijklmnopqrstuvwxyz"];' + + '}());' + }, + { + sTitle: + 'Exclusion of a variable statement in function code that ' + + 'contains a direct call to the eval function.', + sInput: + '/*jshint evil:true */' + + 'void [' + + ' function() {' + + ' eval("");' + + ' void ["abcdefghijklmnopqrstuvwxyz"];' + + ' var foo;' + + ' void ["abcdefghijklmnopqrstuvwxyz"];' + + ' }' + + '];' + }, + { + sTitle: + 'Consolidation within a variable statement in global code.', + sInput: + 'var foo = function() {' + + ' void ["abcdefghijklmnopqrstuvwxyz",' + + ' "abcdefghijklmnopqrstuvwxyz"];' + + '};', + sOutput: + 'var foo = function() {' + + ' var a = "abcdefghijklmnopqrstuvwxyz";' + + ' void [a, a];' + + '};' + }, + { + sTitle: + 'Consolidation within a variable statement excluded in function ' + + 'code due to the presence of a with statement.', + sInput: + '(function() {' + + ' with ({});' + + ' var foo = function() {' + + ' void ["abcdefghijklmnopqrstuvwxyz",' + + ' "abcdefghijklmnopqrstuvwxyz"];' + + ' };' + + '}());', + sOutput: + '(function() {' + + ' with ({});' + + ' var foo = function() {' + + ' var a = "abcdefghijklmnopqrstuvwxyz";' + + ' void [a, a];' + + ' };' + + '}());' + }, + { + sTitle: + 'Consolidation within a variable statement excluded in function ' + + 'code due to the presence of a direct call to the eval function.', + sInput: + '/*jshint evil:true */' + + '(function() {' + + ' eval("");' + + ' var foo = function() {' + + ' void ["abcdefghijklmnopqrstuvwxyz",' + + ' "abcdefghijklmnopqrstuvwxyz"];' + + ' };' + + '}());', + sOutput: + '/*jshint evil:true */' + + '(function() {' + + ' eval("");' + + ' var foo = function() {' + + ' var a = "abcdefghijklmnopqrstuvwxyz";' + + ' void [a, a];' + + ' };' + + '}());' + }, + { + sTitle: + 'Inclusion of a variable statement in function code that ' + + 'contains no with statement and no direct call to the eval ' + + 'function.', + sInput: + '(function() {' + + ' void ["abcdefghijklmnopqrstuvwxyz"];' + + ' var foo;' + + ' void ["abcdefghijklmnopqrstuvwxyz"];' + + '}());', + sOutput: + '(function() {' + + ' var a = "abcdefghijklmnopqrstuvwxyz";' + + ' void [a];' + + ' var foo;' + + ' void [a];' + + '}());' + }, + { + sTitle: + 'Ignorance with regard to a variable statement in global code.', + sInput: + 'var foo = "abcdefghijklmnopqrstuvwxyz";' + + 'void ["abcdefghijklmnopqrstuvwxyz",' + + ' "abcdefghijklmnopqrstuvwxyz"];', + sOutput: + 'var foo = "abcdefghijklmnopqrstuvwxyz";' + + '(function() {' + + ' var a = "abcdefghijklmnopqrstuvwxyz";' + + ' void [a, a];' + + '}());' + }, + // 12.4 Expression Statement. + { + sTitle: + 'Preservation of identifiers in an expression statement.', + sInput: + 'void [typeof abcdefghijklmnopqrstuvwxyz,' + + ' typeof abcdefghijklmnopqrstuvwxyz];' + }, + // 12.6.3 The {@code for} Statement. + { + sTitle: + 'Preservation of identifiers in the variable declaration list of ' + + 'a for statement.', + sInput: + 'for (var abcdefghijklmnopqrstuvwxyz; 0 === Math.random(););' + + 'for (var abcdefghijklmnopqrstuvwxyz; 0 === Math.random(););' + }, + // 12.6.4 The {@code for-in} Statement. + { + sTitle: + 'Preservation of identifiers in the variable declaration list of ' + + 'a for-in statement.', + sInput: + 'for (var abcdefghijklmnopqrstuvwxyz in {});' + + 'for (var abcdefghijklmnopqrstuvwxyz in {});' + }, + // 12.7 The {@code continue} Statement. + { + sTitle: + 'Preservation of the identifier in a continue statement.', + sInput: + 'abcdefghijklmnopqrstuvwxyz: for (; 0 === Math.random();) {' + + ' continue abcdefghijklmnopqrstuvwxyz;' + + '}' + + 'abcdefghijklmnopqrstuvwxyz: for (; 0 === Math.random();) {' + + ' continue abcdefghijklmnopqrstuvwxyz;' + + '}' + }, + // 12.8 The {@code break} Statement. + { + sTitle: + 'Preservation of the identifier in a break statement.', + sInput: + 'abcdefghijklmnopqrstuvwxyz: for (; 0 === Math.random();) {' + + ' break abcdefghijklmnopqrstuvwxyz;' + + '}' + + 'abcdefghijklmnopqrstuvwxyz: for (; 0 === Math.random();) {' + + ' break abcdefghijklmnopqrstuvwxyz;' + + '}' + }, + // 12.9 The {@code return} Statement. + { + sTitle: + 'Exclusion of a return statement in function code that contains ' + + 'a with statement.', + sInput: + '(function() {' + + ' with ({});' + + ' void ["abcdefghijklmnopqrstuvwxyz"];' + + ' if (0 === Math.random()) {' + + ' return;' + + ' } else {' + + ' }' + + ' void ["abcdefghijklmnopqrstuvwxyz"];' + + '}());' + }, + { + sTitle: + 'Exclusion of a return statement in function code that contains ' + + 'a direct call to the eval function.', + sInput: + '/*jshint evil:true */' + + '(function() {' + + ' eval("");' + + ' void ["abcdefghijklmnopqrstuvwxyz"];' + + ' if (0 === Math.random()) {' + + ' return;' + + ' } else {' + + ' }' + + ' void ["abcdefghijklmnopqrstuvwxyz"];' + + '}());' + }, + { + sTitle: + 'Consolidation within a return statement excluded in function ' + + 'code due to the presence of a with statement.', + sInput: + '(function() {' + + ' with ({});' + + ' return function() {' + + ' void ["abcdefghijklmnopqrstuvwxyz",' + + ' "abcdefghijklmnopqrstuvwxyz"];' + + ' };' + + '}());', + sOutput: + '(function() {' + + ' with ({});' + + ' return function() {' + + ' var a = "abcdefghijklmnopqrstuvwxyz";' + + ' void [a, a];' + + ' };' + + '}());' + }, + { + sTitle: + 'Consolidation within a return statement excluded in function ' + + 'code due to the presence of a direct call to the eval function.', + sInput: + '/*jshint evil:true */' + + '(function() {' + + ' eval("");' + + ' return function() {' + + ' void ["abcdefghijklmnopqrstuvwxyz",' + + ' "abcdefghijklmnopqrstuvwxyz"];' + + ' };' + + '}());', + sOutput: + '/*jshint evil:true */' + + '(function() {' + + ' eval("");' + + ' return function() {' + + ' var a = "abcdefghijklmnopqrstuvwxyz";' + + ' void [a, a];' + + ' };' + + '}());' + }, + { + sTitle: + 'Inclusion of a return statement in function code that contains ' + + 'no with statement and no direct call to the eval function.', + sInput: + '(function() {' + + ' void ["abcdefghijklmnopqrstuvwxyz"];' + + ' if (0 === Math.random()) {' + + ' return;' + + ' } else {' + + ' }' + + ' void ["abcdefghijklmnopqrstuvwxyz"];' + + '}());', + sOutput: + '(function() {' + + ' var a = "abcdefghijklmnopqrstuvwxyz";' + + ' void [a];' + + ' if (0 === Math.random()) {' + + ' return;' + + ' } else {' + + ' }' + + ' void [a];' + + '}());' + }, + // 12.10 The {@code with} Statement. + { + sTitle: + 'Preservation of the statement in a with statement.', + sInput: + 'with ({}) {' + + ' void ["abcdefghijklmnopqrstuvwxyz",' + + ' "abcdefghijklmnopqrstuvwxyz"];' + + '}' + }, + { + sTitle: + 'Exclusion of a with statement in the same syntactic code unit.', + sInput: + 'void ["abcdefghijklmnopqrstuvwxyz"];' + + 'with ({' + + ' foo: "abcdefghijklmnopqrstuvwxyz",' + + ' bar: "abcdefghijklmnopqrstuvwxyz"' + + '}) {' + + ' void ["abcdefghijklmnopqrstuvwxyz",' + + ' "abcdefghijklmnopqrstuvwxyz"];' + + '}' + + 'void ["abcdefghijklmnopqrstuvwxyz"];' + }, + { + sTitle: + 'Exclusion of a with statement in nested function code.', + sInput: + 'void ["abcdefghijklmnopqrstuvwxyz"];' + + '(function() {' + + ' with ({' + + ' foo: "abcdefghijklmnopqrstuvwxyz",' + + ' bar: "abcdefghijklmnopqrstuvwxyz"' + + ' }) {' + + ' void ["abcdefghijklmnopqrstuvwxyz",' + + ' "abcdefghijklmnopqrstuvwxyz"];' + + ' }' + + '}());' + + 'void ["abcdefghijklmnopqrstuvwxyz"];' + }, + // 12.12 Labelled Statements. + { + sTitle: + 'Preservation of the label of a labelled statement.', + sInput: + 'abcdefghijklmnopqrstuvwxyz: for (; 0 === Math.random(););' + + 'abcdefghijklmnopqrstuvwxyz: for (; 0 === Math.random(););' + }, + // 12.14 The {@code try} Statement. + { + sTitle: + 'Preservation of the identifier in the catch clause of a try' + + 'statement.', + sInput: + 'try {' + + '} catch (abcdefghijklmnopqrstuvwxyz) {' + + '} finally {' + + '}' + + 'try {' + + '} catch (abcdefghijklmnopqrstuvwxyz) {' + + '} finally {' + + '}' + }, + // 13 Function Definition. + { + sTitle: + 'Preservation of the identifier of a function declaration.', + sInput: + 'function abcdefghijklmnopqrstuvwxyz() {' + + '}' + + 'void [abcdefghijklmnopqrstuvwxyz];' + }, + { + sTitle: + 'Preservation of the identifier of a function expression.', + sInput: + 'void [' + + ' function abcdefghijklmnopqrstuvwxyz() {' + + ' },' + + ' function abcdefghijklmnopqrstuvwxyz() {' + + ' }' + + '];' + }, + { + sTitle: + 'Preservation of a formal parameter of a function declaration.', + sInput: + 'function foo(abcdefghijklmnopqrstuvwxyz) {' + + '}' + + 'function bar(abcdefghijklmnopqrstuvwxyz) {' + + '}' + }, + { + sTitle: + 'Preservation of a formal parameter in a function expression.', + sInput: + 'void [' + + ' function(abcdefghijklmnopqrstuvwxyz) {' + + ' },' + + ' function(abcdefghijklmnopqrstuvwxyz) {' + + ' }' + + '];' + }, + { + sTitle: + 'Exclusion of a function declaration.', + sInput: + 'void ["abcdefghijklmnopqrstuvwxyz"];' + + 'function foo() {' + + '}' + + 'void ["abcdefghijklmnopqrstuvwxyz"];' + }, + { + sTitle: + 'Consolidation within a function declaration.', + sInput: + 'function foo() {' + + ' void ["abcdefghijklmnopqrstuvwxyz",' + + ' "abcdefghijklmnopqrstuvwxyz"];' + + '}', + sOutput: + 'function foo() {' + + ' var a = "abcdefghijklmnopqrstuvwxyz";' + + ' void [a, a];' + + '}' + }, + // 14 Program. + { + sTitle: + 'Preservation of a program without source elements.', + sInput: + '' + }, + // 14.1 Directive Prologues and the Use Strict Directive. + { + sTitle: + 'Preservation of a Directive Prologue in global code.', + sInput: + '"abcdefghijklmnopqrstuvwxyz";' + + '\'zyxwvutsrqponmlkjihgfedcba\';' + }, + { + sTitle: + 'Preservation of a Directive Prologue in a function declaration.', + sInput: + 'function foo() {' + + ' "abcdefghijklmnopqrstuvwxyz";' + + ' \'zyxwvutsrqponmlkjihgfedcba\';' + + '}' + }, + { + sTitle: + 'Preservation of a Directive Prologue in a function expression.', + sInput: + 'void [' + + ' function() {' + + ' "abcdefghijklmnopqrstuvwxyz";' + + ' \'zyxwvutsrqponmlkjihgfedcba\';' + + ' }' + + '];' + }, + { + sTitle: + 'Ignorance with regard to a Directive Prologue in global code.', + sInput: + '"abcdefghijklmnopqrstuvwxyz";' + + 'void ["abcdefghijklmnopqrstuvwxyz",' + + ' "abcdefghijklmnopqrstuvwxyz"];', + sOutput: + '"abcdefghijklmnopqrstuvwxyz";' + + '(function() {' + + ' var a = "abcdefghijklmnopqrstuvwxyz";' + + ' void [a, a];' + + '}());' + }, + { + sTitle: + 'Ignorance with regard to a Directive Prologue in a function' + + 'declaration.', + sInput: + 'function foo() {' + + ' "abcdefghijklmnopqrstuvwxyz";' + + ' void ["abcdefghijklmnopqrstuvwxyz",' + + ' "abcdefghijklmnopqrstuvwxyz"];' + + '}', + sOutput: + 'function foo() {' + + ' "abcdefghijklmnopqrstuvwxyz";' + + ' var a = "abcdefghijklmnopqrstuvwxyz";' + + ' void [a, a];' + + '}' + }, + { + sTitle: + 'Ignorance with regard to a Directive Prologue in a function' + + 'expression.', + sInput: + '(function() {' + + ' "abcdefghijklmnopqrstuvwxyz";' + + ' void ["abcdefghijklmnopqrstuvwxyz",' + + ' "abcdefghijklmnopqrstuvwxyz"];' + + '}());', + sOutput: + '(function() {' + + ' "abcdefghijklmnopqrstuvwxyz";' + + ' var a = "abcdefghijklmnopqrstuvwxyz";' + + ' void [a, a];' + + '}());' + }, + // 15.1 The Global Object. + { + sTitle: + 'Preservation of a property of the global object.', + sInput: + 'void [undefined, undefined, undefined, undefined, undefined];' + }, + // 15.1.2.1.1 Direct Call to Eval. + { + sTitle: + 'Exclusion of a direct call to the eval function in the same ' + + 'syntactic code unit.', + sInput: + '/*jshint evil:true */' + + 'void ["abcdefghijklmnopqrstuvwxyz"];' + + 'eval("");' + + 'void ["abcdefghijklmnopqrstuvwxyz"];' + }, + { + sTitle: + 'Exclusion of a direct call to the eval function in nested ' + + 'function code.', + sInput: + '/*jshint evil:true */' + + 'void ["abcdefghijklmnopqrstuvwxyz"];' + + '(function() {' + + ' eval("");' + + '}());' + + 'void ["abcdefghijklmnopqrstuvwxyz"];' + }, + { + sTitle: + 'Consolidation within a direct call to the eval function.', + sInput: + '/*jshint evil:true */' + + 'eval(function() {' + + ' void ["abcdefghijklmnopqrstuvwxyz",' + + ' "abcdefghijklmnopqrstuvwxyz"];' + + '}());', + sOutput: + '/*jshint evil:true */' + + 'eval(function() {' + + ' var a = "abcdefghijklmnopqrstuvwxyz";' + + ' void [a, a];' + + '}());' + }, + // Consolidation proper. + { + sTitle: + 'No consolidation if it does not result in a reduction of the ' + + 'number of source characters.', + sInput: + '(function() {' + + ' var foo;' + + ' void ["ab", "ab", "abc", "abc"];' + + '}());' + }, + { + sTitle: + 'Identification of a range of source elements at the beginning ' + + 'of global code.', + sInput: + '/*jshint evil:true */' + + '"abcdefghijklmnopqrstuvwxyz";' + + 'void ["abcdefghijklmnopqrstuvwxyz",' + + ' "abcdefghijklmnopqrstuvwxyz"];' + + 'eval("");', + sOutput: + '/*jshint evil:true */' + + '"abcdefghijklmnopqrstuvwxyz";' + + '(function() {' + + ' var a = "abcdefghijklmnopqrstuvwxyz";' + + ' void [a, a];' + + '}());' + + 'eval("");' + }, + { + sTitle: + 'Identification of a range of source elements in the middle of ' + + 'global code.', + sInput: + '/*jshint evil:true */' + + '"abcdefghijklmnopqrstuvwxyz";' + + 'eval("");' + + 'void ["abcdefghijklmnopqrstuvwxyz",' + + ' "abcdefghijklmnopqrstuvwxyz"];' + + 'eval("");', + sOutput: + '/*jshint evil:true */' + + '"abcdefghijklmnopqrstuvwxyz";' + + 'eval("");' + + '(function() {' + + ' var a = "abcdefghijklmnopqrstuvwxyz";' + + ' void [a, a];' + + '}());' + + 'eval("");' + }, + { + sTitle: + 'Identification of a range of source elements at the end of ' + + 'global code.', + sInput: + '/*jshint evil:true */' + + '"abcdefghijklmnopqrstuvwxyz";' + + 'eval("");' + + 'void ["abcdefghijklmnopqrstuvwxyz",' + + ' "abcdefghijklmnopqrstuvwxyz"];', + sOutput: + '/*jshint evil:true */' + + '"abcdefghijklmnopqrstuvwxyz";' + + 'eval("");' + + '(function() {' + + ' var a = "abcdefghijklmnopqrstuvwxyz";' + + ' void [a, a];' + + '}());' + }, + { + sTitle: + 'Identification of a range of source elements at the beginning ' + + 'of function code.', + sInput: + '/*jshint evil:true */' + + '(function() {' + + ' "abcdefghijklmnopqrstuvwxyz";' + + ' void ["abcdefghijklmnopqrstuvwxyz",' + + ' "abcdefghijklmnopqrstuvwxyz"];' + + ' eval("");' + + '}());', + sOutput: + '/*jshint evil:true */' + + '(function() {' + + ' "abcdefghijklmnopqrstuvwxyz";' + + ' (function() {' + + ' var a = "abcdefghijklmnopqrstuvwxyz";' + + ' void [a, a];' + + ' }());' + + ' eval("");' + + '}());' + }, + { + sTitle: + 'Identification of a range of source elements in the middle of ' + + 'function code.', + sInput: + '/*jshint evil:true */' + + '(function() {' + + ' "abcdefghijklmnopqrstuvwxyz";' + + ' eval("");' + + ' void ["abcdefghijklmnopqrstuvwxyz",' + + ' "abcdefghijklmnopqrstuvwxyz"];' + + ' eval("");' + + '}());', + sOutput: + '/*jshint evil:true */' + + '(function() {' + + ' "abcdefghijklmnopqrstuvwxyz";' + + ' eval("");' + + ' (function() {' + + ' var a = "abcdefghijklmnopqrstuvwxyz";' + + ' void [a, a];' + + ' }());' + + ' eval("");' + + '}());' + }, + { + sTitle: + 'Identification of a range of source elements at the end of ' + + 'function code.', + sInput: + '/*jshint evil:true */' + + '(function() {' + + ' "abcdefghijklmnopqrstuvwxyz";' + + ' eval("");' + + ' void ["abcdefghijklmnopqrstuvwxyz",' + + ' "abcdefghijklmnopqrstuvwxyz"];' + + '}());', + sOutput: + '/*jshint evil:true */' + + '(function() {' + + ' "abcdefghijklmnopqrstuvwxyz";' + + ' eval("");' + + ' (function() {' + + ' var a = "abcdefghijklmnopqrstuvwxyz";' + + ' void [a, a];' + + ' }());' + + '}());' + }, + { + sTitle: + 'Evaluation with regard to String values of String literals and ' + + 'String values derived from identifier names used as property' + + 'accessors.', + sInput: + '(function() {' + + ' var foo;' + + ' void ["abcdefg", Math.abcdefg, "abcdef", Math.abcdef];' + + '}());', + sOutput: + '(function() {' + + ' var a = "abcdefg", foo;' + + ' void [a, Math[a], "abcdef", Math.abcdef];' + + '}());' + }, + { + sTitle: + 'Evaluation with regard to the necessity of adding a variable ' + + 'statement.', + sInput: + '/*jshint evil:true */' + + '(function() {' + + ' void ["abcdefgh", "abcdefgh"];' + + '}());' + + 'eval("");' + + '(function() {' + + ' void ["abcdefg", "abcdefg"];' + + '}());' + + 'eval("");' + + '(function() {' + + ' var foo;' + + ' void ["abcd", "abcd"];' + + '}());', + sOutput: + '/*jshint evil:true */' + + '(function() {' + + ' var a = "abcdefgh";' + + ' void [a, a];' + + '}());' + + 'eval("");' + + '(function() {' + + ' void ["abcdefg", "abcdefg"];' + + '}());' + + 'eval("");' + + '(function() {' + + ' var a = "abcd", foo;' + + ' void [a, a];' + + '}());' + }, + { + sTitle: + 'Evaluation with regard to the necessity of enclosing source ' + + 'elements.', + sInput: + '/*jshint evil:true */' + + 'void ["abcdefghijklmnopqrstuvwxy", "abcdefghijklmnopqrstuvwxy"];' + + 'eval("");' + + 'void ["abcdefghijklmnopqrstuvwx", "abcdefghijklmnopqrstuvwx"];' + + 'eval("");' + + '(function() {' + + ' void ["abcdefgh", "abcdefgh"];' + + '}());' + + '(function() {' + + ' void ["abcdefghijklmnopqrstuvwxy",' + + ' "abcdefghijklmnopqrstuvwxy"];' + + ' eval("");' + + ' void ["abcdefghijklmnopqrstuvwx",' + + ' "abcdefghijklmnopqrstuvwx"];' + + ' eval("");' + + ' (function() {' + + ' void ["abcdefgh", "abcdefgh"];' + + ' }());' + + '}());', + sOutput: + '/*jshint evil:true */' + + '(function() {' + + ' var a = "abcdefghijklmnopqrstuvwxy";' + + ' void [a, a];' + + '}());' + + 'eval("");' + + 'void ["abcdefghijklmnopqrstuvwx", "abcdefghijklmnopqrstuvwx"];' + + 'eval("");' + + '(function() {' + + ' var a = "abcdefgh";' + + ' void [a, a];' + + '}());' + + '(function() {' + + ' (function() {' + + ' var a = "abcdefghijklmnopqrstuvwxy";' + + ' void [a, a];' + + ' }());' + + ' eval("");' + + ' void ["abcdefghijklmnopqrstuvwx", "abcdefghijklmnopqrstuvwx"];' + + ' eval("");' + + ' (function() {' + + ' var a = "abcdefgh";' + + ' void [a, a];' + + ' }());' + + '}());' + }, + { + sTitle: + 'Employment of a closure while consolidating in global code.', + sInput: + 'void ["abcdefghijklmnopqrstuvwxyz",' + + ' "abcdefghijklmnopqrstuvwxyz"];', + sOutput: + '(function() {' + + ' var a = "abcdefghijklmnopqrstuvwxyz";' + + ' void [a, a];' + + '}());' + }, + { + sTitle: + 'Assignment of a shorter identifier to a value whose ' + + 'consolidation results in a greater reduction of the number of ' + + 'source characters.', + sInput: + '(function() {' + + ' var b, c, d, e, f, g, h, i, j, k, l, m,' + + ' n, o, p, q, r, s, t, u, v, w, x, y, z,' + + ' A, B, C, D, E, F, G, H, I, J, K, L, M,' + + ' N, O, P, Q, R, S, T, U, V, W, X, Y, Z,' + + ' $, _;' + + ' void ["abcde", "abcde", "edcba", "edcba", "edcba"];' + + '}());', + sOutput: + '(function() {' + + ' var a = "edcba",' + + ' b, c, d, e, f, g, h, i, j, k, l, m,' + + ' n, o, p, q, r, s, t, u, v, w, x, y, z,' + + ' A, B, C, D, E, F, G, H, I, J, K, L, M,' + + ' N, O, P, Q, R, S, T, U, V, W, X, Y, Z,' + + ' $, _;' + + ' void ["abcde", "abcde", a, a, a];' + + '}());' + } +].forEach(cAssert); + +process.exit(bTestsPassed ? 0 : 1); + diff --git a/node_modules/uglify-js/test/testparser.js b/node_modules/uglify-js/test/testparser.js new file mode 100755 index 00000000..a0f9f28f --- /dev/null +++ b/node_modules/uglify-js/test/testparser.js @@ -0,0 +1,409 @@ +#! /usr/bin/env node +global.DIGITS_OVERRIDE_FOR_TESTING = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ$_0123456789"; + +var parseJS = require("../lib/parse-js"); +var sys = require("util"); + +// write debug in a very straightforward manner +var debug = function(){ + sys.log(Array.prototype.slice.call(arguments).join(', ')); +}; + +var testsPassed = true; + +ParserTestSuite(function(i, input, desc){ + try { + parseJS.parse(input); + debug("ok " + i + ": " + desc); + } catch(e){ + debug("FAIL " + i + " " + desc + " (" + e + ")"); + testsPassed = false; + } +}); + +process.exit(testsPassed ? 0 : 1); + +function ParserTestSuite(callback){ + var inps = [ + ["var abc;", "Regular variable statement w/o assignment"], + ["var abc = 5;", "Regular variable statement with assignment"], + ["/* */;", "Multiline comment"], + ['/** **/;', 'Double star multiline comment'], + ["var f = function(){;};", "Function expression in var assignment"], + ['hi; // moo\n;', 'single line comment'], + ['var varwithfunction;', 'Dont match keywords as substrings'], // difference between `var withsomevar` and `"str"` (local search and lits) + ['a + b;', 'addition'], + ["'a';", 'single string literal'], + ["'a\\n';", 'single string literal with escaped return'], + ['"a";', 'double string literal'], + ['"a\\n";', 'double string literal with escaped return'], + ['"var";', 'string is a keyword'], + ['"variable";', 'string starts with a keyword'], + ['"somevariable";', 'string contains a keyword'], + ['"somevar";', 'string ends with a keyword'], + ['500;', 'int literal'], + ['500.;', 'float literal w/o decimals'], + ['500.432;', 'float literal with decimals'], + ['.432432;', 'float literal w/o int'], + ['(a,b,c);', 'parens and comma'], + ['[1,2,abc];', 'array literal'], + ['var o = {a:1};', 'object literal unquoted key'], + ['var o = {"b":2};', 'object literal quoted key'], // opening curly may not be at the start of a statement... + ['var o = {c:c};', 'object literal keyname is identifier'], + ['var o = {a:1,"b":2,c:c};', 'object literal combinations'], + ['var x;\nvar y;', 'two lines'], + ['var x;\nfunction n(){; }', 'function def'], + ['var x;\nfunction n(abc){; }', 'function def with arg'], + ['var x;\nfunction n(abc, def){ ;}', 'function def with args'], + ['function n(){ "hello"; }', 'function def with body'], + ['/a/;', 'regex literal'], + ['/a/b;', 'regex literal with flag'], + ['/a/ / /b/;', 'regex div regex'], + ['a/b/c;', 'triple division looks like regex'], + ['+function(){/regex/;};', 'regex at start of function body'], + // http://code.google.com/p/es-lab/source/browse/trunk/tests/parser/parsertests.js?r=86 + // http://code.google.com/p/es-lab/source/browse/trunk/tests/parser/parsertests.js?r=430 + + // first tests for the lexer, should also parse as program (when you append a semi) + + // comments + ['//foo!@#^&$1234\nbar;', 'single line comment'], + ['/* abcd!@#@$* { } && null*/;', 'single line multi line comment'], + ['/*foo\nbar*/;','multi line comment'], + ['/*x*x*/;','multi line comment with *'], + ['/**/;','empty comment'], + // identifiers + ["x;",'1 identifier'], + ["_x;",'2 identifier'], + ["xyz;",'3 identifier'], + ["$x;",'4 identifier'], + ["x$;",'5 identifier'], + ["_;",'6 identifier'], + ["x5;",'7 identifier'], + ["x_y;",'8 identifier'], + ["x+5;",'9 identifier'], + ["xyz123;",'10 identifier'], + ["x1y1z1;",'11 identifier'], + ["foo\\u00D8bar;",'12 identifier unicode escape'], + //["foo�bar;",'13 identifier unicode embedded (might fail)'], + // numbers + ["5;", '1 number'], + ["5.5;", '2 number'], + ["0;", '3 number'], + ["0.0;", '4 number'], + ["0.001;", '5 number'], + ["1.e2;", '6 number'], + ["1.e-2;", '7 number'], + ["1.E2;", '8 number'], + ["1.E-2;", '9 number'], + [".5;", '10 number'], + [".5e3;", '11 number'], + [".5e-3;", '12 number'], + ["0.5e3;", '13 number'], + ["55;", '14 number'], + ["123;", '15 number'], + ["55.55;", '16 number'], + ["55.55e10;", '17 number'], + ["123.456;", '18 number'], + ["1+e;", '20 number'], + ["0x01;", '22 number'], + ["0XCAFE;", '23 number'], + ["0x12345678;", '24 number'], + ["0x1234ABCD;", '25 number'], + ["0x0001;", '26 number'], + // strings + ["\"foo\";", '1 string'], + ["\'foo\';", '2 string'], + ["\"x\";", '3 string'], + ["\'\';", '4 string'], + ["\"foo\\tbar\";", '5 string'], + ["\"!@#$%^&*()_+{}[]\";", '6 string'], + ["\"/*test*/\";", '7 string'], + ["\"//test\";", '8 string'], + ["\"\\\\\";", '9 string'], + ["\"\\u0001\";", '10 string'], + ["\"\\uFEFF\";", '11 string'], + ["\"\\u10002\";", '12 string'], + ["\"\\x55\";", '13 string'], + ["\"\\x55a\";", '14 string'], + ["\"a\\\\nb\";", '15 string'], + ['";"', '16 string: semi in a string'], + ['"a\\\nb";', '17 string: line terminator escape'], + // literals + ["null;", "null"], + ["true;", "true"], + ["false;", "false"], + // regex + ["/a/;", "1 regex"], + ["/abc/;", "2 regex"], + ["/abc[a-z]*def/g;", "3 regex"], + ["/\\b/;", "4 regex"], + ["/[a-zA-Z]/;", "5 regex"], + + // program tests (for as far as they havent been covered above) + + // regexp + ["/foo(.*)/g;", "another regexp"], + // arrays + ["[];", "1 array"], + ["[ ];", "2 array"], + ["[1];", "3 array"], + ["[1,2];", "4 array"], + ["[1,2,,];", "5 array"], + ["[1,2,3];", "6 array"], + ["[1,2,3,,,];", "7 array"], + // objects + ["{};", "1 object"], + ["({x:5});", "2 object"], + ["({x:5,y:6});", "3 object"], + ["({x:5,});", "4 object"], + ["({if:5});", "5 object"], + ["({ get x() {42;} });", "6 object"], + ["({ set y(a) {1;} });", "7 object"], + // member expression + ["o.m;", "1 member expression"], + ["o['m'];", "2 member expression"], + ["o['n']['m'];", "3 member expression"], + ["o.n.m;", "4 member expression"], + ["o.if;", "5 member expression"], + // call and invoke expressions + ["f();", "1 call/invoke expression"], + ["f(x);", "2 call/invoke expression"], + ["f(x,y);", "3 call/invoke expression"], + ["o.m();", "4 call/invoke expression"], + ["o['m'];", "5 call/invoke expression"], + ["o.m(x);", "6 call/invoke expression"], + ["o['m'](x);", "7 call/invoke expression"], + ["o.m(x,y);", "8 call/invoke expression"], + ["o['m'](x,y);", "9 call/invoke expression"], + ["f(x)(y);", "10 call/invoke expression"], + ["f().x;", "11 call/invoke expression"], + + // eval + ["eval('x');", "1 eval"], + ["(eval)('x');", "2 eval"], + ["(1,eval)('x');", "3 eval"], + ["eval(x,y);", "4 eval"], + // new expression + ["new f();", "1 new expression"], + ["new o;", "2 new expression"], + ["new o.m;", "3 new expression"], + ["new o.m(x);", "4 new expression"], + ["new o.m(x,y);", "5 new expression"], + // prefix/postfix + ["++x;", "1 pre/postfix"], + ["x++;", "2 pre/postfix"], + ["--x;", "3 pre/postfix"], + ["x--;", "4 pre/postfix"], + ["x ++;", "5 pre/postfix"], + ["x /* comment */ ++;", "6 pre/postfix"], + ["++ /* comment */ x;", "7 pre/postfix"], + // unary operators + ["delete x;", "1 unary operator"], + ["void x;", "2 unary operator"], + ["+ x;", "3 unary operator"], + ["-x;", "4 unary operator"], + ["~x;", "5 unary operator"], + ["!x;", "6 unary operator"], + // meh + ["new Date++;", "new date ++"], + ["+x++;", " + x ++"], + // expression expressions + ["1 * 2;", "1 expression expressions"], + ["1 / 2;", "2 expression expressions"], + ["1 % 2;", "3 expression expressions"], + ["1 + 2;", "4 expression expressions"], + ["1 - 2;", "5 expression expressions"], + ["1 << 2;", "6 expression expressions"], + ["1 >>> 2;", "7 expression expressions"], + ["1 >> 2;", "8 expression expressions"], + ["1 * 2 + 3;", "9 expression expressions"], + ["(1+2)*3;", "10 expression expressions"], + ["1*(2+3);", "11 expression expressions"], + ["xy;", "13 expression expressions"], + ["x<=y;", "14 expression expressions"], + ["x>=y;", "15 expression expressions"], + ["x instanceof y;", "16 expression expressions"], + ["x in y;", "17 expression expressions"], + ["x&y;", "18 expression expressions"], + ["x^y;", "19 expression expressions"], + ["x|y;", "20 expression expressions"], + ["x+y>>= y;", "1 assignment"], + ["x <<= y;", "2 assignment"], + ["x = y;", "3 assignment"], + ["x += y;", "4 assignment"], + ["x /= y;", "5 assignment"], + // comma + ["x, y;", "comma"], + // block + ["{};", "1 block"], + ["{x;};", "2 block"], + ["{x;y;};", "3 block"], + // vars + ["var x;", "1 var"], + ["var x,y;", "2 var"], + ["var x=1,y=2;", "3 var"], + ["var x,y=2;", "4 var"], + // empty + [";", "1 empty"], + ["\n;", "2 empty"], + // expression statement + ["x;", "1 expression statement"], + ["5;", "2 expression statement"], + ["1+2;", "3 expression statement"], + // if + ["if (c) x; else y;", "1 if statement"], + ["if (c) x;", "2 if statement"], + ["if (c) {} else {};", "3 if statement"], + ["if (c1) if (c2) s1; else s2;", "4 if statement"], + // while + ["do s; while (e);", "1 while statement"], + ["do { s; } while (e);", "2 while statement"], + ["while (e) s;", "3 while statement"], + ["while (e) { s; };", "4 while statement"], + // for + ["for (;;) ;", "1 for statement"], + ["for (;c;x++) x;", "2 for statement"], + ["for (i;i> 1; +var c = 8 >>> 1; \ No newline at end of file diff --git a/node_modules/uglify-js/test/unit/compress/test/issue34.js b/node_modules/uglify-js/test/unit/compress/test/issue34.js new file mode 100644 index 00000000..022f7a31 --- /dev/null +++ b/node_modules/uglify-js/test/unit/compress/test/issue34.js @@ -0,0 +1,3 @@ +var a = {}; +a["this"] = 1; +a["that"] = 2; \ No newline at end of file diff --git a/node_modules/uglify-js/test/unit/compress/test/issue349.js b/node_modules/uglify-js/test/unit/compress/test/issue349.js new file mode 100644 index 00000000..71241f66 --- /dev/null +++ b/node_modules/uglify-js/test/unit/compress/test/issue349.js @@ -0,0 +1,25 @@ +(function () { + var cake + if (noFreeCakes) return /* I would + insert something + there, but I'm sort + of lazy so whatever. + */ cake = new FreeCake() + return cake +})() + +(function () { + var cake + if (noFreeCakes) return /* I would insert something there, */ /* + but I'm sort of lazy so + */ /* whatever. */ cake = new FreeCake() + return cake +})() + +(function () { + var cake + if (noFreeCakes) return // I would insert something there, but I'm sort of lazy so whatever. + cake = new FreeCake() + return cake +})() + diff --git a/node_modules/uglify-js/test/unit/compress/test/issue353.js b/node_modules/uglify-js/test/unit/compress/test/issue353.js new file mode 100644 index 00000000..11ec1165 --- /dev/null +++ b/node_modules/uglify-js/test/unit/compress/test/issue353.js @@ -0,0 +1,4 @@ +function test() { + debugger; + return; +} diff --git a/node_modules/uglify-js/test/unit/compress/test/issue372.js b/node_modules/uglify-js/test/unit/compress/test/issue372.js new file mode 100644 index 00000000..3791ce88 --- /dev/null +++ b/node_modules/uglify-js/test/unit/compress/test/issue372.js @@ -0,0 +1,32 @@ +"use strict"; +"use strict"; + +function a() { + void "a"; + function b() { + "use strict"; + void "b"; + function c() { + "use strict"; + void "c"; + } + function d() { + "salmon"; + void "d"; + } + } + function e() { + "salmon"; + void "e"; + function f() { + "use strict"; + "salmon"; + void "f"; + for (var i = 0; i < 10; i++) var g = function() { + "use strict"; + "salmon"; + void "g"; + }(); + } + } +}; diff --git a/node_modules/uglify-js/test/unit/compress/test/issue4.js b/node_modules/uglify-js/test/unit/compress/test/issue4.js new file mode 100644 index 00000000..0b761037 --- /dev/null +++ b/node_modules/uglify-js/test/unit/compress/test/issue4.js @@ -0,0 +1,3 @@ +var a = 2e3; +var b = 2e-3; +var c = 2e-5; \ No newline at end of file diff --git a/node_modules/uglify-js/test/unit/compress/test/issue48.js b/node_modules/uglify-js/test/unit/compress/test/issue48.js new file mode 100644 index 00000000..031e85b3 --- /dev/null +++ b/node_modules/uglify-js/test/unit/compress/test/issue48.js @@ -0,0 +1 @@ +var s, i; s = ''; i = 0; \ No newline at end of file diff --git a/node_modules/uglify-js/test/unit/compress/test/issue50.js b/node_modules/uglify-js/test/unit/compress/test/issue50.js new file mode 100644 index 00000000..060f9df8 --- /dev/null +++ b/node_modules/uglify-js/test/unit/compress/test/issue50.js @@ -0,0 +1,9 @@ +function bar(a) { + try { + foo(); + } catch(e) { + alert("Exception caught (foo not defined)"); + } + alert(a); // 10 in FF, "[object Error]" in IE +} +bar(10); diff --git a/node_modules/uglify-js/test/unit/compress/test/issue53.js b/node_modules/uglify-js/test/unit/compress/test/issue53.js new file mode 100644 index 00000000..4f8b32f1 --- /dev/null +++ b/node_modules/uglify-js/test/unit/compress/test/issue53.js @@ -0,0 +1 @@ +x = (y, z) diff --git a/node_modules/uglify-js/test/unit/compress/test/issue54.1.js b/node_modules/uglify-js/test/unit/compress/test/issue54.1.js new file mode 100644 index 00000000..967052e8 --- /dev/null +++ b/node_modules/uglify-js/test/unit/compress/test/issue54.1.js @@ -0,0 +1,3 @@ +foo.toString(); +a.toString(16); +b.toString.call(c); diff --git a/node_modules/uglify-js/test/unit/compress/test/issue68.js b/node_modules/uglify-js/test/unit/compress/test/issue68.js new file mode 100644 index 00000000..14054d01 --- /dev/null +++ b/node_modules/uglify-js/test/unit/compress/test/issue68.js @@ -0,0 +1,5 @@ +function f() { + if (a) return; + g(); + function g(){} +}; diff --git a/node_modules/uglify-js/test/unit/compress/test/issue69.js b/node_modules/uglify-js/test/unit/compress/test/issue69.js new file mode 100644 index 00000000..d25ecd67 --- /dev/null +++ b/node_modules/uglify-js/test/unit/compress/test/issue69.js @@ -0,0 +1 @@ +[(a,b)] diff --git a/node_modules/uglify-js/test/unit/compress/test/issue9.js b/node_modules/uglify-js/test/unit/compress/test/issue9.js new file mode 100644 index 00000000..61588614 --- /dev/null +++ b/node_modules/uglify-js/test/unit/compress/test/issue9.js @@ -0,0 +1,4 @@ +var a = { + a: 1, + b: 2, // <-- trailing comma +}; diff --git a/node_modules/uglify-js/test/unit/compress/test/issues222_324.js b/node_modules/uglify-js/test/unit/compress/test/issues222_324.js new file mode 100644 index 00000000..58b0d3c7 --- /dev/null +++ b/node_modules/uglify-js/test/unit/compress/test/issues222_324.js @@ -0,0 +1,82 @@ +!function() { + 'use strict'; + + var 七, // issue #222 + $1, // issue #222 + ლ_ಠ益ಠ_ლ; // issue #324 +}; + +!function() { + 'use strict'; + + // test if Unicode canonical equivalence is ignored + var \u006E\u0303, + \u00F1; +}; + +!function() { + 'use strict'; + + var \uFF38, // X Lu (third to last) + \uFF58, // x Ll (third to last) + \u1FBC, // ᾼ Lt (third to last) + \uFF70, // ー Lm (third to last) + \u4DB3, // 䶳 Lo (third to last within a range) + \u97CA, // 韊 Lo (third to last within a range) + \uD7A1, // 힡 Lo (third to last within a range) + \uFFDA, // ᅳ Lo (third to last) + \uA6ED, // ꛭ Nl (third to last) + \u0024, // $ + \u005F, // _ + \u0024\uFF38, // X Lu (third to last) + \u0024\uFF58, // x Ll (third to last) + \u0024\u1FBC, // ᾼ Lt (third to last) + \u0024\uFF70, // ー Lm (third to last) + \u0024\u4DB3, // 䶳 Lo (third to last within a range) + \u0024\u97CA, // 韊 Lo (third to last within a range) + \u0024\uD7A1, // 힡 Lo (third to last within a range) + \u0024\uFFDA, // ᅳ Lo (third to last) + \u0024\uA6ED, // ꛭ Nl (third to last) + \u0024\uFE24, // ︤ Mn (third to last) + \u0024\uABE9, // ꯩ Mc (third to last) + \u0024\uFF17, // 7 Nd (third to last) + \u0024\uFE4E, // ﹎ Pc (third to last) + \u0024\u200C, // ZERO WIDTH NON-JOINER + \u0024\u200D, // ZERO WIDTH JOINER + \u0024\u0024, // $ + \u0024\u005F; // _ +}; + +!function() { + 'use strict'; + + var X, + x, + ᾼ, + ー, + 䶳, + 韊, + 힡, + ᅳ, + ꛭ, + $, + _, + $X, + $x, + $ᾼ, + $ー, + $䶳, + $韊, + $힡, + $ᅳ, + $ꛭ, + $︤, + $ꯩ, + $7, + $﹎, + $‌, + $‍, + $$, + $_; +}; + diff --git a/node_modules/uglify-js/test/unit/compress/test/mangle.js b/node_modules/uglify-js/test/unit/compress/test/mangle.js new file mode 100644 index 00000000..c271a26d --- /dev/null +++ b/node_modules/uglify-js/test/unit/compress/test/mangle.js @@ -0,0 +1,5 @@ +(function() { + var x = function fun(a, fun, b) { + return fun; + }; +}()); diff --git a/node_modules/uglify-js/test/unit/compress/test/null_string.js b/node_modules/uglify-js/test/unit/compress/test/null_string.js new file mode 100644 index 00000000..a675b1c5 --- /dev/null +++ b/node_modules/uglify-js/test/unit/compress/test/null_string.js @@ -0,0 +1 @@ +var nullString = "\0" \ No newline at end of file diff --git a/node_modules/uglify-js/test/unit/compress/test/strict-equals.js b/node_modules/uglify-js/test/unit/compress/test/strict-equals.js new file mode 100644 index 00000000..b631f4c3 --- /dev/null +++ b/node_modules/uglify-js/test/unit/compress/test/strict-equals.js @@ -0,0 +1,3 @@ +typeof a === 'string' +b + "" !== c + "" +d < e === f < g diff --git a/node_modules/uglify-js/test/unit/compress/test/var.js b/node_modules/uglify-js/test/unit/compress/test/var.js new file mode 100644 index 00000000..609a35d2 --- /dev/null +++ b/node_modules/uglify-js/test/unit/compress/test/var.js @@ -0,0 +1,3 @@ +// var declarations after each other should be combined +var a = 1; +var b = 2; \ No newline at end of file diff --git a/node_modules/uglify-js/test/unit/compress/test/whitespace.js b/node_modules/uglify-js/test/unit/compress/test/whitespace.js new file mode 100644 index 00000000..6a15c464 --- /dev/null +++ b/node_modules/uglify-js/test/unit/compress/test/whitespace.js @@ -0,0 +1,21 @@ +function id(a) { + // Form-Feed + // Vertical Tab + // No-Break Space + ᠎// Mongolian Vowel Separator +  // En quad +  // Em quad +  // En space +  // Em space +  // Three-Per-Em Space +  // Four-Per-Em Space +  // Six-Per-Em Space +  // Figure Space +  // Punctuation Space +  // Thin Space +  // Hair Space +  // Narrow No-Break Space +  // Medium Mathematical Space +  // Ideographic Space + return a; +} diff --git a/node_modules/uglify-js/test/unit/compress/test/with.js b/node_modules/uglify-js/test/unit/compress/test/with.js new file mode 100644 index 00000000..de266ed5 --- /dev/null +++ b/node_modules/uglify-js/test/unit/compress/test/with.js @@ -0,0 +1,2 @@ +with({}) { +}; diff --git a/node_modules/uglify-js/test/unit/scripts.js b/node_modules/uglify-js/test/unit/scripts.js new file mode 100644 index 00000000..564abd3f --- /dev/null +++ b/node_modules/uglify-js/test/unit/scripts.js @@ -0,0 +1,57 @@ +global.DIGITS_OVERRIDE_FOR_TESTING = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ$_0123456789"; + +var fs = require('fs'), + uglify = require('../../uglify-js'), + jsp = uglify.parser, + nodeunit = require('nodeunit'), + path = require('path'), + pro = uglify.uglify; + +var Script = process.binding('evals').Script; + +var scriptsPath = __dirname; + +function compress(code) { + var ast = jsp.parse(code); + ast = pro.ast_mangle(ast, { mangle: true }); + ast = pro.ast_squeeze(ast, { no_warnings: true }); + ast = pro.ast_squeeze_more(ast); + return pro.gen_code(ast); +}; + +var testDir = path.join(scriptsPath, "compress", "test"); +var expectedDir = path.join(scriptsPath, "compress", "expected"); + +function getTester(script) { + return function(test) { + var testPath = path.join(testDir, script); + var expectedPath = path.join(expectedDir, script); + var content = fs.readFileSync(testPath, 'utf-8'); + var outputCompress = compress(content); + + // Check if the noncompressdata is larger or same size as the compressed data + test.ok(content.length >= outputCompress.length); + + // Check that a recompress gives the same result + var outputReCompress = compress(content); + test.equal(outputCompress, outputReCompress); + + // Check if the compressed output is what is expected + var expected = fs.readFileSync(expectedPath, 'utf-8'); + test.equal(outputCompress, expected.replace(/(\r?\n)+$/, "")); + + test.done(); + }; +}; + +var tests = {}; + +var scripts = fs.readdirSync(testDir); +for (var i in scripts) { + var script = scripts[i]; + if (/\.js$/.test(script)) { + tests[script] = getTester(script); + } +} + +module.exports = nodeunit.testCase(tests); diff --git a/node_modules/uglify-js/tmp/hoist.js b/node_modules/uglify-js/tmp/hoist.js new file mode 100644 index 00000000..4bf2b94d --- /dev/null +++ b/node_modules/uglify-js/tmp/hoist.js @@ -0,0 +1,33 @@ +function foo(arg1, arg2, arg3, arg4, arg5, arg6) { + var a = 5; + { + var d = 10, mak = 20, buz = 30; + var q = buz * 2; + } + if (moo) { + var a, b, c; + } + for (var arg1 = 0, d = 20; arg1 < 10; ++arg1) + console.log(arg3); + for (var i in mak) {} + for (j in d) {} + var d; + + function test() { + + }; + + //test(); + + (function moo(first, second){ + console.log(first); + })(1); + + (function moo(first, second){ + console.log(moo()); + })(1); +} + + +var foo; +var bar; diff --git a/node_modules/uglify-js/tmp/instrument.js b/node_modules/uglify-js/tmp/instrument.js new file mode 100644 index 00000000..c6a9d798 --- /dev/null +++ b/node_modules/uglify-js/tmp/instrument.js @@ -0,0 +1,97 @@ +// sample on how to use the parser and walker API to instrument some code + +var jsp = require("uglify-js").parser; +var pro = require("uglify-js").uglify; + +function instrument(code) { + var ast = jsp.parse(code, false, true); // true for the third arg specifies that we want + // to have start/end tokens embedded in the + // statements + var w = pro.ast_walker(); + + // we're gonna need this to push elements that we're currently looking at, to avoid + // endless recursion. + var analyzing = []; + function do_stat() { + var ret; + if (this[0].start && analyzing.indexOf(this) < 0) { + // without the `analyzing' hack, w.walk(this) would re-enter here leading + // to infinite recursion + analyzing.push(this); + ret = [ "splice", // XXX: "block" is safer + [ [ "stat", + [ "call", [ "name", "trace" ], + [ [ "string", this[0].toString() ], + [ "num", this[0].start.line ], + [ "num", this[0].start.col ], + [ "num", this[0].end.line ], + [ "num", this[0].end.col ]]]], + w.walk(this) ]]; + analyzing.pop(this); + } + return ret; + }; + var new_ast = w.with_walkers({ + "stat" : do_stat, + "label" : do_stat, + "break" : do_stat, + "continue" : do_stat, + "debugger" : do_stat, + "var" : do_stat, + "const" : do_stat, + "return" : do_stat, + "throw" : do_stat, + "try" : do_stat, + "defun" : do_stat, + "if" : do_stat, + "while" : do_stat, + "do" : do_stat, + "for" : do_stat, + "for-in" : do_stat, + "switch" : do_stat, + "with" : do_stat + }, function(){ + return w.walk(ast); + }); + return pro.gen_code(new_ast, { beautify: true }); +} + + + + +////// test code follows. + +var code = instrument(test.toString()); +console.log(code); + +function test() { + // simple stats + a = 5; + c += a + b; + "foo"; + + // var + var foo = 5; + const bar = 6, baz = 7; + + // switch block. note we can't track case lines the same way. + switch ("foo") { + case "foo": + return 1; + case "bar": + return 2; + } + + // for/for in + for (var i = 0; i < 5; ++i) { + console.log("Hello " + i); + } + for (var i in [ 1, 2, 3]) { + console.log(i); + } + + // note however that the following is broken. I guess we + // should add the block brackets in this case... + for (var i = 0; i < 5; ++i) + console.log("foo"); +} diff --git a/node_modules/uglify-js/tmp/test.js b/node_modules/uglify-js/tmp/test.js new file mode 100755 index 00000000..f295fba8 --- /dev/null +++ b/node_modules/uglify-js/tmp/test.js @@ -0,0 +1,30 @@ +#! /usr/bin/env node + +global.sys = require(/^v0\.[012]/.test(process.version) ? "sys" : "util"); +var fs = require("fs"); +var uglify = require("uglify-js"), // symlink ~/.node_libraries/uglify-js.js to ../uglify-js.js + jsp = uglify.parser, + pro = uglify.uglify; + +var code = fs.readFileSync("hoist.js", "utf8"); +var ast = jsp.parse(code); + +ast = pro.ast_lift_variables(ast); + +var w = pro.ast_walker(); +ast = w.with_walkers({ + "function": function() { + var node = w.dive(this); // walk depth first + console.log(pro.gen_code(node, { beautify: true })); + return node; + }, + "name": function(name) { + return [ this[0], "X" ]; + } +}, function(){ + return w.walk(ast); +}); + +console.log(pro.gen_code(ast, { + beautify: true +})); diff --git a/node_modules/uglify-js/uglify-js.js b/node_modules/uglify-js/uglify-js.js new file mode 100644 index 00000000..6e14a637 --- /dev/null +++ b/node_modules/uglify-js/uglify-js.js @@ -0,0 +1,18 @@ +//convienence function(src, [options]); +function uglify(orig_code, options){ + options || (options = {}); + var jsp = uglify.parser; + var pro = uglify.uglify; + + var ast = jsp.parse(orig_code, options.strict_semicolons); // parse code and get the initial AST + ast = pro.ast_mangle(ast, options.mangle_options); // get a new AST with mangled names + ast = pro.ast_squeeze(ast, options.squeeze_options); // get an AST with compression optimizations + var final_code = pro.gen_code(ast, options.gen_options); // compressed code here + return final_code; +}; + +uglify.parser = require("./lib/parse-js"); +uglify.uglify = require("./lib/process"); +uglify.consolidator = require("./lib/consolidator"); + +module.exports = uglify diff --git a/processbuild.js b/processbuild.js new file mode 100644 index 00000000..fb5e92b1 --- /dev/null +++ b/processbuild.js @@ -0,0 +1,20 @@ +var fs = require('fs'); + +var jsp = require("uglify-js").parser; +var pro = require("uglify-js").uglify; + +var mod = fs.readFileSync(__dirname + '/dist/modernizr-build.js', 'utf8'); +var license = fs.readFileSync(__dirname + '/LICENSE', 'utf8'); + +mod = mod.replace('define("modernizr-init",[], function(){});', ''); +mod = license + "\n;(function(window, document, undefined){\n" + mod + "\n})(this, document);"; + +fs.writeFileSync(__dirname + '/dist/modernizr-build.js', mod, 'utf8'); + +var ast = jsp.parse(mod); // parse code and get the initial AST +ast = pro.ast_lift_variables(ast); +ast = pro.ast_mangle(ast, {except : ["Modernizr"]}); // get a new AST with mangled names +ast = pro.ast_squeeze(ast); // get an AST with compression optimizations +var final_code = pro.gen_code(ast); + +fs.writeFileSync(__dirname + '/dist/modernizr-build.min.js', license + ";" + final_code, 'utf8');