代码压缩
本节适用于希望在构建过程的最后一步使用 swc 的用户。 如果您想在使用打包工具时使用 swc 压缩器,请参阅以下文档:
- 使用 Terser Webpack 插件的 swcMinify
- SWC Minify Webpack 插件,这是一个独立的工具,如果您只需要压缩功能。
从 v1.2.67
开始,您可以通过在 .swcrc
文件中启用 minify
来配置 SWC 以压缩您的代码:
{
// 启用压缩
"minify": true,
// 可选,配置压缩选项
"jsc": {
"minify": {
"compress": {
"unused": true
},
"mangle": true
}
}
}
配置
关于注释的说明
如果您将 jsc.minify.compress
设置为 true
或 {}
,SWC 将仅保留许可证注释。
如果您不希望这样,请修改 jsc.minify.format
。
jsc.minify.compress
类型:boolean | object
。
类似于 terser
的 compress 选项 (opens in a new tab)。
{
"jsc": {
"minify": {
"compress": true // 等同于 {}
}
}
}
arguments
,默认为false
。arrows
,默认为true
。booleans
,默认为true
。booleans_as_integers
,默认为false
。collapse_vars
,默认为true
。comparisons
,默认为true
。computed_props
,默认为true
。conditionals
,默认为true
。dead_code
,默认为true
。defaults
,默认为true
。directives
,默认为true
。drop_console
,默认为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
类型:boolean | object
。
类似于 terser
的 mangle 选项 (opens in a new tab)。
{
"jsc": {
"minify": {
"mangle": true // 等同于 {}
}
}
}
props
,默认为false
,true
等同于{}
。topLevel
,默认为true
。为了与terser
兼容,别名为toplevel
。keepClassNames
,默认为false
。为了与terser
兼容,别名为keep_classnames
。keepFnNames
,默认为false
。keepPrivateProps
,默认为false
。为了与terser
兼容,别名为keep_private_props
。reserved
,默认为[]
。ie8
,忽略。safari10
,默认为false
。
jsc.minify.mangle.properties
类型:object
。
类似于 terser
的 mangle properties 选项 (opens in a new tab)。
{
"jsc": {
"minify": {
"mangle":{
"properties":{
"reserved": ["foo", "bar"],
"undeclared":false,
"regex":"rust regex"
}
}
}
}
}
-
reserved
:不要使用这些名称作为属性。 -
undeclared
:即使未声明,也要压缩属性。 -
regex
:仅当属性匹配此正则表达式时才压缩。
jsc.minify.format
这些属性大多尚未实现,但它存在是为了支持将 terser 配置传递给 swc 压缩器而无需修改。
asciiOnly
,默认为false
。自v1.2.184
起实现,为了与terser
兼容,别名为ascii_only
。beautify
,默认为false
。目前无操作。braces
,默认为false
。目前无操作。comments
,默认为some
。false
删除所有注释'some'
保留一些注释'all'
保留所有注释
ecma
,默认为 5。目前无操作。indentLevel
,目前无操作,为了与terser
兼容,别名为indent_level
。indentStart
,目前无操作,为了与terser
兼容,别名为indent_start
。inlineScript
,为了与terser
兼容,别名为inline_script
,需要@swc/[email protected]
或更高版本。keepNumbers
,目前无操作,为了与terser
兼容,别名为keep_numbers
。keepQuotedProps
,目前无操作,为了与terser
兼容,别名为keep_quoted_props
。maxLineLen
,目前无操作,为了与terser
兼容,别名为max_line_len
。preamble
,自v1.3.66
起支持。quoteKeys
,目前无操作,为了与terser
兼容,别名为quote_keys
。quoteStyle
,目前无操作,为了与terser
兼容,别名为quote_style
。preserveAnnotations
,目前无操作,为了与terser
兼容,别名为preserve_annotations
。safari10
,目前无操作。semicolons
,目前无操作。shebang
,目前无操作。webkit
,目前无操作。wrapIife
,目前无操作,为了与terser
兼容,别名为wrap_iife
。wrapFuncArgs
,目前无操作,为了与terser
兼容,别名为wrap_func_args
。
@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 resolutions (opens in a new tab) 减少构建时间并覆盖 Terser,而无需库更新其依赖项。示例 package.json
将包括:
{
"resolutions": { "terser": "npm:@swc/core" }
}
这将为所有嵌套依赖项使用 SWC 压缩器而不是 Terser。确保删除您的 lockfile 并重新安装依赖项。
$ rm -rf node_modules yarn.lock
$ yarn