Transformer(转换器)
一个高性能的转换器,将不支持的语法重写为目标运行时支持的形式。
功能特性
- 将 ESNext 降级到 ES2015。
- 将 TypeScript 转换为 JavaScript。
- 将 JSX 转换为 JavaScript,内置 React Refresh。
- 内置支持热门插件,如 styled-components。
- 替换全局变量。
- 无需使用 TypeScript 编译器即可生成 TypeScript 隔离声明。
通用选项
transform 函数接受文件名、源代码和选项对象:
js
import { transform } from "oxc-transform";
const result = await transform("lib.ts", sourceCode, {
// 强制指定源语言。默认从文件名推断。
lang: "tsx", // "js" | "jsx" | "ts" | "tsx" | "dts"
// 将源代码视为 script、module 或 CommonJS。默认自动推断。
sourceType: "module", // "script" | "module" | "commonjs" | "unambiguous"
// 当前工作目录。用于解析相对路径。
cwd: "/path/to/project",
// 启用 source map 生成。
sourcemap: true,
// 配置运行时辅助函数策略。
helpers: {
mode: "Runtime", // "Runtime"(从 @oxc-project/runtime 导入)或 "External"(使用全局 babelHelpers)
},
// 详见子页面了解更多选项:
// typescript, jsx, target, assumptions, define, inject, decorator, plugins
});transform 函数是异步的。同步版本 transformSync 也提供相同签名。
安装
Node.js
- 使用 Node.js 绑定库 oxc-transform。
- 在 stackblitz 上试用。
Rust
使用带有 transformer feature 的总览 crate oxc。
Rust 使用示例可以在这里找到。