Skip to content

Oxc Transformer Alpha 版本


我们很高兴宣布 Oxc transform(又名 transpile)的 alpha 版本发布。

此版本包含三个主要功能:

  1. 将 TypeScript 转换为 ESNext。
  2. 将 React JSX 转换为 ESNext,内置 React Refresh。
  3. TypeScript Isolated Declarations DTS 生成 无需使用 TypeScript 编译器。

在这个 alpha 阶段,我们建议尝试这些功能以加快构建速度。

我们的基准测试 显示:

  • Transform:Oxc 比 SWC 快 3 倍 - 5 倍,内存使用减少 20%,包体积更小(2 MB 对比 SWC 的 37 MB)。
  • Transform:Oxc 比 Babel 快 20 倍 - 50 倍,内存使用减少 70%,体积小 19 MB,只需安装 2 个 npm 包,而 Babel 需要 170 个。
  • React 开发 + React Refresh:Oxc 比 SWC 快 5 倍,比 Babel 快 50 倍。
  • TS isolated declarations .d.ts 生成:Oxc 在典型文件上比 TSC 快 40 倍,在较大文件上快 20 倍。

使用示例

oxc-transform npm 包

Vue.js 目前在其构建管道中 实验 使用 oxc-transform npm 包进行 isolated declarations:

javascript
import { isolatedDeclaration } from "oxc-transform";
const dts = isolatedDeclaration(filename, ts);

@lukeed@maraisr 正在他们的包 empathicdldr 中使用 oxc-transform单步完成 转换和生成 .d.ts

以下示例演示了在单个转换步骤中发出 .js.d.ts

javascript
import { transform } from "oxc-transform";
const transformed = transform(filePath, sourceCode, {
  typescript: {
    onlyRemoveTypeImports: true,
    declaration: { stripInternal: true },
  },
});
await fs.writeFile("out.js", transformed.code);
await fs.writeFile("out.d.ts", transformed.declaration);

unplugin-isolated-decl

vue-macros 使用 unplugin-isolated-decl 作为其 esbuild 插件的集成工具。

@sxzz 报告 他们的 .d.ts 生成时间从 76 秒减少到 16 秒。

Airtable 的 Bazel 构建

来自 Airtable@michaelm 正在其 Bazel 构建 的 CI 管道中集成 Oxc 的 isolated declarations .d.ts 生成功能。

Rust oxc_transformer crate

Rolldown 打包器 直接 使用 oxc_transformer Rust crate。

基准测试结果

基准测试设置位于 oxc-project/bench-transformer 并且基准测试结果显示在其 GitHub Actions 中。

(欢迎纠正任何配置错误。)

ubuntu-latest 上,测量了不同代码行数的示例:

转换

行数oxcswcbabel
~1000.14 ms0.7 ms (5x)11.5 ms (82x)
~10000.9 ms5.7 ms (6.3x)38.7 ms (43x)
~1000014.9 ms35.9 ms(2.4x)492 ms (33x)

Isolated Declarations

行数oxctsc
~1000.1 ms23.1 ms (231x)
~10003.1 ms26.8 ms (8.6x)
~100003.5 ms115.2 ms (33x)

包大小

Oxc 仅下载 2 个 npm 包,总共 2 MB。

大小
@oxc-transform/binding-darwin-arm642.0 MB
@swc/core-darwin-arm6437.5 MB
@babel/core + @babel/preset-env + @babel/preset-react + @babel/preset-typescript21 MB 和 170 个包

内存使用

Oxc 使用更少的内存。

转换 parser.ts(10777 行)的内存使用 - 使用 /usr/bin/time -alh node 测量:

最大 RSS
oxc51 MB
swc67 MB
babel172 MB

下一个版本

我们的下一个版本将包括目标降级到 ES6 和 @babel/plugin-transform-modules-commonjs

致谢

感谢 @Dunqing@overlookmotel 为本版本发布所做的所有辛勤工作。

感谢 snyder.techschoolhouse.world@lukeed@maraisr 的慷慨 赞助