Skip to content

参考

基于 @swc/core 1.15.x.swcrc 字段、CLI、API 与默认值速查。

.swcrc 字段速查

顶层

字段默认说明
$schema"https://swc.rs/schema.json",获得编辑器补全
jsc编译器核心(见下)
module模块输出(见下)
env按目标环境降级(与 jsc.target 互斥
minifyfalse顶层布尔,开总压缩
sourceMapsfalsetrue / "inline"
isModule强制按模块/脚本解析
exclude / test按文件名正则筛选

jsc

字段默认说明
jsc.parser.syntax"ecmascript""ecmascript" / "typescript"
jsc.parser.jsxfalse(ecmascript)解析 JSX
jsc.parser.tsxfalse(typescript)解析 TSX
jsc.parser.decoratorsfalse解析装饰器语法
jsc.parser.dynamicImportfalse解析 import()
jsc.targetes5语法降级目标 es3esnext
jsc.loosefalse宽松转换(Babel loose 假设)
jsc.externalHelpersfalse助手外置到 @swc/helpers
jsc.keepClassNamesfalse保留类名(需 target ≥ es2016)
jsc.transform.react.runtime"classic""automatic"(React 17+)/ "classic"
jsc.transform.react.importSource"react"automatic 运行时来源
jsc.transform.react.pragma"React.createElement"classic 工厂函数
jsc.transform.react.development注入 __self / __source 调试信息
jsc.transform.react.refreshfalsereact-refresh(实验)
jsc.transform.legacyDecorator旧版装饰器(TS experimentalDecorators)
jsc.transform.decoratorMetadata对应 TS emitDecoratorMetadata
jsc.transform.useDefineForClassFields类字段用 define 还是赋值语义
jsc.transform.optimizerglobals / simplify 等优化
jsc.baseUrl路径别名基准(须绝对路径
jsc.paths路径映射(依赖 baseUrl,同 tsconfig)
jsc.experimental.pluginsWasm 插件,[name, configJSON] 元组
jsc.minify.compress / mangle / formatTerser 兼容压缩选项

module

字段默认说明
module.typees6es6 / commonjs / amd / umd / systemjs
module.strictfalseCJS 下模拟 __esModule
module.strictModetrue输出 "use strict"
module.lazyfalse惰性加载依赖
module.importInterop"swc""swc" / "node" / "none"
module.noInteropfalse关闭互操作 helper

env(与 jsc.target 互斥)

字段说明
env.targetsbrowserslist 查询或 { chrome: "100" }
env.modepolyfill 注入:"usage" / "entry"(不写不注入)
env.coreJscore-js 版本(如 "3"
env.shippedProposals包含已落地提案
env.bugfixes针对引擎 bug 的修正转换

CLI 速查

bash
npx swc src -d dist        # 编译目录到 dist/
npx swc src -d dist -w     # 监听增量
npx swc file.ts -o out.js  # 单文件输出
npx swc src -d dist --source-maps        # 产出 source map
npx swc src -d dist --config-file .swcrc # 指定配置文件
npx swc src -d dist -C jsc.target=es2022 # 命令行覆盖配置项(-C)

API 速查(@swc/core)

API说明
transform(code, opts) / transformSync转译源码字符串(最常用)
transformFile(path, opts) / transformFileSync按路径转译文件
minify(code, opts) / minifySync压缩(Terser 兼容)
parse(code, opts) / parseSync / parseFile*解析为 AST(@deprecated 倾向 Wasm 插件)
print(ast, opts) / printSyncAST 打印回代码(须与 parse 同一 Compiler 实例
bundle(opts)打包(spack,实验、成熟度不足)
new Compiler()复用实例做 parse → print

旧 JS 端 plugins() 与裸 parse/print 组合已 @deprecated,扩展走 Wasm 插件。

与各工具的分工速记

任务谁来做
类型检查只有 tsctsc --noEmit)——SWC 不做
快速 emit .js(剥类型 + 降级)SWC / esbuild / Babel
压缩 minifySWC(Terser 兼容)/ Terser / esbuild
打包Webpack(swc-loader) / Rspack / Rollup / Vite(SWC 当转译内核)
polyfill 注入SWC env(coreJs) 或打包器 + core-js
扩展编译行为SWC Wasm 插件swc_core 须匹配宿主 ABI)

谁在用 SWC

Next.js 内置编译器 · Parcel · Deno · @swc/jest · NestJS(可选)· Rspack / Rsbuild 的 swc-loader

版本现状(2026-06)

dist-tag版本
@swc/corelatest1.15.x(长期 1.x)
@swc/clilatest0.8.x
@swc/helperslatestexternalHelpers 的运行时
swc-loader / @swc/jestlatestWebpack / Jest 集成

Wasm 插件版本请用 plugins.swc.rs 按宿主 @swc/core 选择,无向后兼容保证