Skip to content
文档
配置
压缩

代码压缩

⚠️

本节适用于希望在构建过程的最后一步使用 swc 的用户。 如果您想在使用打包工具时使用 swc 压缩器,请参阅以下文档:

v1.2.67 开始,您可以通过在 .swcrc 文件中启用 minify 来配置 SWC 以压缩您的代码:

.swcrc
{
  // 启用压缩
  "minify": true,
  // 可选,配置压缩选项
  "jsc": {
    "minify": {
      "compress": {
        "unused": true
      },
      "mangle": true
    }
  }
}

配置

关于注释的说明

如果您将 jsc.minify.compress 设置为 true{},SWC 将仅保留许可证注释。 如果您不希望这样,请修改 jsc.minify.format

jsc.minify.compress

类型:boolean | object

类似于 tersercompress 选项 (opens in a new tab)

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

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

.swcrc
{
  "jsc": {
    "minify": {
      "mangle": true // 等同于 {}
    }
  }
}
  • props,默认为 falsetrue 等同于 {}
  • 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 压缩器而无需修改。

  • 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 将包括:

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

这将为所有嵌套依赖项使用 SWC 压缩器而不是 Terser。确保删除您的 lockfile 并重新安装依赖项。

$ rm -rf node_modules yarn.lock
$ yarn