# esbuild

**esbuild** 是一款面向 Web 代码的开源打包与压缩工具，由 Evan Wallace 用 Go 语言编写，于 2019–2020 年间发布。它能够解析代码库中的 `import` 与 `require` 语句，构建依赖图，并输出打包后的文件，内置摇树优化（从最终产物中移除未使用的代码）、压缩和源码映射功能。它最核心的卖点是速度：该项目主张，Web 现有的构建工具比它们本可以达到的速度慢 10 到 100 倍，而 esbuild 正是为了证明打包能跑得多快而写的。<sup>[1](https://github.com/evanw/esbuild)</sup><sup> • </sup><sup>[2](https://www.centerconsulting.com/code-library/tools/esbuild)</sup> 它采用 MIT 许可证分发，GitHub 仓库约有 4.01 万颗星。<sup>[1](https://github.com/evanw/esbuild)</sup>

| 关键事实 | 详情 |
|---|---|
| 作者与起源 | Evan Wallace，在 2019–2020 年寒假期间作为业余项目写成；最早的 npm 版本发布于 2019–2020 年<sup>[3](https://www.bram.us/2020/03/02/esbuild-an-extremely-fast-javascript-bundler-and-minifier/)</sup><sup> • </sup><sup>[4](https://www.npmjs.com/package/esbuild?activeTab=versions)</sup> |
| 实现方式 | Go 语言，编译为原生代码；一切从零写起，包括一个自制的 TypeScript 解析器<sup>[5](https://esbuild.github.io/faq/)</sup> |
| 内置功能 | 支持 JavaScript、CSS、TypeScript 和 JSX；可打包 ESM 与 CommonJS；摇树优化、压缩、源码映射；本地服务器、监听模式和插件<sup>[1](https://github.com/evanw/esbuild)</sup> |
| 基准测试速度 | 在一台 2019 款 6 核 MacBook Pro 上，打包并压缩一个大型 JavaScript 代码库（将 three.js 复制 10 份，共 547,441 行）耗时 0.39 秒，而 webpack 5 需 41.21 秒，rollup 4 + terser 需 34.10 秒<sup>[5](https://esbuild.github.io/faq/)</sup> |
| TypeScript 处理方式 | 解析 TypeScript 并剥离类型标注，但不做类型检查；必须另行运行 `tsc --noEmit`<sup>[6](https://esbuild.github.io/content-types/)</sup> |
| 当今的典型角色 | 作为快速的转换与打包层嵌入其他工具之中，尤以 Vite 的开发服务器为代表；AWS CDK 和 AWS Lambda 也用它来打包函数<sup>[7](https://lycoristechnologies.com/blog/esbuild-vs-rollup-vs-webpack/)</sup><sup> • </sup><sup>[8](https://docs.aws.amazon.com/lambda/latest/dg/typescript-package.html)</sup> |
| 当前版本 | v0.28.2，发布于 2026 年 8 月<sup>[9](https://github.com/evanw/esbuild/releases)</sup> |

## 为什么快

esbuild 的 FAQ 将速度归因于若干设计选择。<sup>[5](https://esbuild.github.io/faq/)</sup>

**原生代码与并行。** 其他大多数打包工具用 [JavaScript](https://www.edgepedia.org/zh/javascript) 编写，FAQ 称这对 JIT 编译语言而言是最糟糕的性能场景：每次运行都要先把打包工具自身的代码重新解析一遍，然后才能开始打包。Go 天生为并行而设计，线程之间共享内存；而 JavaScript 的线程必须序列化数据并各自维护独立的堆；FAQ 作者实测，这使 JavaScript worker 线程可实现的并行度大约减半。解析和代码生成——也就是工作量的大头——在所有 CPU 核心上完全并行化；而链接环节本质上大部分是串行的。<sup>[5](https://esbuild.github.io/faq/)</sup>

**对代码的遍历次数少。** esbuild 只对整个 JavaScript AST 做三次遍历：一次用于词法分析、解析、作用域建立和符号声明；一次用于符号绑定、语法压缩、JSX/TS 到 JS 的转换，以及 ESNext 到 ES2015 的降级；一次用于标识符与空白压缩、代码生成和源码映射生成。<sup>[5](https://esbuild.github.io/faq/)</sup> 架构文档将项目原则表述为：最大化并行度；避免不必要的工作，比如把 JavaScript 写出去再用另一个工具读回来；透明地支持同一文件中同时存在 ES6 与 CommonJS 语法；并合理组织数据结构，使监听模式下的增量构建能够跳过未变更文件的全 AST 遍历。<sup>[10](https://github.com/evanw/esbuild/blob/main/docs/architecture.md)</sup>

**自制的 TypeScript 解析器。** 许多打包工具使用官方 [TypeScript](https://www.edgepedia.org/zh/typescript) 编译器作为解析器，FAQ 批评其对象形状多态（megamorphic）、存在不必要的动态属性访问，而且似乎在禁用类型检查时仍会运行类型检查器。esbuild 则自行实现了自己的解析器。<sup>[5](https://esbuild.github.io/faq/)</sup>

## 构建是如何进行的

构建流水线分为两个主要阶段，均在 `bundler.go` 中实现。<sup>[10](https://github.com/evanw/esbuild/blob/main/docs/architecture.md)</sup>

1. **扫描阶段。** 从入口点出发，一个并行的工作列表算法遍历依赖图。每个文件在独立的 goroutine 中被解析为 AST，并可能通过 ES6 `import` 语句、`import()` 表达式或 CommonJS `require()` 表达式向工作列表中添加更多文件，直到工作列表为空。
2. **编译阶段。** 对每个入口点，将导入与导出链接起来，把 AST 转回 JavaScript，再将结果拼接成最终的打包文件。

打包并非默认行为：不带 `--bundle` 的 `esbuild in.js` 不会内联依赖，必须显式启用打包。<sup>[11](https://esbuild.github.io/api/)</sup>

## API 接口与插件

esbuild 提供 CLI、JavaScript API 和 Go API。**转换 API** 是构建 API 的一个受限特例：它在一个与其他文件隔离的环境中转换一段代码字符串，因此可以在浏览器中使用，也不受附近 `package.json` 文件的影响，但与打包和插件不兼容。**构建 API** 则是通用形式。<sup>[11](https://esbuild.github.io/api/)</sup> 该 API 还可以通过独立的 `esbuild-wasm` 包在浏览器中运行，它使用 Web Worker 中的 [WebAssembly](https://www.edgepedia.org/zh/webassembly)，使用前需要调用 `initialize()`。<sup>[11](https://esbuild.github.io/api/)</sup>

**插件 API** 有意比 webpack 的更窄。一个插件是一个带有 `name` 属性和 `setup` 函数的对象，传给构建 API 调用；插件无法从命令行使用，也不能与转换 API 一起使用。插件通过 `onResolve` 和 `onLoad` 回调挂接到解析与加载环节，并可以在默认 `file` 命名空间之外的命名空间中创建虚拟模块。<sup>[12](https://esbuild.github.io/plugins/)</sup> Wallace 在 2020 年写道，在打包器之外再构建一套即插即用的架构，会以牺牲性能为代价，而且 esbuild 当时仍是一个实验。<sup>[13](https://github.com/evanw/esbuild/issues/101)</sup>

## TypeScript 与压缩行为

esbuild 的 TypeScript 加载器对 `.ts`、`.tsx`、`.mts` 和 `.cts` 文件默认启用。它解析 TypeScript 语法并丢弃类型标注，把类型当作注释处理，这与 Babel 的 TypeScript 实现采用相同的编译策略。它不做类型检查，因此必须并行运行 `tsc --noEmit`，而需要类型解释的特性（例如 `emitDecoratorMetadata`）不受支持。<sup>[6](https://esbuild.github.io/content-types/)</sup>

作为压缩工具，esbuild 的目标是在运行速度快得多的同时，产出体积与专用压缩工具相差几个百分点以内的结果，并不追求在所有情况下都最优。文档中列出的注意事项包括：它不保证在函数上调用 `.toString()` 的返回值在压缩前后保持不变，因此依赖这一点的代码（例如某些 [AngularJS](https://www.edgepedia.org/zh/angularjs) 模式）在压缩后会出错；它默认不保留函数和类的 `.name` 属性；直接使用 `eval` 或 `with` 语句会因绑定发生在运行时，而导致整个文件的标识符重命名被禁用。<sup>[11](https://esbuild.github.io/api/)</sup>

## 与其他打包工具相比的局限

与 **webpack** 相比，esbuild 以可配置性换取速度。webpack 是一个插件与加载器平台，流水线中几乎每一部分都可替换，包括针对 CSS、图片和字体的加载器链；esbuild 则是一个更小、更快的核心，插件面更窄，依赖 webpack 加载器改写模块内容的构建，可能需要重写那部分流水线，而非重新配置。<sup>[14](https://hysenlabs.com/en/projects/evanw-esbuild)</sup> 与 **Rollup** 相比，差别在于实现语言和插件深度：Rollup 基于 JavaScript，其插件 API 能更细致地操作模块图；而 esbuild 是一个 Go 二进制文件，插件面更窄。<sup>[14](https://hysenlabs.com/en/projects/evanw-esbuild)</sup> esbuild 也不附带支持热模块替换的开发服务器，也不提供 Vite 之类工具在其之上提供的框架集成，而且它的代码分割支持在历史上一直落后于更成熟的打包工具。<sup>[14](https://hysenlabs.com/en/projects/evanw-esbuild)</sup><sup> • </sup><sup>[15](https://www.softwr.com/compare/esbuild-vs-rollup)</sup>

## 在其他工具内部的使用

大多数开发者是间接接触到 esbuild 的。**Vite** 使用 esbuild 进行快速的开发期转换和依赖预打包，而生产构建则使用 Rollup；Vite 自己的文档将冷启动之快归功于 esbuild。<sup>[7](https://lycoristechnologies.com/blog/esbuild-vs-rollup-vs-webpack/)</sup><sup> • </sup><sup>[14](https://hysenlabs.com/en/projects/evanw-esbuild)</sup>

**AWS** 的工具链直接嵌入 esbuild：AWS CDK 的 `NodejsFunction` 构造用 esbuild 打包 Lambda 函数，[AWS Lambda](https://www.edgepedia.org/zh/aws-lambda) 文档展示了诸如 `esbuild index.ts --bundle --minify --sourcemap --platform=node --target=es2020 --outfile=dist/index.js` 的构建脚本，产出一个用于部署的 JavaScript 文件。<sup>[8](https://docs.aws.amazon.com/lambda/latest/dg/typescript-package.html)</sup><sup> • </sup><sup>[17](https://github.com/mrgrain/cdk-esbuild)</sup>

## 安全

2026 年 6 月发布的一份 GitHub 安全公告 GHSA-g7r4-m6w7-qqqr 描述了 esbuild 在 Windows 上开发服务器中的一个路径遍历漏洞：由于请求路径使用 Go 的 POSIX 风格 `path.Clean()` 进行清理，而该函数不把反斜杠视为分隔符，使用 `\` 构造的请求可以逃逸出配置的 `servedir` 根目录，读取任意文件。0.27.3 版本受影响，0.28.1 及之后版本已修复；该问题被评为低危，CVSS 评分为 2.5。<sup>[18](https://github.com/evanw/esbuild/security/advisories/GHSA-g7r4-m6w7-qqqr)</sup>

## References

1. [evanw/esbuild: An extremely fast bundler for the web, GitHub](https://github.com/evanw/esbuild)
2. [esbuild, CenterConsulting code library](https://www.centerconsulting.com/code-library/tools/esbuild)
3. [esbuild – An extremely fast JavaScript bundler and minifier, Bram.us (March 2, 2020)](https://www.bram.us/2020/03/02/esbuild-an-extremely-fast-javascript-bundler-and-minifier/)
4. [esbuild version history, npm](https://www.npmjs.com/package/esbuild?activeTab=versions)
5. [esbuild FAQ: Why is esbuild fast? and Benchmark details](https://esbuild.github.io/faq/)
6. [esbuild Content Types: TypeScript](https://esbuild.github.io/content-types/)
7. [esbuild vs Rollup vs webpack: Comparing JS Bundlers, Lycoris Technologies](https://lycoristechnologies.com/blog/esbuild-vs-rollup-vs-webpack/)
8. [Deploy transpiled TypeScript code in Lambda with .zip file archives, AWS Lambda documentation](https://docs.aws.amazon.com/lambda/latest/dg/typescript-package.html)
9. [Releases · evanw/esbuild, GitHub](https://github.com/evanw/esbuild/releases)
10. [esbuild architecture documentation, GitHub](https://github.com/evanw/esbuild/blob/main/docs/architecture.md)
11. [esbuild API documentation](https://esbuild.github.io/api/)
12. [esbuild Plugins documentation](https://esbuild.github.io/plugins/)
13. [Question: Do you reimplement the typescript ast? · Issue #101 · evanw/esbuild](https://github.com/evanw/esbuild/issues/101)
14. [esbuild: install, first bundle and how it compares, Hysen Labs](https://hysenlabs.com/en/projects/evanw-esbuild)
15. [esbuild vs Rollup: Web Development compared, Softwr](https://www.softwr.com/compare/esbuild-vs-rollup)
16. [Dependency Pre-Bundling, Vite documentation](https://vite.dev/guide/dep-pre-bundling)
17. [mrgrain/cdk-esbuild: CDK constructs for esbuild, GitHub](https://github.com/mrgrain/cdk-esbuild)
18. [Arbitrary file read when running the development server on Windows · GHSA-g7r4-m6w7-qqqr, GitHub advisory](https://github.com/evanw/esbuild/security/advisories/GHSA-g7r4-m6w7-qqqr)

---
*Initially written Oct 15, 2026*

Topic: 技术与人造世界 › 计算技术与数字系统 › 软件与编程 › Development tools and collaboration infrastructure › Build, deployment, and development tooling › JavaScript bundlers and build tooling

Copyright 2026 Edgepedia

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

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

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