Skip to content
开发文档
Usage
@swc/cli

@swc/cli

用法

¥Usage

运行以下命令来下载预构建的二进制文件:

¥Run the following to download pre-built binaries:

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

然后,你可以转译你的文件:

¥Then, you can transpile your files:

# Transpile one file and emit to stdout
npx swc ./file.js
 
# Transpile one file and emit to `output.js`
npx swc ./file.js -o output.js
 
# Transpile and write to /output dir
npx swc ./my-dir -d output

选项

¥Options

--filename (-f)

从标准输入读取时使用的文件名。这将用于源映射和错误。

¥Filename to use when reading from stdin. This will be used in source maps and errors.

npx swc -f input.js

--config-file

要使用的 .swcrc 文件的路径。

¥Path to a .swcrc file to use.

npx swc input.js --config-file .swcrc

--env-name

加载配置和插件时使用的 'env' 的名称。默认值为 SWC_ENVNODE_ENVdevelopment

¥The name of the 'env' to use when loading configs and plugins. Defaults to the value of SWC_ENV, or else NODE_ENV, or else development.

npx swc input.js --env-name='test'

--no-swcrc

是否查找 .swcrc 文件。

¥Whether or not to look up .swcrc files.

npx swc input.js --no-swcrc

--ignore

不编译的全局路径列表。

¥List of glob paths to not compile.

npx swc src --ignore **/*.test.js

--only

仅编译的 glob 路径列表

¥List of glob paths to only compile

示例:

¥Example:

npx swc src --only **/*.js

--watch (-w)

要在更改时自动重新编译文件,请安装 chokidar

¥To automatically recompile files on changes, install chokidar:

npm i -D chokidar

然后,添加 -w 标志:

¥Then, add the -w flag:

npx swc input.js -w

--quiet (-q)

抑制编译输出。

¥Suppress compilation output.

npx swc input.js -q

--source-maps (-s)

值:true|false|inline|both

¥Values: true|false|inline|both

npx swc input.js -s

--source-map-target

定义源映射的 file

¥Define the file for the source map.

npx swc input.js -s --source-map-target input.map.js

--source-file-name

在返回的源映射上设置 sources[0]

¥Set sources[0] on returned source map

--source-root

所有来源都是相对的根源。

¥The root from which all sources are relative.

--out-file (-o)

将所有输入文件编译成一个文件。

¥Compile all input files into a single file.

npx swc input.js -o output.js

--out-dir (-d)

将模块的输入目录编译到输出目录中。

¥Compile an input directory of modules into an output directory.

npx swc src -d dist

--copy-files (-D)

编译目录时,复制不可编译的文件。

¥When compiling a directory, copy over non-compilable files.

npx swc src --copy-files

--include-dotfiles

编译和复制不可编译文件时包括点文件。

¥Include dotfiles when compiling and copying non-compilable files.

npx swc src --include-dotfiles

--config (-C)

覆盖 .swcrc 文件中的配置。

¥Override a config from .swcrc file.

npx swc src -C module.type=amd -C module.moduleId=hello

--sync

同步调用 swc。对于调试很有用。

¥Invoke swc synchronously. Useful for debugging.

npx swc src --sync

--log-watch-compilation

成功编译监视的文件时记录一条消息。

¥Log a message when a watched file is successfully compiled.

npx swc input.js --log-watch-compilation

--extensions

使用特定的扩展。

¥Use specific extensions.

--strip-leading-paths

构建最终输出路径时删除前导目录(包括所有父级相对路径)。例如,它将 src 文件夹下的所有模块编译到 dist 文件夹,而不在 dist 内创建 src 文件夹。

¥Remove the leading directory (including all parent relative paths) when building the final output path. As an example it compiles all modules under src folder to dist folder, without create the src folder inside of dist.

npx swc src -d dist --strip-leading-paths
Last updated on November 24, 2023