uglifyjs 压缩
UglifyJS is widely known as the most performant and effective JavaScript minifier available. UglifyJS' default minification with --compress
is nice but it doesn't do the full job. There are a number of additional directives for the compress
option, including:
UglifyJS是众所周知的最高效,最有效JavaScript压缩程序。 UglifyJS使用--compress
的默认缩小效果很好,但是并不能完成全部工作。 compress
选项还有许多其他指令,包括:
sequences
-- join consecutive simple statements using the comma operator
sequences
-使用逗号运算符连接连续的简单语句
properties
-- rewrite property access using the dot notation, for examplefoo["bar"] → foo.bar
properties
使用点表示法重写属性访问,例如foo["bar"] → foo.bar
dead_code
-- remove unreachable code
dead_code
删除无法访问的代码
drop_debugger
-- removedebugger;
statements
drop_debugger
删除debugger;
陈述
unsafe
(default: false) -- apply "unsafe" transformations (discussion below)
unsafe
(默认值:false)-应用“不安全”转换(以下讨论)
conditionals
-- apply optimizations forif
-s and conditional expressions
conditionals
-对if
-s和条件表达式进行优化
comparisons
-- apply certain optimizations to binary nodes, for example:!(a <= b) → a > b
(only whenunsafe
), attempts to negate binary nodes, e.g.a = !b && !c && !d && !e → a=!(b||c||d||e)
etc.
comparisons
-对二进制节点进行某些优化,例如:!(a <= b) → a > b
(仅当unsafe
),尝试取反二进制节点,例如a = !b && !c && !d && !e → a=!(b||c||d||e)
等。
evaluate
-- attempt to evaluate constant expressions
evaluate
-尝试评估常数表达式
booleans
-- various optimizations for boolean context, for example!!a ? b : c → a ? b : c
booleans
-布尔值上下文的各种优化,例如!!a ? b : c → a ? b : c
!!a ? b : c → a ? b : c
loops
-- optimizations fordo
,while
andfor
loops when we can statically determine the condition
loops
-当可以静态确定条件时对do
,while
和for
循环进行优化
unused
-- drop unreferenced functions and variables
unused
-删除未引用的函数和变量
hoist_funs
-- hoist function declarations
hoist_funs
提升函数声明
hoist_vars
(default: false) -- hoistvar
declarations (this isfalse
by default because it seems to increase the size of the output in general)
hoist_vars
(默认:false)-提升var
声明(默认情况下为false
,因为它通常会增加输出的大小)
if_return
-- optimizations for if/return and if/continue
if_return
优化if / return和if / continue
join_vars
-- join consecutivevar
statements
join_vars
连接连续的var
语句
cascade
-- small optimization for sequences, transformx, x
intox
andx = something(), x
intox = something()
cascade
-对序列进行小的优化,将x, x
转换为x
和x = something(), x
转换为x = something()
warnings
-- display warnings when dropping unreachable code or unused declarations etc.
warnings
-丢弃无法访问的代码或未使用的声明等时显示警告。
negate_iife
-- negate "Immediately-Called Function Expressions" where the return value is discarded, to avoid the parens that the code generator would insert.
negate_iife
-否定“立即调用的函数表达式”,在该表达式中丢弃返回值,以避免代码生成器插入的括号。
pure_getters
-- the default isfalse
. If you passtrue
for this, UglifyJS will assume that object property access (e.g.foo.bar
orfoo["bar"]
) doesn't have any side effects.
pure_getters
默认为false
。 如果为此传递true
,则UglifyJS将假定对象属性访问(例如foo.bar
或foo["bar"]
)没有任何副作用。
pure_funcs
-- defaultnull
. You can pass an array of names and UglifyJS will assume that those functions do not produce side effects. DANGER: will not check if the name is redefined in scope. An example case here, for instancevar q = Math.floor(a/b)
. If variableq
is not used elsewhere, UglifyJS will drop it, but will still keep theMath.floor(a/b)
, not knowing what it does. You can passpure_funcs: [ 'Math.floor' ]
to let it know that this function won't produce any side effect, in which case the whole statement would get discarded. The current implementation adds some overhead (compression will be slower).
pure_funcs
默认为null
您可以传递一个名称数组,UglifyJS会假定这些函数不会产生副作用。 危险:将不检查名称是否在范围内重新定义。 这里有一个示例案例,例如var q = Math.floor(a/b)
。 如果在其他地方未使用变量q
,则UglifyJS会将其删除,但仍会保留Math.floor(a/b)
,而不知道它的作用。 您可以传递pure_funcs: [ 'Math.floor' ]
使其知道该函数不会产生任何副作用,在这种情况下,整个语句将被丢弃。 当前的实现增加了一些开销(压缩会更慢)。
drop_console
-- defaultfalse
. Passtrue
to discard calls toconsole.*
functions.
drop_console
默认为false
。 传递true
以放弃对console.*
函数的调用。
So instead of simply doing a basic compress, squeeze the hell out of your JavaScript files by altering booleans, removing unneeded var
uses, axing unreachable code, and much more. Here's an example of such a case using the NodeJS API:
因此,除了简单地进行基本压缩之外,还可以通过更改布尔值 ,删除不需要的var
使用, 更改无法访问的代码等等来将JavaScript压缩出地狱。 这是使用NodeJS API的这种情况的示例:
var UglifyJS = require('uglify-js');
var fs = require('fs');
var result = UglifyJS.minify('site.js', {
mangle: true,
compress: {
sequences: true,
dead_code: true,
conditionals: true,
booleans: true,
unused: true,
if_return: true,
join_vars: true,
drop_console: true
}
});
fs.writeFileSync('site.min.js', result.code);
You can pass those compression values via command line as well. This post isn't meant to be groundbreaking but more to raise awareness that simply using --compress
doesn't optimize minification anywhere near potential. If you're going to minify and compress your JavaScript, go all out!
您也可以通过命令行传递这些压缩值。 这篇文章并不是开创性的,它的目的还在于提高人们的意识,即仅使用--compress
并不能在接近潜力的任何地方优化缩小效果。 如果您要缩小和压缩JavaScript,请全力以赴!
uglifyjs 压缩