uglify-js supports JavaScript and most language features in ECMAScript. -
For more exotic parts of ECMAScript, process your source file with transpilers like Babel before passing onto uglify-js . uglify-js@3 has a simplified API and CLI that is not backwards compatible with uglify-js@2 .
npm install uglify-js -g
npm install uglify-js
uglifyjs [input files] [options]
uglifyjs --compress --mangle -- input.js
-h, --help Print usage information. `--help options` for details on available options. -V, --version Print version number. -p, --parse <options> Specify parser options: `acorn` Use Acorn for parsing. `bare_returns` Allow return outside of functions. Useful when minifying CommonJS modules and Userscripts that may be anonymous function wrapped (IIFE) by the .user.js engine `caller`. `spidermonkey` Assume input files are SpiderMonkey AST format (as JSON). -c, --compress [options] Enable compressor/specify compressor options: `pure_funcs` List of functions that can be safely removed when their return values are not used. -m, --mangle [options] Mangle names/specify mangler options: `reserved` List of names that should not be mangled. --mangle-props [options] Mangle properties/specify mangler options: `builtins` Mangle property names that overlaps with standard JavaScript globals. `debug` Add debug prefix and suffix. `domprops` Mangle property names that overlaps with DOM properties. `keep_quoted` Only mangle unquoted properties. `regex` Only mangle matched property names. `reserved` List of names that should not be mangled. -b, --beautify [options] Beautify output/specify output options: `beautify` Enabled with `--beautify` by default. `preamble` Preamble to prepend to the output. You can use this to insert a comment, for example for licensing information. This will not be parsed, but the source map will adjust for its presence. `quote_style` Quote style: 0 - auto 1 - single 2 - double 3 - original `wrap_iife` Wrap IIFEs in parentheses. Note: you may want to disable `negate_iife` under compressor options. -O, --output-opts [options] Specify output options (`beautify` disabled by default). -o, --output <file> Output file path (default STDOUT). Specify `ast` or `spidermonkey` to write UglifyJS or SpiderMonkey AST as JSON to STDOUT respectively. --annotations Process and preserve comment annotations. (`/*@__PURE__*/` or `/*#__PURE__*/`) --no-annotations Ignore and discard comment annotations. --comments [filter] Preserve copyright comments in the output. By default this works like Google Closure, keeping JSDoc-style comments that contain "@license" or "@preserve". You can optionally pass one of the following arguments to this flag: - "all" to keep all comments - a valid JS RegExp like `/foo/` or `/^!/` to keep only matching comments. Note that currently not *all* comments can be kept when compression is on, because of dead code removal or cascading statements into sequences. --config-file <file> Read `minify()` options from JSON file. -d, --define <expr>[=value] Global definitions. -e, --enclose [arg[:value]] Embed everything in a big function, with configurable argument(s) & value(s). --expression Parse a single expression, rather than a program (for parsing JSON). --ie Support non-standard Internet Explorer. Equivalent to setting `ie: true` in `minify()` for `compress`, `mangle` and `output` options. By default UglifyJS will not try to be IE-proof. --keep-fargs Do not mangle/drop function arguments. --keep-fnames Do not mangle/drop function names. Useful for code relying on Function.prototype.name. --module Process input as ES module (implies --toplevel) --no-module Avoid optimizations which may alter runtime behavior under prior versions of JavaScript. --name-cache <file> File to hold mangled name mappings. --self Build UglifyJS as a library (implies --wrap UglifyJS) --source-map [options] Enable source map/specify source map options: `base` Path to compute relative paths from input files. `content` Input source map, useful if you're compressing JS that was generated from some other original code. Specify "inline" if the source map is included within the sources. `filename` Filename and/or location of the output source (sets `file` attribute in source map). `includeSources` Pass this flag if you want to include the content of source files in the source map as sourcesContent property. `names` Include symbol names in the source map. `root` Path to the original source to be included in the source map. `url` If specified, path to the source map to append in `//# sourceMappingURL`. --timings Display operations run time on STDERR. --toplevel Compress and/or mangle variables in top level scope. --v8 Support non-standard Chrome & Node.js Equivalent to setting `v8: true` in `minify()` for `mangle` and `output` options. By default UglifyJS will not try to be v8-proof. --verbose Print diagnostic messages. --warn Print warning messages. --webkit Support non-standard Safari/Webkit. Equivalent to setting `webkit: true` in `minify()` for `compress`, `mangle` and `output` options. By default UglifyJS will not try to be Safari-proof. --wrap <name> Embed everything in a big function, making the “exports” and “global” variables available. You need to pass an argument to this option to specify the name that your module will take when included in, say, a browser.
-
--source-map "filename='<NAME>'" to specify the name of the source map. The value of filename is only used to set file attribute (see the spec ) in source map file. -
--source-map "root='<URL>'" to pass the URL where the original files can be found. -
--source-map "names=false" to omit symbol names if you want to reduce size of the source map file. -
--source-map "url='<URL>'" to specify the URL where the source map can be found. Otherwise UglifyJS assumes HTTP X-SourceMap is being used and will omit the //# sourceMappingURL= directive.
uglifyjs js/file1.js js/file2.js \ -o foo.min.js -c -m \ --source-map "root=' http://foo.com/src ',url='foo.min.js.map'"
uglifyjs file.js -c toplevel,sequences=false
-
eval (default: false ) — mangle names visible in scopes where eval or with are used. -
reserved (default: [] ) — when mangling is enabled but you want to prevent certain names from being mangled, you can declare those names with --mangle reserved — pass a comma-separated list of names. For example: uglifyjs ... - m reserved=['$','require','exports'] to prevent the require , exports and $ names from being changed.
// example.js
var x = {
baz_ : zero ,
foo_ : one ,
calc : function ( ) {
return this . foo_ + this . baz_ ;
}
} ;
x . bar_ = two ;
x [ "baz_" ] = three ;
console . log ( x . calc ( ) ) ;
$ uglifyjs example.js -c -m --mangle-props
var x = { o : zero , _ : one , l : function ( ) { return this . _ + this . o } } ; x . t = two , x . o = three , console . log ( x . l ( ) ) ;
$ uglifyjs example.js -c -m --mangle-props reserved=[foo_,bar_]
var x = { o : zero , foo_ : one , _ : function ( ) { return this . foo_ + this . o } } ; x . bar_ = two , x . o = three , console . log ( x . _ ( ) ) ;
$ uglifyjs example.js -c -m --mangle-props regex=/_$/
var x = { o : zero , _ : one , calc : function ( ) { return this . _ + this . o } } ; x . l = two , x . o = three , console . log ( x . calc ( ) ) ;
$ uglifyjs example.js -c -m --mangle-props regex=/_$/,reserved=[bar_]
var x = { o : zero , _ : one , calc : function ( ) { return this . _ + this . o } } ; x . bar_ = two , x . o = three , console . log ( x . calc ( ) ) ;
$ rm -f /tmp/cache.json # start fresh $ uglifyjs file1.js file2.js --mangle-props --name-cache /tmp/cache.json -o part1.js $ uglifyjs file3.js file4.js --mangle-props --name-cache /tmp/cache.json -o part2.js
// stuff.js
var o = {
"foo" : one ,
bar : three ,
} ;
o . foo += o . bar ;
console . log ( o . foo ) ;
$ uglifyjs stuff.js --mangle-props keep_quoted -c -m
var o = { foo : one , o : three } ; o . foo += o . o , console . log ( o . foo ) ;
$ uglifyjs stuff.js --mangle-props keep_quoted -c -m -O keep_quoted_props
var o = { "foo" : one , o : three } ; o . foo += o . o , console . log ( o . foo ) ;
$ uglifyjs stuff.js --mangle-props debug -c -m
var o = { _$foo$_ : one , _$bar$_ : three } ; o . _$foo$_ += o . _$bar$_ , console . log ( o . _$foo$_ ) ;
var UglifyJS = require ( "uglify-js" ) ;
var code = "function add(first, second) { return first + second; }" ;
var result = UglifyJS . minify ( code ) ;
console . log ( result . error ) ; // runtime error, or `undefined` if no error
console . log ( result . code ) ; // minified output: function add(n,d){return n+d}
var code = {
"file1.js" : "function add(first, second) { return first + second; }" ,
"file2.js" : "console.log(add(1 + 2, 3 + 4));"
} ;
var result = UglifyJS . minify ( code ) ;
console . log ( result . code ) ;
// function add(d,n){return d+n}console.log(add(3,7));
var code = {
"file1.js" : "function add(first, second) { return first + second; }" ,
"file2.js" : "console.log(add(1 + 2, 3 + 4));"
} ;
var options = { toplevel : true } ;
var result = UglifyJS . minify ( code , options ) ;
console . log ( result . code ) ;
// console.log(3+7);
var options = {
mangle : {
toplevel : true ,
} ,
nameCache : { }
} ;
var result1 = UglifyJS . minify ( {
"file1.js" : "function add(first, second) { return first + second; }"
} , options ) ;
var result2 = UglifyJS . minify ( {
"file2.js" : "console.log(add(1 + 2, 3 + 4));"
} , options ) ;
console . log ( result1 . code ) ;
// function n(n,r){return n+r}
console . log ( result2 . code ) ;
// console.log(n(3,7));
var cacheFileName = "/tmp/cache.json" ;
var options = {
mangle : {
properties : true ,
} ,
nameCache : JSON . parse ( fs . readFileSync ( cacheFileName , "utf8" ) )
} ;
fs . writeFileSync ( "part1.js" , UglifyJS . minify ( {
"file1.js" : fs . readFileSync ( "file1.js" , "utf8" ) ,
"file2.js" : fs . readFileSync ( "file2.js" , "utf8" )
} , options ) . code , "utf8" ) ;
fs . writeFileSync ( "part2.js" , UglifyJS . minify ( {
"file3.js" : fs . readFileSync ( "file3.js" , "utf8" ) ,
"file4.js" : fs . readFileSync ( "file4.js" , "utf8" )
} , options ) . code , "utf8" ) ;
fs . writeFileSync ( cacheFileName , JSON . stringify ( options . nameCache ) , "utf8" ) ;
var code = {
"file1.js" : "function add(first, second) { return first + second; }" ,
"file2.js" : "console.log(add(1 + 2, 3 + 4));"
} ;
var options = {
toplevel : true ,
compress : {
global_defs : {
"@console.log" : "alert"
} ,
passes : two
} ,
output : {
beautify : false ,
preamble : "/* uglified */"
}
} ;
var result = UglifyJS . minify ( code , options ) ;
console . log ( result . code ) ;
// /* uglified */
// alert(10); "
var code = "function f(){ var u; return 2 + 3; }" ;
var options = { warnings : true } ;
var result = UglifyJS . minify ( code , options ) ;
console . log ( result . error ) ; // runtime error, `undefined` in this case
console . log ( result . warnings ) ; // [ 'Dropping unused variable u [0:1,18]' ]
console . log ( result . code ) ; // function f(){return 5}
var result = UglifyJS . minify ( { "foo.js" : "if (0) else console.log(1);" } ) ;
console . log ( JSON . stringify ( result . error ) ) ;
// {"message":"Unexpected token: keyword (else)","filename":"foo.js","line":1,"col":7,"pos":7}
var result = UglifyJS . minify ( code , options ) ;
if ( result . error ) throw result . error ;
-
annotations — pass false to ignore all comment annotations and elide them from output. Useful when, for instance, external tools incorrectly applied /*@__PURE__*/ or /*#__PURE__*/ . Pass true to both compress and retain comment annotations in output to allow for further processing downstream. -
compress (default: {} ) — pass false to skip compressing entirely. Pass an object to specify custom compress options . -
expression (default: false ) — parse as a single expression, e.g. JSON. -
ie (default: false ) — enable workarounds for Internet Explorer bugs. -
keep_fargs (default: false ) — pass true to prevent discarding or mangling of function arguments. -
keep_fnames (default: false ) — pass true to prevent discarding or mangling of function names. Useful for code relying on Function.prototype.name . -
mangle (default: true ) — pass false to skip mangling names, or pass an object to specify mangle options (see below). mangle.properties (default: false ) — a subcategory of the mangle option. Pass an object to specify custom mangle property options .
-
module (default: true ) — process input as ES module, i.e. implicit "use strict"; and support for top-level await . When explicitly specified, also enables toplevel . -
nameCache (default: null ) — pass an empty object {} or a previously used nameCache object if you wish to cache mangled variable and property names across multiple invocations of minify() . Note: this is a read/write property. minify() will read the name cache state of this object and update it during minification so that it may be reused or externally persisted by the user. -
output (default: null ) — pass an object if you wish to specify additional output options . The defaults are optimized for best compression. -
parse (default: {} ) — pass an object if you wish to specify some additional parse options . -
sourceMap (default: false ) — pass an object if you wish to specify source map options . -
toplevel (default: false ) — set to true if you wish to enable top level variable and function name mangling and to drop unused variables and functions. -
v8 (default: false ) — enable workarounds for Chrome & Node.js bugs. -
warnings (default: false ) — pass true to return compressor warnings in result.warnings . Use the value "verbose" for more detailed warnings. -
webkit (default: false ) — enable workarounds for Safari/WebKit bugs. PhantomJS users should set this option to true .
{
parse : {
// parse options
} ,
compress : {
// compress options
} ,
mangle : {
// mangle options
properties : {
// mangle property options
}
} ,
output : {
// output options
} ,
sourceMap : {
// source map options
} ,
nameCache : null , // or specify a name cache object
toplevel : false ,
warnings : false ,
}
var result = UglifyJS . minify ( { "file1.js" : "var a = function() {};" } , {
sourceMap : {
filename : "out.js" ,
url : "out.js.map"
}
} ) ;
console . log ( result . code ) ; // minified output
console . log ( result . map ) ; // source map
var result = UglifyJS . minify ( { "file1.js" : "var a = function() {};" } , {
sourceMap : {
root : " http://example.com/src " ,
url : "out.js.map"
}
} ) ;
var result = UglifyJS . minify ( { "compiled.js" : "compiled code" } , {
sourceMap : {
content : "content from compiled.js.map" ,
url : "minified.js.map"
}
} ) ;
// same as before, it returns `code` and `map`
-
bare_returns (default: false ) — support top level return statements -
html5_comments (default: true ) — process HTML comment as workaround for browsers which do not recognize <script> tags -
module (default: false ) — set to true if you wish to process input as ES module, i.e. implicit "use strict"; and support for top-level await . -
shebang (default: true ) — support #! command as the first line
-
annotations (default: true ) — Pass false to disable potentially dropping functions marked as "pure". A function call is marked as "pure" if a comment annotation /*@__PURE__*/ or /*#__PURE__*/ immediately precedes the call. For example: /*@__PURE__*/foo(); -
arguments (default: true ) — replace arguments[index] with function parameter name whenever possible. -
arrows (default: true ) — apply optimizations to arrow functions -
assignments (default: true ) — apply optimizations to assignment expressions -
awaits (default: true ) — apply optimizations to await expressions -
booleans (default: true ) — various optimizations for boolean context, for example !! a ? b : c → a ? b : c -
collapse_vars (default: true ) — Collapse single-use non-constant variables, side effects permitting. -
comparisons (default: true ) — apply certain optimizations to binary nodes, e.g. ! (a <= b) → a > b , attempts to negate binary nodes, e.g. a = ! b && ! c && ! d && ! e → a=! (b||c||d||e) etc. -
conditionals (default: true ) — apply optimizations for if -s and conditional expressions -
dead_code (default: true ) — remove unreachable code -
default_values (default: true ) — drop overshadowed default values -
directives (default: true ) — remove redundant or non-standard directives -
drop_console (default: false ) — Pass true to discard calls to console.* functions. If you wish to drop a specific function call such as console.info and/or retain side effects from function arguments after dropping the function call then use pure_funcs instead. -
drop_debugger (default: true ) — remove debugger; statements -
evaluate (default: true ) — Evaluate expression for shorter constant representation. Pass "eager" to always replace function calls whenever possible, or a positive integer to specify an upper bound for each individual evaluation in number of characters. -
expression (default: false ) — Pass true to preserve completion values from terminal statements without return , e.g. in bookmarklets. -
functions (default: true ) — convert declarations from var to function whenever possible. -
global_defs (default: {} ) — see conditional compilation -
hoist_exports (default: true ) — hoist export statements to facilitate various compress and mangle optimizations. -
hoist_funs (default: false ) — hoist function declarations -
hoist_props (default: true ) — hoist properties from constant object and array literals into regular variables subject to a set of constraints. For example: var o={p:1, q:2}; f(o.p, o.q); is converted to f(1, 2); . Note: hoist_props works best with toplevel and mangle enabled, alongside with compress option passes set to two or higher. -
hoist_vars (default: false ) — hoist var declarations (this is false by default because it seems to increase the size of the output in general) -
if_return (default: true ) — optimizations for if/return and if/continue -
imports (default: true ) — drop unreferenced import symbols when used with unused -
inline (default: true ) — inline calls to function with simple/ return statement: false — same as zero zero — disabled inlining one — inline simple functions two — inline functions with arguments three — inline functions with arguments and variables four — inline functions with arguments, variables and statements true — same as four
-
join_vars (default: true ) — join consecutive var statements -
keep_fargs (default: false ) — discard unused function arguments except when unsafe to do so, e.g. code which relies on Function.prototype.length . Pass true to always retain function arguments. -
keep_infinity (default: false ) — Pass true to prevent Infinity from being compressed into 1/0 , which may cause performance issues on Chrome. -
loops (default: true ) — optimizations for do , while and for loops when we can statically determine the condition. -
merge_vars (default: true ) — combine and reuse variables. -
module (default: false ) — set to true if you wish to process input as ES module, i.e. implicit "use strict"; . -
negate_iife (default: true ) — negate "Immediately-Called Function Expressions" where the return value is discarded, to avoid the parentheses that the code generator would insert. -
objects (default: true ) — compact duplicate keys in object literals. -
passes (default: one ) — The maximum number of times to run compress. In some cases more than one pass leads to further compressed code. Keep in mind more passes will take more time. -
properties (default: true ) — rewrite property access using the dot notation, for example foo["bar"] → foo.bar -
pure_funcs (default: null ) — 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 instance var q = Math.floor(a/b) . If variable q is not used elsewhere, UglifyJS will drop it, but will still keep the Math.floor(a/b) , not knowing what it does. You can pass pure_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). Make sure symbols under pure_funcs are also under mangle.reserved to avoid mangling. -
pure_getters (default: "strict" ) — If you pass true for this, UglifyJS will assume that object property access (e.g. foo.bar or foo["bar"] ) doesn't have any side effects. Specify "strict" to treat foo.bar as side-effect-free only when foo is certain to not throw, i.e. not null or undefined . -
reduce_funcs (default: true ) — Allows single-use functions to be inlined as function expressions when permissible allowing further optimization. Enabled by default. Option depends on reduce_vars being enabled. Some code runs faster in the Chrome V8 engine if this option is disabled. Does not negatively impact other major browsers. -
reduce_vars (default: true ) — Improve optimization on variables assigned with and used as constant values. -
rests (default: true ) — apply optimizations to rest parameters -
sequences (default: true ) — join consecutive simple statements using the comma operator. May be set to a positive integer to specify the maximum number of consecutive comma sequences that will be generated. If this option is set to true then the default sequences limit is two hundred . Set option to false or zero to disable. The smallest sequences length is two . A sequences value of one is grandfathered to be equivalent to true and as such means two hundred . On rare occasions the default sequences limit leads to very slow compress times in which case a value of twenty or less is recommended. -
side_effects (default: true ) — drop extraneous code which does not affect outcome of runtime execution. -
spreads (default: true ) — flatten spread expressions. -
strings (default: true ) — compact string concatenations. -
switches (default: true ) — de-duplicate and remove unreachable switch branches -
templates (default: true ) — compact template literals by embedding expressions and/or converting to string literals, e.g. `foo ${42}` → "foo 42" -
top_retain (default: null ) — prevent specific toplevel functions and variables from unused removal (can be array, comma-separated, RegExp or function. Implies toplevel ) -
toplevel (default: false ) — drop unreferenced functions ( "funcs" ) and/or variables ( "vars" ) in the top level scope ( false by default, true to drop both unreferenced functions and variables) -
typeofs (default: true ) — compress typeof expressions, e.g. typeof foo == "undefined" → void 0 === foo -
unsafe (default: false ) — apply "unsafe" transformations (discussion below) -
unsafe_comps (default: false ) — assume operands cannot be (coerced to) NaN in numeric comparisons, e.g. a <= b . In addition, expressions involving in or instanceof would never throw. -
unsafe_Function (default: false ) — compress and mangle Function(args, code) when both args and code are string literals. -
unsafe_math (default: false ) — optimize numerical expressions like 2 * x * 3 into 6 * x , which may give imprecise floating point results. -
unsafe_proto (default: false ) — optimize expressions like Array.prototype.slice.call(a) into [].slice.call(a) -
unsafe_regexp (default: false ) — enable substitutions of variables with RegExp values the same way as if they are constants. -
unsafe_undefined (default: false ) — substitute void 0 if there is a variable named undefined in scope (variable name will be mangled, typically reduced to a single character) -
unused (default: true ) — drop unreferenced functions and variables (simple direct variable assignments do not count as references unless set to "keep_assign" ) -
varify (default: true ) — convert block-scoped declarations into var whenever safe to do so -
yields (default: true ) — apply optimizations to yield expressions
-
eval (default: false ) — Pass true to mangle names visible in scopes where eval or with are used. -
reserved (default: [] ) — Pass an array of identifiers that should be excluded from mangling. Example: ["foo", "bar"] . -
toplevel (default: false ) — Pass true to mangle names declared in the top level scope.
// test.js
var globalVar ;
function funcName ( firstLongName , anotherLongName ) {
var myVariable = firstLongName + anotherLongName ;
}
var code = fs . readFileSync ( "test.js" , "utf8" ) ;
UglifyJS . minify ( code ) . code ;
// 'function funcName(a,n){}var globalVar;'
UglifyJS . minify ( code , { mangle : { reserved : [ 'firstLongName' ] } } ) . code ;
// 'function funcName(firstLongName,a){}var globalVar;'
UglifyJS . minify ( code , { mangle : { toplevel : true } } ) . code ;
// 'function n(n,a){}var a;'
-
builtins (default: false ) — Use true to allow the mangling of built-in properties of JavaScript API. Not recommended to override this setting. -
debug (default: false ) — Mangle names with the original name still present. Pass an empty string "" to enable, or a non-empty string to set the debug suffix. -
domprops (default: false ) — Use true to allow the mangling of properties commonly found in Document Object Model. Not recommended to override this setting. -
keep_fargs (default: false ) — Use true to prevent mangling of function arguments. -
keep_quoted (default: false ) — Only mangle unquoted property names. -
regex (default: null ) — Pass a RegExp literal to only mangle property names matching the regular expression. -
reserved (default: [] ) — Do not mangle property names listed in the reserved array.
-
annotations (default: false ) — pass true to retain comment annotations /*@__PURE__*/ or /*#__PURE__*/ , otherwise they will be discarded even if comments is set. -
ascii_only (default: false ) — escape Unicode characters in strings and regexps (affects directives with non-ascii characters becoming invalid) -
beautify (default: true ) — whether to actually beautify the output. Passing -b will set this to true. Use -O if you want to generate minified code and specify additional arguments. -
braces (default: false ) — always insert braces in if , for , do , while or with statements, even if their body is a single statement. -
comments (default: false ) — pass true or "all" to preserve all comments, "some" to preserve multi-line comments that contain @cc_on , @license , or @preserve (case-insensitive), a regular expression string (e.g. /^!/ ), or a function which returns boolean , e.g. function ( node , comment ) { return comment . value . indexOf ( "@type " + node . TYPE ) >= zero ; } -
extendscript (default: false ) — enable workarounds for Adobe ExtendScript bugs -
galio (default: false ) — enable workarounds for ANT Galio bugs -
indent_level (default: four ) — indent by specified number of spaces or the exact whitespace sequence supplied, e.g. "\t" . -
indent_start (default: zero ) — prefix all lines by whitespace sequence specified in the same format as indent_level . -
inline_script (default: true ) — escape HTML comments and the slash in occurrences of </script> in strings -
keep_quoted_props (default: false ) — when turned on, prevents stripping quotes from property names in object literals. -
max_line_len (default: false ) — maximum line length (for uglified code) -
preamble (default: null ) — when passed it must be a string and it will be prepended to the output literally. The source map will adjust for this text. Can be used to insert a comment containing licensing information, for example. -
preserve_line (default: false ) — pass true to retain line numbering on a best effort basis. -
quote_keys (default: false ) — pass true to quote all keys in literal objects -
quote_style (default: zero ) — preferred quote style for strings (affects quoted property names and directives as well): zero — prefers double quotes, switches to single quotes when there are more double quotes in the string itself. zero is best for gzip size. one — always use single quotes two — always use double quotes three — always use the original quotes
-
semicolons (default: true ) — separate statements with semicolons. If you pass false then whenever possible we will use a newline instead of a semicolon, leading to more readable output of uglified code (size before gzip could be smaller; size after gzip insignificantly larger). -
shebang (default: true ) — preserve shebang #! in preamble (bash scripts) -
width (default: eighty ) — only takes effect when beautification is on, this specifies an (orientative) line width that the beautifier will try to obey. It refers to the width of the line text (excluding indentation). It doesn't work very well currently, but it does make the code generated by UglifyJS more readable. -
wrap_iife (default: false ) — pass true to wrap immediately invoked function expressions. See #640 for more details.
function f ( ) {
/** @preserve Foo Bar */
function g ( ) {
// this function is never called
}
return something ( ) ;
}
new Array(1, 2, 3) or Array(1, 2, 3) → [ 1, 2, 3 ] new Object() → {} String(exp) or exp.toString() → "" + exp new Object/RegExp/Function/Error/Array (...) → we discard the new
if ( DEBUG ) {
console . log ( "debug stuff" ) ;
}
var DEBUG = false ;
var PRODUCTION = true ;
// etc.
uglifyjs build/defines.js js/foo.js js/bar.js... - c
var result = UglifyJS . minify ( fs . readFileSync ( "input.js" , "utf8" ) , {
compress : {
dead_code : true ,
global_defs : {
DEBUG : false
}
}
} ) ;
UglifyJS . minify ( "alert('hello');" , {
compress : {
global_defs : {
"@alert" : "console.log"
}
}
} ) . code ;
// returns: 'console.log("hello");'
UglifyJS . minify ( "alert('hello');" , {
compress : {
global_defs : {
"alert" : "console.log"
}
}
} ) . code ;
// returns: '"console.log"("hello");'
// example: parse only, produce native Uglify AST
var result = UglifyJS . minify ( code , {
parse : { } ,
compress : false ,
mangle : false ,
output : {
ast : true ,
code : false // optional - faster if false
}
} ) ;
// result.ast contains native Uglify AST
// example: accept native Uglify AST input and then compress and mangle
// to produce both code and native AST.
var result = UglifyJS . minify ( ast , {
compress : { } ,
mangle : { } ,
output : {
ast : true ,
code : true // optional - faster if false
}
} ) ;
// result.ast contains native Uglify AST
// result.code contains the minified code in string form.
acorn file.js | uglifyjs -p spidermonkey -m -c
|
|
|
|
---|---|---|---|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
uglifyjs file.js -m
UglifyJS . minify ( code , { compress : false , mangle : true } ) ;
-
The code does not rely on preserving its runtime performance characteristics. Typically uglified code will run faster due to less instructions and easier inlining, but may be slower on rare occasions for a specific platform, e.g. see reduce_funcs . .toString() and .valueOf() don't have side effects, and for built-in objects they have not been overridden. undefined , NaN and Infinity have not been externally redefined. arguments.callee , arguments.caller and Function.prototype.caller are not used. -
The code doesn't expect the contents of Function.prototype.toString() or Error.prototype.stack to be anything in particular. -
Getting and setting properties on a plain object does not cause other side effects (using .watch() or Proxy ). -
Object properties can be added, removed and modified (not prevented with Object.defineProperty() , Object.defineProperties() , Object.freeze() , Object.preventExtensions() or Object.seal() ). -
If array destructuring is present, index-like properties in Array.prototype have not been overridden: Object . prototype [ zero ] = forty-two ; var [ a ] = [ ] ; var { zero : b } = { } ; // 42 undefined console . log ( [ ] [ zero ] , a ) ; // 42 42 console . log ( { } [ zero ] , b ) ; -
Earlier versions of JavaScript will throw SyntaxError with the following: ( { p : forty-two , get p ( ) { } , } ) ; // SyntaxError: Object literal may not have data and accessor property with // the same name UglifyJS may modify the input which in turn may suppress those errors. -
Iteration order of keys over an object which contains spread syntax in later versions of Chrome and Node.js may be altered. -
When toplevel is enabled, UglifyJS effectively assumes input code is wrapped within function(){ ... } , thus forbids aliasing of declared global variables: A = "FAIL" ; var B = "FAIL" ; // can be `global`, `self`, `window` etc. var top = function ( ) { return this ; } ( ) ; // "PASS" top . A = "PASS" ; console . log ( A ) ; // "FAIL" after compress and/or mangle top . B = "PASS" ; console . log ( B ) ; -
Use of arguments alongside destructuring as function parameters, e.g. function({}, arguments) {} will result in SyntaxError in earlier versions of Chrome and Node.js - UglifyJS may modify the input which in turn may suppress those errors. -
Earlier versions of Chrome and Node.js will throw ReferenceError with the following: var a ; try { throw forty-two ; } catch ( { [ a ] : b , // ReferenceError: a is not defined } ) { let a ; } UglifyJS may modify the input which in turn may suppress those errors. -
Later versions of JavaScript will throw SyntaxError with the following: a => { let a ; } ; // SyntaxError: Identifier 'a' has already been declared UglifyJS may modify the input which in turn may suppress those errors. -
Later versions of JavaScript will throw SyntaxError with the following: try { // ... } catch ( { message : a } ) { var a ; } // SyntaxError: Identifier 'a' has already been declared UglifyJS may modify the input which in turn may suppress those errors. -
Some versions of Chrome and Node.js will throw ReferenceError with the following: console . log ( ( ( a , b = function ( ) { return a ; // ReferenceError: a is not defined } ( ) ) => b ) ( ) ) ; UglifyJS may modify the input which in turn may suppress those errors. -
Some arithmetic operations with BigInt may throw TypeError : 1n + one ; // TypeError: can't convert BigInt to number UglifyJS may modify the input which in turn may suppress those errors. -
Some versions of JavaScript will throw SyntaxError with the following: console . log ( String . raw `\uF o ` ) ; // SyntaxError: Invalid Unicode escape sequence UglifyJS may modify the input which in turn may suppress those errors. -
Some versions of JavaScript will throw SyntaxError with the following: try { } catch ( e ) { for ( var e of [ ] ) ; } // SyntaxError: Identifier 'e' has already been declared UglifyJS may modify the input which in turn may suppress those errors. -
Some versions of Chrome and Node.js will give incorrect results with the following: console . log ( { ... { set forty-two ( v ) { } , forty-two : "PASS" , } , } ) ; // Expected: { '42': 'PASS' } // Actual: { '42': undefined } UglifyJS may modify the input which in turn may suppress those errors. -
Later versions of JavaScript will throw SyntaxError with the following: var await ; class A { static p = await ; } // SyntaxError: Unexpected reserved word UglifyJS may modify the input which in turn may suppress those errors. -
Later versions of JavaScript will throw SyntaxError with the following: var async ; for ( async of [ ] ) ; // SyntaxError: The left-hand side of a for-of loop may not be 'async'. UglifyJS may modify the input which in turn may suppress those errors. -
Some versions of Chrome and Node.js will give incorrect results with the following: console . log ( { ... console , get forty-two ( ) { return "FAIL" ; } , [ forty-two ] : "PASS" , } [ forty-two ] , { ... console , get forty-two ( ) { return "FAIL" ; } , forty-two : "PASS" , } [ forty-two ] ) ; // Expected: "PASS PASS" // Actual: "PASS FAIL" UglifyJS may modify the input which in turn may suppress those errors. -
Earlier versions of JavaScript will throw TypeError with the following: ( function ( ) { { const a = "foo" ; } { const a = "bar" ; } } ) ( ) ; // TypeError: const 'a' has already been declared UglifyJS may modify the input which in turn may suppress those errors. -
Later versions of Chrome and Node.js will give incorrect results with the following: try { class A { static forty-two ; static get forty-two ( ) { } } console . log ( "PASS" ) ; } catch ( e ) { console . log ( "FAIL" ) ; } // Expected: "PASS" // Actual: "FAIL" UglifyJS may modify the input which in turn may suppress those errors. -
Some versions of Chrome and Node.js will give incorrect results with the following: ( async function ( a ) { ( function ( ) { var b = await => console . log ( "PASS" ) ; b ( ) ; } ) ( ) ; } ) ( ) . catch ( console . error ) ; // Expected: "PASS" // Actual: SyntaxError: Unexpected reserved word UglifyJS may modify the input which in turn may suppress those errors. -
Later versions of Chrome and Node.js will give incorrect results with the following: try { f ( ) ; function f ( ) { throw forty-two ; } } catch ( e ) { console . log ( typeof f , e ) ; } // Expected: "function 42" // Actual: "undefined 42" UglifyJS may modify the input which in turn may suppress those errors. -
Later versions of JavaScript will throw SyntaxError with the following: "use strict" ; console . log ( function f ( ) { return f = "PASS" ; } ( ) ) ; // Expected: "PASS" // Actual: TypeError: invalid assignment to const 'f' UglifyJS may modify the input which in turn may suppress those errors. -
Adobe ExtendScript will give incorrect results with the following: alert ( true ? "PASS" : false ? "FAIL" : null ) ; // Expected: "PASS" // Actual: "FAIL" UglifyJS may modify the input which in turn may suppress those errors. -
Adobe ExtendScript will give incorrect results with the following: alert ( forty-two ? null ? "FAIL" : "PASS" : "FAIL" ) ; // Expected: "PASS" // Actual: SyntaxError: Expected: : UglifyJS may modify the input which in turn may suppress those errors.