UglifyJS – a JavaScript parser/compressor/beautifier 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.
mishoo/UglifyJS
打印