Skip to content
文档
配置
压缩

压缩

⚠️

本节适用于想要使用 swc 作为构建过程最后一步的人。 如果你想在打包工具中使用 swc 压缩器,请参阅以下文档:

假设

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 以压缩你的代码:

.swcrc
{
  // 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) 类似。

.swcrc
{
  "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.

tersermangle 选项 (opens in a new tab) 类似。

.swcrc
{
  "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.

tersermangle properties 选项 (opens in a new tab) 类似。

.swcrc
{
  "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 将包括:

package.json
{
  "resolutions": { "terser": "npm:@swc/core" }
}

这将使用 SWC 压缩器代替 Terser 用于所有嵌套依赖项。确保删除你的 lockfile 并重新安装你的依赖项。

$ rm -rf node_modules yarn.lock
$ yarn