| name | swc-compiler-transform |
| description | SWC (Speedy Web Compiler) Rust-based JS/TS transforms. Programmatic transpilation, custom plugins via WASM, minification, module format conversion, and performance-sensitive parse pipelines. Sources: swc-project/swc (Apache-2.0). |
/swc-compiler-transform
When to Use
- Parse/transform large TypeScript codebases where Babel is too slow (10–70× faster)
- CI pipelines: batch transpile thousands of files before analysis
- Module conversion: CJS → ESM, TypeScript → JS, JSX → createElement
- Minification without Terser in build-critical pipelines
Do NOT use for
- Custom AST visitor plugins in pure JS (use [[babel-ast-transform]])
- When SWC's WASM plugin SDK is overkill — Babel plugins are simpler to write
Basic transform (TS → JS)
import { transformSync } from '@swc/core'
const result = transformSync(`
const greet = (name: string): string => \`Hello, \${name}!\`
export default greet
`, {
filename: 'greet.ts',
sourceMaps: true,
jsc: {
parser: { syntax: 'typescript', tsx: false },
target: 'es2019',
transform: {
legacyDecorator: true,
},
},
module: { type: 'es6' },
})
console.log(result.code)
console.log(result.map)
Async transform (preferred for large files)
import { transform } from '@swc/core'
import { readFile, writeFile } from 'fs/promises'
import { globby } from 'globby'
async function batchTransform(pattern: string, outDir: string) {
const files = await globby(pattern)
await Promise.all(files.map(async (file) => {
const source = await readFile(file, 'utf8')
const { code, map } = await transform(source, {
filename: file,
sourceMaps: true,
jsc: {
parser: { syntax: 'typescript', tsx: file.endsWith('.tsx') },
target: 'es2020',
},
module: { type: 'commonjs' },
})
const outFile = file.replace('src/', outDir + '/').replace(/\.tsx?$/, '.js')
await (outFile, code)
(map) (outFile + , map)
}))
}
Minify (production build)
import { minify } from '@swc/core'
const { code } = await minify(sourceCode, {
compress: {
unused: true,
dead_code: true,
drop_console: true,
passes: 2,
},
mangle: { toplevel: false },
format: { comments: false },
sourceMap: false,
})
Parse only (AST extraction)
import { parse } from '@swc/core'
const ast = await parse(`
export function add(a: number, b: number) { return a + b }
`, {
syntax: 'typescript',
comments: false,
script: false,
})
const firstExport = ast.body[0]
Anti-Fake-Pass Checklist
❌ Using transformSync in async Node server → blocks event loop on large files
❌ jsc.target: 'es5' without specifying externalHelpers: true → bloated output (helpers inlined per file)
❌ SWC AST !== ESTree — don't pass SWC ast to ESLint/Babel APIs directly
❌ sourceMaps: true without writing .map file → source maps referenced but missing
❌ tsx: false for .tsx files → JSX syntax errors
❌ SWC WASM plugins require @swc/wasm-typescript — not bundled by default