zoukankan      html  css  js  c++  java
  • 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.
  • 相关阅读:
    java—数组乘积输入: 一个长度为n的整数数组input 输出: 一个长度为n的数组result,满足result[i] = input数组中,除了input[i] 之外的所有数的乘积,不用考虑溢出例如 input {2, 3, 4, 5} output: {60, 40, 30, 24}
    深入理解按位异或运算符
    针对数组的三中排序方式:冒泡排序,选择排序,插入排序
    笔试题目整理
    Android中AIDL通信机制分析
    android消息处理机制之2handler与looper,MessageQueue:的关系
    Android消息处理机制(Handler 与Message)---01
    vim 批处理
    React 组件条件渲染的几种方式
    vim命令行模式
  • 原文地址:https://www.cnblogs.com/lexus/p/2498705.html
Copyright © 2011-2022 走看看