Oxc Transform Node API

0.126.0 · active · verified Sun Apr 19

oxc-transform is the Node.js API for Oxc's high-performance JavaScript, TypeScript, and React JSX transformer, which is built in Rust. It provides synchronous (`transformSync`, `isolatedDeclarationSync`) and asynchronous (`transform`, `isolatedDeclaration`) functions for parsing and transforming code. A key feature is its specialized capability for generating TypeScript declaration files (`.d.ts`) with isolated declarations, fully conforming to TypeScript's `--isolatedDeclarations` compiler option. Currently at version 0.126.0, the package maintains a rapid release cadence, reflecting active and continuous development within the broader Oxc project ecosystem. Its primary differentiators include its Rust-native performance, positioning it as a fast and efficient alternative to tools like Babel, and its robust, up-to-date support for modern ECMAScript features, TypeScript syntax, and React JSX out-of-the-box. The library is designed to serve as a foundational, high-performance component for various JavaScript build and tooling pipelines requiring efficient code transformation.

Common errors

Warnings

Install

Imports

Quickstart

This example demonstrates how to use `transformSync` to transpile TypeScript code to JavaScript and simultaneously generate a `.d.ts` declaration file with isolated declarations, including basic options for JSX and React.

import assert from "assert";
import { transformSync } from "oxc-transform";

const filename = "test.ts";
const sourceCode = "class A<T> { constructor(arg: T) { console.log(arg); } }";

// Transform TypeScript code to plain JavaScript and generate a declaration file
const { code, declaration, errors } = transformSync(filename, sourceCode, {
  typescript: {
    declaration: true, // Enable isolated declarations
    jsxPragma: "React",
    jsxPragmaFrag: "React.Fragment"
  },
  // Example: Enable more transformations if needed
  react: {
    runtime: "automatic",
    development: true
  }
});

// Log results for verification
console.log("Transformed Code:\n", code);
console.log("Declaration File:\n", declaration);

// Basic assertions
assert.equal(code.trim(), "class A { constructor(arg) { console.log(arg); } }");
assert.equal(declaration.trim(), "declare class A<T> {\n    constructor(arg: T);\n}");
assert.equal(errors.length, 0, `Expected no errors, but got ${errors.length} errors.`);

console.log("Transformation successful!");

view raw JSON →