跳到內容
文件
用法
@swc/cli

@swc/cli

用法

執行以下指令下載預先建置的二進位檔

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

然後,你可以轉譯你的檔案

# 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

選項

--filename (-f)

從 stdin 讀取時要使用的檔案名稱。這將用於原始碼對應和錯誤。

npx swc -f input.js

--config-file

要使用的 .swcrc 檔案路徑。

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

--env-name

載入設定檔和外掛時要使用的「env」名稱。預設值為 SWC_ENV 的值,否則為 NODE_ENV,否則為 development

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

--no-swcrc

是否要尋找 .swcrc 檔案。

npx swc input.js --no-swcrc

--ignore

要編譯的 glob 路徑清單。

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

--only

編譯 glob 路徑清單

範例

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

--watch (-w)

若要自動重新編譯變更的檔案,請安裝 chokidar

npm i -D chokidar

然後,新增 -w 旗標

npx swc input.js -w

--quiet (-q)

抑制編譯輸出。

npx swc input.js -q

--source-maps (-s)

值: true|false|inline|both

npx swc input.js -s

--source-map-target

定義來源地圖的 檔案

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

--source-file-name

在傳回的來源地圖上設定 sources[0]

--source-root

所有來源的根目錄。

--out-file (-o)

將所有輸入檔案編譯成單一檔案。

npx swc input.js -o output.js

--out-dir (-d)

將輸入的模組目錄編譯成輸出目錄。

npx swc src -d dist

--copy-files (-D)

編譯目錄時,複製不可編譯的檔案。

npx swc src --copy-files

--include-dotfiles

編譯和複製不可編譯檔案時包含 dotfiles。

npx swc src --include-dotfiles

--config (-C)

覆寫 .swcrc 檔案中的設定檔。

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

--sync

同步呼叫 swc。對於除錯很有用。

npx swc src --sync

--log-watch-compilation

在成功編譯監控的檔案時記錄訊息。

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

--extensions

使用特定副檔名。

--strip-leading-paths

在建立最終輸出路徑時移除開頭目錄(包括所有父層相對路徑)。例如,它會將 src 資料夾下的所有模組編譯到 dist 資料夾,而不會在 dist 中建立 src 資料夾。

npx swc src -d dist --strip-leading-paths