Oxc 轉換器 Alpha
我們很高興宣布 Oxc 轉換 (又稱轉譯) 的 Alpha 版本發佈。
此版本包含三個主要功能
- 將 TypeScript 轉換為 ESNext。
- 將 React JSX 轉換為 ESNext,並內建 React Refresh。
- TypeScript 隔離宣告 DTS 發射,無需使用 TypeScript 編譯器。
在這個 Alpha 階段,我們建議您實驗這些功能以加快建置時間。
我們的基準測試顯示
- 轉換:Oxc 比 SWC 快 3 倍 - 5 倍,使用的記憶體減少 20%,並且套件大小更小(2 MB 對比 SWC 的 37 MB)。
- 轉換:Oxc 比 Babel 快 20 倍 - 50 倍,使用的記憶體減少 70%,並且小 19 MB,僅需安裝 2 個 npm 套件,而 Babel 則需要 170 個。
- React 開發 + React Refresh:Oxc 比 SWC 快 5 倍,比 Babel 快 50 倍。
- TS 隔離宣告
.d.ts
發射:Oxc 在典型檔案上比 TSC 快 40 倍,在較大檔案上快 20 倍。
使用範例
oxc-transform
npm 套件
Vue.js 目前正在 試用 oxc-transform
npm 套件在其建置管道中用於隔離宣告
import { isolatedDeclaration } from "oxc-transform";
const dts = isolatedDeclaration(filename, ts);
@lukeed 和 @maraisr 正在為他們的套件 empathic
和 dldr
使用 oxc-transform
來轉換並產生 .d.ts
在單一步驟中。
以下範例展示了在單一轉換步驟中發射 .js
和 .d.ts
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 的隔離宣告 .d.ts
發射。
Rust oxc_transformer
crate
Rolldown bundler 直接使用 oxc_transformer
Rust crate。
基準測試結果
基準測試設定位於 oxc-project/bench-transformer,基準測試顯示在其 GitHub Actions 中。
(歡迎針對任何錯誤配置進行更正。)
在 ubuntu-latest
上,測量了不同程式碼行的範例
轉換
行數 | oxc | swc | babel |
---|---|---|---|
~100 | 0.14 毫秒 | 0.7 毫秒 (5 倍) | 11.5 毫秒 (82 倍) |
~1000 | 0.9 毫秒 | 5.7 毫秒 (6.3 倍) | 38.7 毫秒 (43 倍) |
~10000 | 14.9 毫秒 | 35.9 毫秒(2.4 倍) | 492 毫秒 (33 倍) |
隔離宣告
行數 | oxc | tsc |
---|---|---|
~100 | 0.1 毫秒 | 23.1 毫秒 (231 倍) |
~1000 | 3.1 毫秒 | 26.8 毫秒 (8.6 倍) |
~10000 | 3.5 毫秒 | 115.2 毫秒 (33 倍) |
套件大小
Oxc 僅下載 2 個 npm 套件,總共 2 MB。
套件 | 大小 |
---|---|
@oxc-transform/binding-darwin-arm64 | 2.0 MB |
@swc/core-darwin-arm64 | 37.5 MB |
@babel/core + @babel/preset-env + @babel/preset-react + @babel/preset-typescript | 21 MB 和 170 個套件 |
記憶體使用量
Oxc 使用較少的記憶體。
使用 /usr/bin/time -alh node
測量的轉換 parser.ts
(10777 行) 的記憶體使用量
最大 RSS | |
---|---|
oxc | 51 MB |
swc | 67 MB |
babel | 172 MB |
下一個版本
我們的下一個版本將包含將目標降級至 ES6 和 @babel/plugin-transform-modules-commonjs
。
致謝
感謝 @Dunqing 和 @overlookmotel 為此版本付出的所有辛勞。
感謝 snyder.tech、schoolhouse.world、@lukeed 和 @maraisr 的慷慨贊助。