SWC 命令行工具 #

基本命令格式 #

bash
swc [options] <input...>

全局选项 #

查看帮助 #

bash
# 查看帮助信息
swc --help
swc -h

# 查看版本
swc --version
swc -V

常用选项 #

选项 简写 描述
--help -h 显示帮助信息
--version -V 显示版本号
--config -C 覆盖配置项
--config-file 指定配置文件路径
--no-config 不使用配置文件
--filename 指定输入文件名(用于 stdin)
--source-root 指定源码根目录
--source-root-target 指定输出根目录

编译命令 #

编译单个文件 #

bash
# 基本编译
swc input.js

# 指定输出文件
swc input.js -o output.js
swc input.js --out-file output.js

# 从 stdin 读取
echo "const x = 1;" | swc

# 指定 stdin 文件名
echo "const x = 1;" | swc --filename input.js

编译目录 #

bash
# 编译整个目录
swc src -d dist
swc src --out-dir dist

# 编译多个目录
swc src lib -d dist

# 保持目录结构
swc src -d dist --copy-files

编译选项 #

bash
# 指定输出文件
swc input.js -o output.js

# 指定输出目录
swc src -d dist

# 生成 source map
swc input.js -o output.js -s
swc input.js -o output.js --source-maps

# 内联 source map
swc input.js -o output.js -s inline

# 指定 source map 目标路径
swc input.js -o output.js --source-map-target map.js

输出选项 #

输出文件 #

bash
# -o, --out-file
swc src/index.js -o dist/index.js

# 多文件输出到目录
swc src -d dist

输出目录 #

bash
# -d, --out-dir
swc src -d dist

# 指定输出目录结构
swc src -d dist --out-dir dist

Source Map 选项 #

bash
# 生成独立 source map 文件
swc src -d dist -s

# 内联 source map
swc src -d dist -s inline

# 指定 source map 根路径
swc src -d dist -s --source-root /src

监听模式 #

启用监听 #

bash
# 监听文件变化
swc src -d dist -w
swc src -d dist --watch

# 监听并输出详细信息
swc src -d dist -w --verbose

监听选项 #

bash
# 监听单个文件
swc src/index.js -o dist/index.js -w

# 监听目录
swc src -d dist -w

# 监听多个目录
swc src lib -d dist -w

监听配置 #

json
// .swcrc
{
  "watch": {
    "paths": ["src/**/*"],
    "ignore": ["src/**/*.test.js"]
  }
}

配置覆盖 #

使用 -C 选项 #

bash
# 覆盖单个配置
swc src -d dist -C jsc.target=es2015

# 覆盖多个配置
swc src -d dist -C jsc.target=es2015 -C module.type=commonjs

# 覆盖嵌套配置
swc src -d dist -C jsc.parser.syntax=typescript

# 启用压缩
swc src -d dist -C minify=true

# 启用 source map
swc src -d dist -C sourceMaps=true

配置覆盖示例 #

bash
# TypeScript 项目
swc src -d dist \
  -C jsc.parser.syntax=typescript \
  -C jsc.parser.tsx=true \
  -C jsc.target=es2015

# React 项目
swc src -d dist \
  -C jsc.parser.syntax=ecmascript \
  -C jsc.parser.jsx=true \
  -C jsc.transform.react.runtime=automatic

# 启用压缩
swc src -d dist \
  -C minify=true \
  -C jsc.minify.compress=true \
  -C jsc.minify.mangle=true

不使用配置文件 #

bash
# 完全忽略配置文件
swc src -d dist --no-config

# 使用 --no-config 并指定配置
swc src -d dist --no-config \
  -C jsc.target=es2015 \
  -C module.type=commonjs

文件处理 #

复制文件 #

bash
# 复制非 JS 文件
swc src -d dist --copy-files

# 只复制文件(不编译)
swc src -d dist --copy-files --no-swcrc

排除文件 #

bash
# 排除特定文件
swc src -d dist --ignore "**/*.test.js"
swc src -d dist --ignore "**/*.spec.js" --ignore "**/test/**"

# 排除多个模式
swc src -d dist \
  --ignore "**/*.test.js" \
  --ignore "**/*.spec.js" \
  --ignore "**/node_modules/**"

包含文件 #

bash
# 只编译匹配的文件
swc src -d dist --only "**/*.js"
swc src -d dist --only "**/*.{js,jsx,ts,tsx}"

输出格式 #

模块格式 #

bash
# CommonJS
swc src -d dist -C module.type=commonjs

# ES Modules
swc src -d dist -C module.type=es6

# AMD
swc src -d dist -C module.type=amd

# UMD
swc src -d dist -C module.type=umd

目标环境 #

bash
# ES5
swc src -d dist -C jsc.target=es5

