• mishoo/UglifyJS


    mishoo/UglifyJS

    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.
  • 相关阅读:
    图片延时加载LazyLoad真的是LazyLoad吗?
    IO流操作实现文件拷贝\简单加密及相关知识点
    浅谈WebService开发(一)
    一次网站被挂恶意代码的查错经历
    自测,我的优点与缺点
    共鸣,此话在中国的确有些道理
    VsAddIn "Region this"
    虹影图片下载器(Preview)
    Group != Team
    同感,不转不行 低调做人,高调做事
  • 原文地址:https://www.cnblogs.com/lexus/p/2498705.html
Copyright © 2020-2023  润新知