Skip to main content

swc

pnpm i -D @swc/cli @swc/core

pnpm swc ./src/main.ts -o ./dist/main.js

# swc + esbuild/tsx - 支持 emitDecoratorMetadata, 利用 esbuild 的 bundle 能力
pnpm swc ./src/ -d ./dist/out
pnpm tsx ./dist/out/main.ts

swcrc

{
"$schema": "https://json.schemastore.org/swcrc",
"jsc": {
"parser": {
"syntax": "ecmascript",
"jsx": false,
"dynamicImport": false,
"privateMethod": false,
"functionBind": false,
"exportDefaultFrom": false,
"exportNamespaceFrom": false,
"decorators": false,
"decoratorsBeforeExport": false,
"topLevelAwait": false,
"importMeta": false
},
"transform": null,
"target": "es5",
"loose": false,
"externalHelpers": false,
// Requires v1.2.50 or upper and requires target to be es2016 or upper.
"keepClassNames": false
},
"minify": false,
// 依赖 browserslist
"env": {
"targets": {
"chrome": "79"
},
"mode": "entry",
"coreJs": "3.22"
}
}

Plugins

FAQ

keepImportAssertions

file extension

swc vs esbuild

  • esbuild
    • production ready
    • 不能 bundle systemjs,amd
    • 不支持 emitDecoratorMetadata
  • swc
    • compiler 功能完备
    • 能 bundle 更多格式

strip-leading-paths

# dist/src -> dist
npx swc ./src -d dist --strip-leading-paths