Skip to content

Transformer(转换器)

一个高性能的转换器,将不支持的语法重写为目标运行时支持的形式。

功能特性

通用选项

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

Rust

使用带有 transformer feature 的总览 crate oxc

Rust 使用示例可以在这里找到。

集成