縮小
從 v1.2.67
開始,您可以透過在 .swcrc
檔案中啟用 minify
來設定 SWC 縮小您的程式碼
.swcrc
{
// Enable minification
"minify": true,
// Optional, configure minification options
"jsc": {
"minify": {
"compress": {
"unused": true
},
"mangle": true
}
}
}
組態
關於註解的注意事項
如果您將 jsc.minify.compress
設定為 true
或 {}
,SWC 將只保留授權註解。如果您不想要這樣,請修改 jsc.minify.format
。
jsc.minify.compress
類型:布林值 | 物件
。
類似於 terser(在新分頁中開啟) 的 compress 選項
。
.swcrc
{
"jsc": {
"minify": {
"compress": true // equivalent to {}
}
}
}
arguments
,預設為 false
。
arrows
,預設為 true
。
booleans
,預設為 true
。
布林值作為整數
,預設為 false
。
摺疊變數
,預設為 true
。
比較
,預設為 true
。
計算屬性
,預設為 true
。
條件
,預設為 true
。
無用程式碼
,預設為 true
。
預設值
,預設為 true
。
指令
,預設為 true
。
移除主控台
,預設為 false
。
drop_debugger
,預設為 true
。
ecma
,預設為 5
。
evaluate
,預設為 true
。
global_defs
,預設為 {}
。
hoist_funs
,預設為 false
。
hoist_props
,預設為 true
。
hoist_vars
,預設為 false
。
ie8
,忽略。
if_return
,預設為 true
。
inline
,預設為 true
。
join_vars
,預設為 true
。
keep_classnames
,預設為 false
。
keep_fargs
,預設為 false
。
keep_infinity
,預設為 false
。
loops
,預設為 true
。
negate_iife
,預設為 true
。
passes
,預設為 0
,表示沒有限制。
properties
,預設為 true
。
pure_getters
,預設為 ``。
pure_funcs
,預設為 []
。類型為字串陣列。
reduce_funcs
,預設為 false
。
reduce_vars
,預設為 true
。
sequences
,預設為 true
。
side_effects
,預設為 true
。
switches
,預設為 true
。
top_retain
,預設為 ``。
toplevel
,預設為 true
。
typeofs
,預設為 true
。
unsafe
,預設為 false
。
unsafe_arrows
,預設為 false
。
unsafe_comps
,預設為 false
。
unsafe_Function
,預設為 false
。
unsafe_math
,預設為 false
。
unsafe_symbols
,預設為 false
。
unsafe_methods
,預設為 false
。
unsafe_proto
,預設為 false
。
unsafe_regexp
,預設為 false
。
unsafe_undefined
,預設為 false
。
unused
,預設為 true
。
module
,忽略。目前,所有檔案都視為模組。
jsc.minify.mangle
類型:布林值 | 物件
。
類似於 .swcrc
{
"jsc": {
"minify": {
"mangle": true // equivalent to {}
}
}
}
props
,預設為 false
,且 true
等同於 {}
。
topLevel
,預設為 true
。別名為 toplevel
,以與 terser
相容。
keepClassNames
,預設為 false
。別名為 keep_classnames
,以與 terser
相容。
keepFnNames
,預設為 false
。
keepPrivateProps
,預設為 false
。別名為 keep_private_props
,以與 terser
相容。
reserved
,預設為 []
ie8
,忽略。
safari10
,預設為 false
。
jsc.minify.mangle.properties
類型:object
。
類似於 terser
的 mangle properties 選項(在新分頁中開啟)。
.swcrc
{
"jsc": {
"minify": {
"mangle":{
"properties":{
"reserved": ["foo", "bar"],
"undeclared":false,
"regex":"rust regex"
}
}
}
}
}
-
保留
:請勿將這些名稱用作屬性。
-
未宣告
:即使未宣告,也要混淆屬性。
-
正規表示式
:僅當屬性符合此正規表示式時才混淆屬性
jsc.minify.format
這些屬性大多尚未實作,但其存在是為了支援將 terser 設定傳遞給 swc minify,而無需修改。
asciiOnly
,預設為 false
。實作為 v1.2.184
,並別名為 ascii_only
,以與 terser
相容。
美化
,預設為 false
。目前為空操作。
大括號
,預設為 false
。目前為空操作。
註解
,預設為 部分
。
false
移除所有註解
'some'
保留部分註解
'all'
保留所有註解
ecma
,預設為 5。目前為無效操作。
縮排層級
,目前為無效操作,並別名為 縮排_層級
以與 terser
相容。
縮排開始
,目前為無效操作,並別名為 縮排_開始
以與 terser
相容。
內嵌腳本
,目前為無效操作,並別名為 內嵌_腳本
以與 terser
相容。
keepNumbers
,目前為 noop,並別名為 keep_numbers
,以與 terser
相容。
keepQuotedProps
,目前為 noop,並別名為 keep_quoted_props
,以與 terser
相容。
maxLineLen
,目前為 noop,並別名為 max_line_len
,以與 terser
相容。
preamble
,自 v1.3.66
起支援。
quoteKeys
,目前為 noop,並別名為 quote_keys
以與 terser
相容。
quoteStyle
,目前為 noop,並別名為 quote_style
以與 terser
相容。
preserveAnnotations
,目前為 noop,並別名為 preserve_annotations
以與 terser
相容。
safari10
,目前為 noop。
semicolons
,目前為 noop。
shebang
,目前為 noop。
webkit
,目前為空操作。
wrapIife
,目前為空操作,別名為 wrap_iife
,以與 terser
相容。
wrapFuncArgs
,目前為空操作,別名為 wrap_func_args
,以與 terser
相容。
@swc/core 使用方式
swc.minify(code, options)
此 API 為非同步,所有解析、縮小和程式碼產生都會在背景執行緒中進行。 options
參數與 jsc.minify
物件相同。例如
import swc from "@swc/core";
const { code, map } = await swc.minify(
"import foo from '@src/app'; console.log(foo)",
{
compress: false,
mangle: true,
}
);
expect(code).toMatchInlineSnapshot(`"import a from'@src/app';console.log(a);"`);
傳回 Promise<{ code: string, map: string }>
。
swc.minifySync(code, options)
此 API 存在於 @swc/core
、@swc/wasm
、@swc/wasm-web
。
import swc from "@swc/core";
const { code, map } = swc.minifySync(
"import foo from '@src/app'; console.log(foo)",
{
compress: false,
mangle: true,
module: true
}
);
expect(code).toMatchInlineSnapshot(`"import a from'@src/app';console.log(a);"`);
傳回 { code: string, map: string }
。
WebAssembly 的 API
取代 Terser
透過 Yarn 解析(在新分頁中開啟),您可以在不需要套件更新其相依項的情況下,減少建置時間並覆寫 Terser。範例 package.json
會包含
package.json
{
"resolutions": { "terser": "npm:@swc/core" }
}
這將針對所有巢狀相依項使用 SWC 壓縮器,而非 Terser。請務必移除鎖定檔並重新安裝相依項。
$ rm -rf node_modules yarn.lock
$ yarn
上次更新於 2024 年 4 月 15 日