一、 全局安装uglify-js

npm下载地址:https://www.npmjs.com/package/uglify-js

安装:

> npm install uglify-js -g

查看帮助

> uglifyjs -h
Usage: uglifyjs [options] [files...]

Options:
  -V, --version                            output the version number
  -p, --parse <options>                    Specify parser options.
  -c, --compress [options]                 Enable compressor/specify compressor options.
  -m, --mangle [options]                   Mangle names/specify mangler options.
  --mangle-props [options]                 Mangle properties/specify mangler options.
  -b, --beautify [options]                 Beautify output/specify output options.
  -o, --output <file>                      Output file (default STDOUT).
  --comments [filter]                      Preserve copyright comments in the output.
  --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).
  --ie8                                    Support non-standard Internet Explorer 8.
  --keep-fnames                            Do not mangle/drop function names. Useful for code relying on Function.prototype.name.
  --name-cache <file>                      File to hold mangled name mappings.
  --rename                                 Force symbol expansion.
  --no-rename                              Disable symbol expansion.
  --self                                   Build UglifyJS as a library (implies --wrap UglifyJS)
  --source-map [options]                   Enable source map/specify source map options.
  --timings                                Display operations run time on STDERR.
  --toplevel                               Compress and/or mangle variables in toplevel scope.
  --verbose                                Print diagnostic messages.
  --warn                                   Print warning messages.
  --wrap <name>                            Embed everything as a function with “exports” corresponding to “name” globally.
  -h, --help                               output usage information

示例

目录结构

├─ uglifyjs_test
    ├─ index.min.js
    └─ index.js

执行压缩命令(压缩及混淆)

uglifyjs_test> uglifyjs index.js -c -m -o index.min.js