# ES2015
swc src -d dist -C jsc.target=es2015

# ES2020
swc src -d dist -C jsc.target=es2020

同步与异步 #

同步编译 #

bash
# 默认同步编译
swc src -d dist

并行编译 #

bash
# 指定并行数量
swc src -d dist --workers 4

# 使用所有 CPU 核心
swc src -d dist --workers max

调试选项 #

详细输出 #

bash
# 显示详细信息
swc src -d dist --verbose

# 显示处理的文件
swc src -d dist --verbose --log-watch-compilation

显示配置 #

bash
# 显示当前配置
swc --show-config

# 显示指定文件的配置
swc src/index.js --show-config

错误处理 #

bash
# 遇到错误继续编译
swc src -d dist --no-error-on-ignored

# 显示详细错误信息
swc src -d dist --verbose

实用示例 #

开发环境 #

bash
# 开发模式:监听 + source map
swc src -d dist -w -s inline

# 开发模式:详细输出
swc src -d dist -w -s inline --verbose

生产环境 #

bash
# 生产模式:压缩 + source map
swc src -d dist -C minify=true -s

# 生产模式:完整配置
swc src -d dist \
  -C jsc.target=es2015 \
  -C minify=true \
  -C jsc.minify.compress=true \
  -C jsc.minify.mangle=true \
  -s

TypeScript 项目 #

bash
# 编译 TypeScript
swc src -d dist \
  -C jsc.parser.syntax=typescript \
  -C jsc.parser.tsx=true \
  -C jsc.target=es2015

# 监听 TypeScript
swc src -d dist -w \
  -C jsc.parser.syntax=typescript \
  -C jsc.target=es2015

React 项目 #

bash
# React JSX 编译
swc src -d dist \
  -C jsc.parser.syntax=ecmascript \
  -C jsc.parser.jsx=true \
  -C jsc.transform.react.runtime=automatic

# React TypeScript
swc src -d dist \
  -C jsc.parser.syntax=typescript \
  -C jsc.parser.tsx=true \
  -C jsc.transform.react.runtime=automatic

库开发 #

bash
# 输出多种格式
swc src -d dist/cjs -C module.type=commonjs
swc src -d dist/esm -C module.type=es6

# 带类型声明
swc src -d dist \
  -C jsc.parser.syntax=typescript \
  -C jsc.keepClassNames=true

与 npm scripts 集成 #

package.json 配置 #

json
{
  "scripts": {
    "build": "swc src -d dist",
    "build:dev": "swc src -d dist -s inline",
    "build:prod": "swc src -d dist -C minify=true -s",
    "dev": "swc src -d dist -w -s inline",
    "watch": "swc src -d dist -w"
  }
}

运行脚本 #

bash
npm run build
npm run build:dev
npm run build:prod
npm run dev
npm run watch

管道操作 #

与其他工具配合 #

bash
# 编译后运行
swc src/index.js | node

# 编译后压缩
swc src/index.js | terser > dist/bundle.min.js

# 编译并检查
swc src/index.js | eslint --stdin

输入输出重定向 #

bash
# 从文件输入
swc < src/index.js > dist/index.js

# 输出到文件
swc src/index.js > dist/index.js

# 追加输出
swc src/utils.js >> dist/bundle.js

命令行参数完整列表 #

输入输出 #

参数 简写 描述
--out-file -o 输出文件路径
--out-dir -d 输出目录路径
--filename stdin 输入的文件名

编译选项 #

参数 简写 描述
--config -C 覆盖配置项
--config-file 配置文件路径
--no-config 不使用配置文件
--source-maps -s 生成 source map
--source-map-target source map 目标文件
--source-root 源码根目录

文件处理 #

参数 描述
--copy-files 复制非编译文件
--ignore 排除文件模式
--only 只包含文件模式
--watch -w

其他选项 #

参数 描述
--verbose 详细输出
--version -V
--help -h
--show-config 显示配置
--workers 并行工作进程数

常见问题 #

问题一:命令找不到 #

bash
swc: command not found

解决方案

bash
# 使用 npx
npx swc src -d dist

# 或全局安装
npm install -g @swc/cli @swc/core

问题二:配置不生效 #

检查项

  1. 确认配置文件格式正确
  2. 检查命令行参数是否覆盖配置
  3. 使用 --show-config 查看实际配置

问题三:监听不工作 #

bash
# 确保安装了 chokidar(某些系统需要)
npm install chokidar

# 使用轮询(在某些系统上)
swc src -d dist -w --use-polling

下一步 #

现在你已经掌握了 SWC 命令行工具的使用方法,接下来学习 配置详解 深入了解所有配置选项!

最后更新:2026-03-28