压缩
本节适用于想要使用 swc 作为构建过程最后一步的人。 如果你想在打包工具中使用 swc 压缩器,请参阅以下文档:
- 在 Terser Webpack Plugin 中使用 swcMinify
- SWC Minify Webpack Plugin,这是一个 独立工具,如果你只需要代码压缩功能。
假设
SWC Minifier 对代码进行小量假设以提高压缩效率。这与其他压缩器类似。
- 访问顶级标识符不会产生副作用。
如果你在 globalThis 上使用 getter 声明一个变量,SWC Minifier 可能会破坏它。
- TDZ 违反不存在。
TDZ 违反将被忽略且不保留。
- 算术表达式可能没有副作用。
这意味着你不能依赖 JS 引擎在添加 bigint 和数字时抛出异常。
const a = 1n;
const b = 1;
const c = a + b;执行这段代码时,JS 引擎会抛出异常。但 SWC Minifier 会忽略它。
从 v1.2.67 开始,你可以在 .swcrc 文件中启用 minify 以压缩你的代码:
{
// Enable minification
"minify": true,
// Optional, configure minification options
"jsc": {
"minify": {
"compress": {
"unused": true
},
"mangle": true
}
}
}配置
关于注释的注意事项
如果将 jsc.minify.compress 设置为 true 或 {},从 v1.11.11 开始,SWC 将删除所有注释。
以前的版本只会保留许可证注释。
如果你不想这样,修改 jsc.minify.format。
jsc.minify.compress
类型:boolean | object.
与 terser 的 压缩选项 (opens in a new tab) 类似。
{
"jsc": {
"minify": {
"compress": true // equivalent to {}
}
}
}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 // equivalent to {}
}
}
}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 minify 而不进行修改。
asciiOnly, 默认值为false。在v1.2.184中实现,并作为ascii_only为兼容terser别名。beautify, 默认值为false。当前无操作。braces, 默认值为false。当前无操作。comments, 默认值为some。false删除所有注释'some'保留一些注释'all'保留所有注释
ecma, 默认值为 5。当前无操作。indentLevel, 当前无操作,并作为indent_level为兼容terser别名。indentStart, 当前无操作,并作为indent_start为兼容terser别名。inlineScript, 别名inline_script为兼容terser并需要@swc/core@1.9.2或更高版本。keepNumbers, 当前无操作,并作为keep_numbers为兼容terser别名。keepQuotedProps, 当前无操作,并作为keep_quoted_props为兼容terser别名。maxLineLen, 当前无操作,并作为max_line_len为兼容terser别名。preamble, 自v1.3.66起支持。quoteKeys, 当前无操作,并作为quote_keys为兼容terser别名。quoteStyle, 当前无操作,并作为quote_style为兼容terser别名。preserveAnnotations, 当前无操作,并作为preserve_annotations为兼容terser别名。safari10, 当前无操作。semicolons, 当前无操作。shebang, 当前无操作。webkit, 当前无操作。wrapIife, 当前无操作,并作为wrap_iife为兼容terser别名。wrapFuncArgs, 当前无操作,并作为wrap_func_args为兼容terser别名。
@swc/core Usage
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);"`);Returns { code: string, map: string }.
WebAssembly 的 APIs
替换 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