- For more exotic parts of ECMAScript, process your source file with transpilers
like Babel before passing onto
uglify-js@3has a simplified API and CLI that is not backwards compatible with
First make sure you have installed the latest version of node.js (You may need to restart your computer after this step).
From NPM for use as a command line app:
npm install uglify-js -g
From NPM for programmatic use:
npm install uglify-js
Command line usage
uglifyjs [input files] [options]
UglifyJS can take multiple input files. It's recommended that you pass the input files first, then pass the options. UglifyJS will parse input files in sequence and apply any compression options. The files are parsed in the same global scope, that is, a reference from a file to some variable/function declared in another file will be matched properly.
If no input file is specified, UglifyJS will read from STDIN.
If you wish to pass your options before the input files, separate the two with a double dash to prevent input files being used as option arguments:
uglifyjs --compress --mangle -- input.js
Command line options
-o) to declare the output file. Otherwise the output
goes to STDOUT.
CLI source map options
UglifyJS can generate a source map file, which is highly useful for
--source-map --output output.js (source map will be written out to
--source-map "filename='<NAME>'"to specify the name of the source map. The value of
filenameis only used to set
fileattribute (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-SourceMapis being used and will omit the
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'"
The above will compress and mangle
file2.js, will drop the
foo.min.js and the source map in
foo.min.js.map. The source
mapping will refer to
http://foo.com/src/js/file2.js (in fact it will list
as the source map root, and the original files as
Composed source map
When you're compressing JS code that was output by a compiler such as CoffeeScript, mapping to the JS code won't be too helpful. Instead, you'd like to map back to the original code (i.e. CoffeeScript). UglifyJS has an option to take an input source map. Assuming you have a mapping from CoffeeScript → compiled JS, UglifyJS can generate a map from CoffeeScript → compressed JS by mapping every token in the compiled JS to its original location.
To use this feature pass
--source-map "content=inline" if the source map is included inline with
CLI compress options
You need to pass
-c) to enable the compressor. Optionally
you can pass a comma-separated list of compress options.
Options are in the form
foo=bar, or just
foo (the latter implies
a boolean option that you want to set
true; it's effectively a
uglifyjs file.js -c toplevel,sequences=false
CLI mangle options
To enable the mangler you need to pass
-m). The following
(comma-separated) options are supported:
false) — mangle names visible in scopes where
) — 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=['