压缩器
🌐 Minifier
JavaScript 压缩在优化网站性能方面起着关键作用,因为它减少了发送给用户的数据量,从而加快页面加载速度。这具有巨大的经济价值,尤其对于电子商务网站而言,每一秒都可能意味着数百万美元的收益。
🌐 JavaScript minification plays a crucial role in optimizing website performance as it reduces the amount of data sent to users, resulting in faster page loads. This holds tremendous economic value, particularly for e-commerce websites, where every second can equate to millions of dollars.
然而,现有的压缩器通常需要在压缩质量和速度之间做出权衡。 你得在最慢的那个获得最佳压缩和最快的最好压缩率之间做选择。 但如果我们能开发出更快的压缩器而不牺牲压缩,会怎样?
🌐 However, existing minifiers typically require a trade-off between compression quality and speed. You have to choose between the slowest for the best compression or the fastest for less compression. But what if we could develop a faster minifier without compromising on compression?
项目目标
🌐 Project Goals
我们正在积极开发一个原型,旨在通过移植来自知名压缩工具(如 google-closure-compiler、terser、esbuild 和 tdewolff-minify)的所有测试用例来实现这一目标。
🌐 We are actively working on a prototype that aims to achieve this goal, by porting all test cases from well-known minifiers such as google-closure-compiler, terser, esbuild, and tdewolff-minify.
初步结果显示,我们正按计划实现我们的目标。使用 Oxc 压缩器,你可以在不牺牲压缩质量的情况下,期待更快的压缩时间。
🌐 Preliminary results indicate that we are on track to achieve our objectives. With the Oxc minifier, you can expect faster minification times without sacrificing compression quality.
目标性能
🌐 Target Performance
- 速度:比 Terser 更快,与 esbuild 竞争
- 压缩:匹配或超过 Terser 的压缩比
- 正确性:通过所有主要的压缩器测试套件
架构概览
🌐 Architecture Overview
设计原则
🌐 Design Principles
Oxc 压缩器的构建基于几个关键原则:
🌐 The Oxc minifier is built around several key principles:
- 语义感知:使用语义分析实现安全优化
- 增量:为增量编译工作流而设计
- 可配置:支持多种优化级别和目标
- 正确:优先保证正确性,而不是追求激进的优化
当前状态
🌐 Current Status
已实现的功能
🌐 Implemented Features
- ✅ 死代码消除:移除不可达代码
- ✅ 常量折叠:计算常量表达式
- ✅ Tree Shaking:移除未使用的导出(基础)
- ✅ 变量合并:合并变量声明
- ✅ 语句合并:结合兼容的语句
- ✅ 名称混淆:缩短变量和函数名称
- ✅ 控制流优化:简化控制结构
- ✅ 函数内联:内联小函数
- ✅ 高级树摇优化:跨模块优化
性能优化
🌐 Performance Optimization
保持性能的关键策略:
🌐 Key strategies for maintaining performance:
- 最小 AST 遍历:在单次遍历中结合多种优化
- 高效数据结构:使用区域分配和紧凑表示
- 提前终止:在没有可能带来好处时跳过优化
资源
🌐 Resources
文档
🌐 Documentation
外部参考资料
🌐 External References
游乐场
🌐 Playgrounds
- esbuild: https://esbuild.github.io/try
- rollup: https://rollup.nodejs.cn/rep
- swc: https://play.swc.rs
- terser: https://try.terser.org
- 闭包编译器: https://jscompressor.treblereel.dev
