# UglifyJS

**UglifyJS** 是一个为 Node.js 编写的 [JavaScript](https://www.edgepedia.org/zh/javascript) 解析器、压缩器、代码精简器和美化工具包，以 npm 包 `uglify-js` 的形式发布<sup>[1](https://github.com/mishoo/UglifyJS)</sup>。它将 JavaScript 解析为抽象语法树（AST），然后重写该树以缩小代码：将变量重命名为短名称（变量名混淆）、移除不可达代码，并重新输出紧凑的 JavaScript。同一套流水线也可以反向运行，将压缩后的代码重新格式化为带缩进的可读形式<sup>[1](https://github.com/mishoo/UglifyJS)</sup><sup> • </sup><sup>[2](https://github.com/oyvindkinsey/UglifyJS)</sup>。

| 关键事实 | 详情 |
|---|---|
| 是什么 | 面向 Node.js 的 JavaScript 解析器、压缩器、代码精简器和美化工具包<sup>[1](https://github.com/mishoo/UglifyJS)</sup> |
| 安装 | 命令行工具：`npm install uglify-js -g`；编程调用：`npm install uglify-js`<sup>[1](https://github.com/mishoo/UglifyJS)</sup> |
| 语言支持 | 支持 JavaScript 及大多数 ECMAScript 特性；更特殊的语法应先用 Babel 等工具转译后再压缩<sup>[1](https://github.com/mishoo/UglifyJS)</sup> |
| 版本 3 | `uglify-js@3` 的 API 和 CLI 经过简化，与 `uglify-js@2` 不向后兼容<sup>[1](https://github.com/mishoo/UglifyJS)</sup> |
| 压缩效果 | 在一项公开基准测试中，关闭压缩的情况下，它在 670 毫秒内将测试输入的原始大小缩减了 53%（至 472.16 KB），gzip 后大小缩减了 36%（至 124.61 KB）<sup>[5](https://github.com/privatenumber/minification-benchmarks/blob/master/README.md)</sup> |
| 对比 | 在经典库上取得了约 67% 的最佳原始大小缩减率，但在大输入上比 Terser 慢，且仅支持 ES5 语法<sup>[4](https://www.xconvert.com/js-minifier)</sup> |
| Webpack 集成 | `uglifyjs-webpack-plugin`（版本 2.2.0，MIT 许可，Node >= 6.9.0）将 UglifyJS 接入了 webpack 构建<sup>[7](https://github.com/webpack-contrib/uglifyjs-webpack-plugin/blob/master/package.json)</sup> |

## 工作原理

该工具包操作的是抽象语法树而非文本。分词器和解析器从 JavaScript 源码生成 AST；处理阶段则检查并操作该树，并可从中重新生成 JavaScript 代码<sup>[2](https://github.com/oyvindkinsey/UglifyJS)</sup>。在最初的实现中，解析器是 Marijn Haverbeke 编写的 [Common Lisp](https://www.edgepedia.org/zh/common-lisp) 库 parse-js 到 JavaScript 的移植版本。Marijn Haverbeke 是一位程序员和作家，以 JavaScript 工具方面的工作而闻名<sup>[2](https://github.com/oyvindkinsey/UglifyJS)</sup>。

三种变换定义了该工具。**变量名混淆**生成一棵新的 AST，将变量名和函数名替换为压缩后的名称；默认情况下顶层作用域中的名称保持不变，但 `toplevel` 选项可将混淆扩展到顶层<sup>[2](https://github.com/oyvindkinsey/UglifyJS)</sup><sup> • </sup><sup>[1](https://github.com/mishoo/UglifyJS)</sup>。**压缩**应用减小体积的优化：移除明显不可达的代码，例如 `return`、`throw`、`break` 或 `continue` 之后非声明性的语句；并将块中连续的语句归约为序列表达式，于是 `a = 10; b = 20; foo();` 变成 `a=10,b=20,foo();`，从而可以去掉块的花括号<sup>[3](https://manpages.ubuntu.com/manpages/trusty/man1/uglifyjs.1.html)</sup>。**美化**以缩进重新生成代码，初始缩进和缩进层级均可配置，使压缩后的文件重新变得可读<sup>[2](https://github.com/oyvindkinsey/UglifyJS)</sup>。

美化器的一个局限是它会丢弃所有注释。注释在解析阶段构建初始 AST 时就被丢弃，因为它们不符合树结构；因此美化器不能用于重新格式化注释有意义的代码<sup>[2](https://github.com/oyvindkinsey/UglifyJS)</sup>。默认情况下，压缩器会在生成的输出中保留开头的注释词元，假定它们携带版权信息<sup>[3](https://manpages.ubuntu.com/manpages/trusty/man1/uglifyjs.1.html)</sup>。

## 命令行用法

CLI 的形式为 `uglifyjs [输入文件] [选项]`。多个输入文件按顺序在同一全局作用域中解析，因此一个文件中对另一个文件中声明的变量的引用可以正确解析；没有输入文件时，工具从标准输入读取<sup>[1](https://github.com/mishoo/UglifyJS)</sup>。压缩通过 `--compress`（`-c`）启用，变量名混淆通过 `--mangle`（`-m`）启用，各自接受逗号分隔的选项，例如 `toplevel` 用于处理顶层作用域，`reserved` 用于列出不得更改的名称，例如 `require`、`exports` 和 `$`<sup>[1](https://github.com/mishoo/UglifyJS)</sup>。

一个典型的调用将多个文件压缩并混淆为带 source map 的单个输出：

```
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'"
```

这将压缩后的代码写入 `foo.min.js`，source map 写入 `foo.min.js.map`，map 的根指向原始源码树<sup>[1](https://github.com/mishoo/UglifyJS)</sup>。

## Source map

UglifyJS 可以生成 source map 文件，将压缩输出中的位置映射回原始源码，以便调试器显示可读的代码<sup>[1](https://github.com/mishoo/UglifyJS)</sup>。选项控制 map 的 `file` 属性、原始源码的根 URL、是否包含符号名称（省略可缩小 map），以及 `//# sourceMappingURL=` 指令引用的 URL；如果省略该 URL，工具假定改用 HTTP `X-SourceMap` 头<sup>[1](https://github.com/mishoo/UglifyJS)</sup>。

该工具还支持组合 source map。当输入的 JavaScript 本身是由 [CoffeeScript](https://www.edgepedia.org/zh/coffeescript) 等编译器生成时，指向中间 JavaScript 的 map 用处有限；UglifyJS 可以接受输入的 source map，通过将编译后 JavaScript 中的每个词元映射到其原始位置，生成一个从原始 CoffeeScript 一直到压缩输出的 map<sup>[1](https://github.com/mishoo/UglifyJS)</sup>。

## 库 API

以编程方式使用时，`minify()` 函数接受一个文件名到源码的映射和一个选项对象，选项对象包含 `parse`、`compress`、`mangle`（含用于混淆属性名的 `properties` 子对象）、`output` 和 `sourceMap` 等部分，以及 `toplevel`、`warnings` 和 `nameCache` 等标志<sup>[1](https://github.com/mishoo/UglifyJS)</sup>。`nameCache` 对象在压缩过程中被读取和更新，因此混淆后的变量名和属性名可以在多次调用之间保持一致，或持久化到外部<sup>[1](https://github.com/mishoo/UglifyJS)</sup>。结果对象在 `result.code` 中返回压缩后的代码，并在请求时在 `result.map` 中返回 source map；map 是返回而非写入磁盘<sup>[1](https://github.com/mishoo/UglifyJS)</sup>。

组合各阶段时顺序很重要：如果代码既要压缩又要混淆，混淆应在压缩之后运行，因为压缩器可能丢弃未使用的标识符和不可达代码，而这些原本会被重命名<sup>[8](https://wohugb.gitbooks.io/uglifyjs/content/mangle.html)</sup>。

## ECMAScript 支持与 Terser 分叉

`uglify-js` 支持 JavaScript 及大多数 [ECMAScript](https://www.edgepedia.org/zh/ecmascript) 语言特性，但项目自己的文档建议将更特殊的语法先通过 Babel 等转译器处理后再压缩<sup>[1](https://github.com/mishoo/UglifyJS)</sup>。一张对比表将该工具描述为仅支持 ES5，直接拒绝现代语法，但在经典库上实现了最高的绝对压缩率<sup>[4](https://www.xconvert.com/js-minifier)</sup>。

Terser 是延续 UglifyJS 路线并支持现代语法的分叉，也是最常与之比较的工具；基准测试显示 UglifyJS 在大输入上比 Terser 慢<sup>[4](https://www.xconvert.com/js-minifier)</sup>，在关闭压缩的正面对比中，同一输入上 terser 在 805 毫秒内产出 472.58 KB 原始大小和 124.25 KB gzip 后大小，而 uglify-js 在 670 毫秒内产出 472.16 KB 和 124.61 KB<sup>[5](https://github.com/privatenumber/minification-benchmarks/blob/master/README.md)</sup>。

## 在构建流水线中的使用

UglifyJS 一直是生产环境 JavaScript 构建的标准步骤。Webpack 通过 `webpack.optimize.UglifyJsPlugin` 以及后来的独立插件 `uglifyjs-webpack-plugin` 暴露它，后者支持完整的选项集，包括属性名混淆和要保留的变量名例外列表<sup>[7](https://github.com/webpack-contrib/uglifyjs-webpack-plugin/blob/master/package.json)</sup><sup> • </sup><sup>[6](https://github.com/webpack/docs/wiki/list-of-plugins)</sup>。webpack 1 时代的 Angular 构建指南为该插件配置了 `mangle: { keep_fnames: true }`，并搭配使用 extract-text 和 define 插件<sup>[9](https://static.infragistics.com/marketing/Website/Ignite-UI/Angular-Essentials/angularessentials.pdf)</sup>。关于 JavaScript 混淆与压缩的学术工作将 UglifyJS 与 Google Closure Compiler 一起用作参考压缩工具<sup>[10](https://sigarra.up.pt/feup/en/pub_geral.show_file?pi_doc_id=302972)</sup>。

## References

1. [UglifyJS 3 README, mishoo/UglifyJS, GitHub](https://github.com/mishoo/UglifyJS)
2. [UglifyJS README (earlier version), oyvindkinsey/UglifyJS, GitHub](https://github.com/oyvindkinsey/UglifyJS)
3. [uglifyjs(1) man page, Ubuntu Manpages](https://manpages.ubuntu.com/manpages/trusty/man1/uglifyjs.1.html)
4. [JS Minifier comparison table, XConvert](https://www.xconvert.com/js-minifier)
5. [JS minification benchmarks, privatenumber/minification-benchmarks, GitHub](https://github.com/privatenumber/minification-benchmarks/blob/master/README.md)
6. [List of plugins, webpack/docs Wiki, GitHub](https://github.com/webpack/docs/wiki/list-of-plugins)
7. [uglifyjs-webpack-plugin package.json, webpack-contrib, GitHub](https://github.com/webpack-contrib/uglifyjs-webpack-plugin/blob/master/package.json)
8. [UglifyJS mangling documentation, wohugb gitbook](https://wohugb.gitbooks.io/uglifyjs/content/mangle.html)
9. [Angular Essentials, Infragistics](https://static.infragistics.com/marketing/Website/Ignite-UI/Angular-Essentials/angularessentials.pdf)
10. [Minification study using UglifyJS and Google Closure Compiler, FEUP/U. Porto](https://sigarra.up.pt/feup/en/pub_geral.show_file?pi_doc_id=302972)

---
*Initially written Oct 15, 2026*

Topic: 技术与人造世界 › 计算技术与数字系统 › 软件与编程 › 开发工具与协作基础设施 › 构建、部署与开发工具 › 包管理器

Copyright 2026 Edgepedia

License: Edgepedia Community License 1.0, https://www.edgepedia.org/edgepedia/license

Source: https://www.edgepedia.org/zh/uglifyjs

English original: https://www.edgepedia.org/uglifyjs
