SWC
A super-fast JavaScript and TypeScript compiler written in Rust — used by Next.js, Deno, Parcel, and others as a drop-in replacement for Babel.
SWC (Speedy Web Compiler) is a Rust-based platform for the JavaScript ecosystem. At its core it is a JavaScript and TypeScript compiler that can transpile, bundle, and minify code — performing the same job as Babel, but 20–70× faster on a single thread and 70× faster on four cores. It is used in production by Next.js, Deno, Parcel, and many other tools as their underlying compilation engine.
Features
- Extremely fast — 20–70× faster than Babel for single-threaded transpilation; scales further with multiple cores
- TypeScript support — strips TypeScript types and transforms TS syntax
without a separate
tscpass - JSX support — transforms JSX for React and other frameworks
- Modern JS transforms — compiles modern JavaScript (ES2022+) down to targets like ES5 for broad browser compatibility
- Minification — a Terser-compatible minifier that is significantly faster than Terser itself
- Bundler — an experimental bundler (
spack) for tree-shaking and module bundling - Plugin system — extensible via WebAssembly plugins for custom transforms
- Drop-in Babel replacement — reads Babel config files and supports many Babel plugins
Installation
Install the CLI via npm (the Rust binary is distributed as an npm package):
npm install --save-dev @swc/core @swc/cli
# Or globally
npm install -g @swc/cli
# Debian / Ubuntu / Fedora
# npm install works on all Linux distributions (see above).
Or use it directly via npx:
npx @swc/cli src/index.tsUsage
# Compile a TypeScript file to JavaScript
swc src/index.ts -o dist/index.js
# Compile and watch for changes
swc src/index.ts -o dist/index.js --watch
# Compile an entire directory
swc src/ -d dist/
# Minify output
swc src/index.js -o dist/index.js --minify
# Target a specific environment
swc src/index.ts -o dist/index.js --env-targets "chrome >= 80"
# Print compiled output to stdout
swc src/index.tsConfiguration
SWC is configured via .swcrc (JSON) or swc.config.js in your project root:
{
"jsc": {
"parser": {
"syntax": "typescript",
"tsx": true,
"decorators": true
},
"transform": {
"react": {
"runtime": "automatic"
}
},
"target": "es2020",
"minify": {
"compress": true,
"mangle": true
}
},
"module": {
"type": "commonjs"
},
"sourceMaps": true
}Using SWC in Node.js
SWC is most commonly used as a library rather than a CLI, integrated into build tools:
const { transformSync } = require("@swc/core");
const output = transformSync(
`
const greet = (name: string) => \`Hello, \${name}!\`;
`,
{
filename: "greet.ts",
jsc: {
parser: { syntax: "typescript" },
target: "es2018",
},
},
);
console.log(output.code);Integration with build tools
SWC is embedded in several major tools:
- Next.js — uses SWC as its default compiler since v12, replacing Babel
- Deno — uses SWC internally for TypeScript stripping
- Parcel — uses SWC for JS/TS compilation
- Vite — can use the
vite-plugin-swcplugin - Jest — use
@swc/jestas a drop-in Babel replacement for test transforms
jest integration
npm install --save-dev @swc/jest// jest.config.json
{
"transform": {
"^.+\\.(t|j)sx?$": "@swc/jest"
}
}
This typically cuts Jest startup and transform time dramatically on large TypeScript codebases.
Why SWC over Babel?
Babel is written in JavaScript and runs in Node.js. SWC is compiled Rust running natively. For large codebases, the difference is stark — a full project build that takes 30 seconds with Babel may complete in under 2 seconds with SWC. The API surface is intentionally similar, making migration straightforward for most projects.