API Reference

SWC

Rust-based JavaScript and TypeScript compiler exposing transform and parse APIs that replace Babel with much faster builds.

Official Documentation ↗
CompatibilityThis documentation covers version v1 (@swc/core). Check the official docs for the latest API surface.

Installation

npmnpm install --save-dev @swc/core
pnpmpnpm add -D @swc/core

Methods

Overview

SWC is a Rust-based JavaScript and TypeScript compiler offering transform and parse APIs that mirror Babel with far better throughput. This reference lists the most frequently used APIs with accurate signatures and runnable snippets.

Core APIs

  • transform — Asynchronously transforms a single source string and resolves with the generated code and map.
  • transformSync — Synchronous variant of transform for simple scripts and tooling.
  • parse — Parses source into an AST without emitting code.

Example

const swc = require('@swc/core');

const out = await swc.transform('const x: number = 1;', {
  jsc: { parser: { syntax: 'typescript' }, target: 'es2018' },
});
console.log(out.code);

Notes

SWC targets modern runtimes by default, so set the target explicitly when supporting older browsers. Prefer external helpers to avoid bloating every module.

Verification

Transform a TypeScript file with a typed parameter and confirm the type annotations are stripped in the emitted code.

Errors

The following points are quoted verbatim from the official documentation. 来源:https://swc.rs/docs/usage/core · 取证方式:HTTP GET 官方 README/文档页(HTML 去标签) · 抓取时间:2026-11-07T13:52:44.102Z. Any error type, throw condition or recovery step not stated there is left unspecified; this page does not infer exception classes or return values.

  • Running Swc * in a monorepo subdirectory without “upward”, will cause Swc * to skip loading any swc.config.js files in the project root, * which can lead to unexpected errors and compilation failure. (https://swc.rs/docs/usage/core)
  • If no map is found, or the * map fails to load and parse, it will be silently discarded. (https://swc.rs/docs/usage/core)

FAQ

How do I enable JSX transformation?
Set jsc.parser.syntax to "ecmascript" with jsc.parser.jsx true, or use "typescript" with tsx true, plus jsc.transform.react for the runtime.
Does SWC replace Babel completely?
SWC covers most common transforms faster, but niche Babel plugins may lack equivalents, so some projects keep Babel for specific transforms.
How are external helpers handled?
Set jsc.externalHelpers true and install @swc/helpers so emitted code imports helpers instead of inlining them.