These pages are old. They apply to UglifyJS v2. Version 3 has evolved a lot in the mean time to support most of ES6. Please check the documentation in the official repository for up-to-date information. Big thanks to all contributors, especially to Alex Lam S.L., who has maintained this project for years!
UglifyJS — the compressor
The compressor is a tree transformer which reduces the code size by applying various optimizations on the AST:
- join consecutive
var
/const
statements. - join consecutive simple statements into sequences using the “comma operator”.
- discard unused variables/functions.
- optimize
if
-s and conditional expressions. - evaluate constant expressions.
- drop unreachable code.
- ... and quite a few others.
SYNOPSIS
ast = UglifyJS.parse(code);
ast.figure_out_scope();
compressor = UglifyJS.Compressor({ ...compressor_options... });
ast = ast.transform(compressor);
code = ast.print_to_string(); // get compressed code
The default options should yield to best compression on most scripts. The following options are supported:
sequences : true, // join consecutive statemets with the “comma operator”
properties : true, // optimize property access: a["foo"] → a.foo
dead_code : true, // discard unreachable code
drop_debugger : true, // discard “debugger” statements
unsafe : false, // some unsafe optimizations (see below)
conditionals : true, // optimize if-s and conditional expressions
comparisons : true, // optimize comparisons
evaluate : true, // evaluate constant expressions
booleans : true, // optimize boolean expressions
loops : true, // optimize loops
unused : true, // drop unused variables/functions
hoist_funs : true, // hoist function declarations
hoist_vars : false, // hoist variable declarations
if_return : true, // optimize if-s followed by return/continue
join_vars : true, // join var declarations
cascade : true, // try to cascade `right` into `left` in sequences
side_effects : true, // drop side-effect-free statements
warnings : true, // warn about potentially dangerous optimizations/code
global_defs : {} // global definitions
Global definitions
This is a feature you can use in order to conditionally drop code. For example if you pass:
global_defs: {
DEBUG: false
}
the compressor will assume that's a constant defintion and will discard code like this as being unreachable:
if (DEBUG) {
...
}
This is useful in order to discard stuff that you need only in the development version from production builds.
In previous version of UglifyJS you had to actually pass an AST for such definitions, but in version 2 it's no longer the case—just pass the JavaScript constants that you want your global definitions to evaluate to.
UglifyJS
JS compressor of world fame.
Open demo- Home
- Parser
- Code generator
- Compressor
- Mangler
- The AST structure
- SpiderMonkey AST
- Scope analysis
- AST walker
- AST transformer
- UglifyJS on Github
Latest blog entries tagged "uglifyjs"
- Using UglifyJS for code refactoring
- Should you switch to UglifyJS2?
- UglifyJS online demo
- UglifyJS — why not switching to SpiderMonkey AST
/** May the source-map be with you! **/