Skip to content
开发文档
Configuration
压缩

压缩

¥Minification

⚠️

本节适用于那些想要使用 SWC 作为构建过程最后一步的人。如果你想将 swc minifier 与打包器一起使用,请参阅其 swcMinify 文档。

¥This section is for people who want to use swc as a last step of the build process. If you want to use swc minifier with a bundler, please see its documentation for swcMinify.


Webpack doc

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

¥Starting with v1.2.67, you can configure SWC to minify your code by enabling minify in your .swcrc file:

.swcrc
{
  // Enable minification
  "minify": true,
  // Optional, configure minification options
  "jsc": {
    "minify": {
      "compress": {
        "unused": true
      },
      "mangle": true
    }
  }
}

配置

¥Configuration

关于注释的注意事项

¥Note about comments

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

¥If you set jsc.minify.compress to true or {}, SWC will preserve only license comments. If you don't want this, modify jsc.minify.format.

jsc.minify.compress

类型:boolean | object

¥Type: boolean | object.

terser压缩选项 (opens in a new tab) 类似。

¥Similar to the compress option (opens in a new tab) of terser.

.swcrc
{
  "jsc": {
    "minify": {
      "compress": true // equivalent to {}
    }
  }
}
  • arguments,默认为 false

    ¥arguments, defaults to false.

  • arrows,默认为 true

    ¥arrows, defaults to true.

  • booleans,默认为 true

    ¥booleans, defaults to true.

  • booleans_as_integers,默认为 false

    ¥booleans_as_integers, defaults to false.

  • collapse_vars,默认为 true

    ¥collapse_vars, defaults to true.

  • comparisons,默认为 true

    ¥comparisons, defaults to true.

  • computed_props,默认为 true

    ¥computed_props, defaults to true.

  • conditionals,默认为 true

    ¥conditionals, defaults to true.

  • dead_code,默认为 true

    ¥dead_code, defaults to true.

  • defaults,默认为 true

    ¥defaults, defaults to true.

  • directives,默认为 true

    ¥directives, defaults to true.

  • drop_console,默认为 false

    ¥drop_console, defaults to false.

  • drop_debugger,默认为 true

    ¥drop_debugger, defaults to true.

  • ecma,默认为 5

    ¥ecma, defaults to 5.

  • evaluate,默认为 true

    ¥evaluate, defaults to true.

  • global_defs,默认为 {}

    ¥global_defs, defaults to {}.

  • hoist_funs,默认为 false

    ¥hoist_funs, defaults to false.

  • hoist_props,默认为 true

    ¥hoist_props, defaults to true.

  • hoist_vars,默认为 false

    ¥hoist_vars, defaults to false.

  • ie8,忽略。

    ¥ie8, Ignored.

  • if_return,默认为 true

    ¥if_return, defaults to true.

  • inline,默认为 true

    ¥inline, defaults to true.

  • join_vars,默认为 true

    ¥join_vars, defaults to true.

  • keep_classnames,默认为 false

    ¥keep_classnames, defaults to false.

  • keep_fargs,默认为 false

    ¥keep_fargs, defaults to false.

  • keep_infinity,默认为 false

    ¥keep_infinity, defaults to false.

  • loops,默认为 true

    ¥loops, defaults to true.

  • negate_iife,默认为 true

    ¥negate_iife, defaults to true.

  • passes,默认为 0,表示不限制。

    ¥passes, defaults to 0, which means no limit.

  • properties,默认为 true

    ¥properties, defaults to true.

  • pure_getters,默认为 ``.

    ¥pure_getters, defaults to ``.

  • pure_funcs,默认为 []。类型是字符串数组。

    ¥pure_funcs, defaults to []. Type is an array of string.

  • reduce_funcs,默认为 false

    ¥reduce_funcs, defaults to false.

  • reduce_vars,默认为 true

    ¥reduce_vars, defaults to true.

  • sequences,默认为 true

    ¥sequences, defaults to true.

  • side_effects,默认为 true

    ¥side_effects, defaults to true.

  • switches,默认为 true

    ¥switches, defaults to true.

  • top_retain,默认为 ``.

    ¥top_retain, defaults to ``.

  • toplevel,默认为 true

    ¥toplevel, defaults to true.

  • typeofs,默认为 true

    ¥typeofs, defaults to true.

  • unsafe,默认为 false

    ¥unsafe, defaults to false.

  • unsafe_arrows,默认为 false

    ¥unsafe_arrows, defaults to false.

  • unsafe_comps,默认为 false

    ¥unsafe_comps, defaults to false.

  • unsafe_Function,默认为 false

    ¥unsafe_Function, defaults to false.

  • unsafe_math,默认为 false

    ¥unsafe_math, defaults to false.

  • unsafe_symbols,默认为 false

    ¥unsafe_symbols, defaults to false.

  • unsafe_methods,默认为 false

    ¥unsafe_methods, defaults to false.

  • unsafe_proto,默认为 false

    ¥unsafe_proto, defaults to false.

  • unsafe_regexp,默认为 false

    ¥unsafe_regexp, defaults to false.

  • unsafe_undefined,默认为 false

    ¥unsafe_undefined, defaults to false.

  • unused,默认为 true

    ¥unused, defaults to true.

  • module,忽略。目前,所有文件都被视为模块。

    ¥module, Ignored. Currently, all files are treated as module.

jsc.minify.mangle

类型:boolean | object

¥Type: boolean | object.

terser损坏选项 (opens in a new tab) 类似。

¥Similar to the mangle option (opens in a new tab) of terser.

.swcrc
{
  "jsc": {
    "minify": {
      "mangle": true // equivalent to {}
    }
  }
}
  • props,默认为 falsetrue{} 相同。

    ¥props, Defaults to false, and true is identical to {}.

  • topLevel,默认为 true。别名为 toplevel 以与 terser 兼容。

    ¥topLevel, Defaults to true. Aliased as toplevel for compatibility with terser.

  • keepClassNames,默认为 false。别名为 keep_classnames 以与 terser 兼容。

    ¥keepClassNames, Defaults to false. Aliased as keep_classnames for compatibility with terser.

  • keepFnNames,默认为 false

    ¥keepFnNames, Defaults to false.

  • keepPrivateProps,默认为 false。别名为 keep_private_props 以与 terser 兼容。

    ¥keepPrivateProps, Defaults to false. Aliased as keep_private_props for compatibility with terser.

  • reserved,默认为 []

    ¥reserved, Defaults to []

  • ie8,忽略。

    ¥ie8, Ignored.

  • safari10,默认为 false

    ¥safari10, Defaults to false.

jsc.minify.mangle.properties

类型:object

¥Type: object.

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

¥Similar to the mangle properties option (opens in a new tab) of terser.

.swcrc
{
  "jsc": {
    "minify": {
      "mangle":{
        "properties":{
          "reserved": ["foo", "bar"],
          "undeclared":false,
          "regex":"rust regex"
        }
      }
    }
  }
}
  • reserved:不要将这些名称用作属性。

    ¥reserved: Don't use these names as properties.

  • undeclared:即使未声明,也会损坏属性。

    ¥undeclared: Mangle properties even if it's not declared.

  • regex:仅当与此正则表达式匹配时才处理属性

    ¥regex: Mangle properties only if it matches this regex

jsc.minify.format

这些属性大多尚未实现,但它的存在是为了支持将 terser 配置传递到 swc minify 而不进行修改。

¥These properties are mostly not implemented yet, but it exists to support passing terser config to swc minify without modification.

  • asciiOnly,默认为 false。实现为 v1.2.184 并别名为 ascii_only 以与 terser 兼容。

    ¥asciiOnly, Defaults to false. Implemented as v1.2.184 and aliased as ascii_only for compatibility with terser.

  • beautify,默认为 false。目前没有。

    ¥beautify, Defaults to false. Currently noop.

  • braces,默认为 false。目前没有。

    ¥braces, Defaults to false. Currently noop.

  • comments,默认为 some

    ¥comments, Defaults to some.

    • false 删除所有注释

      ¥false removes all comments

    • 'some' 保留一些注释

      ¥'some' preserves some comments

    • 'all' 保留所有注释

      ¥'all' preserves all comments

  • ecma,默认为 5。目前没有。

    ¥ecma, Defaults to 5. Currently noop.

  • indentLevel,目前 noop 和别名为 indent_level 以兼容 terser

    ¥indentLevel, Currently noop and aliases as indent_level for compatibility with terser.

  • indentStart,目前 noop 和别名为 indent_start 以兼容 terser

    ¥indentStart, Currently noop and aliases as indent_start for compatibility with terser.

  • inlineScript,目前 noop 和别名为 inline_script 以兼容 terser

    ¥inlineScript, Currently noop and aliases as inline_script for compatibility with terser.

  • keepNumbers,目前 noop 和别名为 keep_numbers 以兼容 terser

    ¥keepNumbers, Currently noop and aliases as keep_numbers for compatibility with terser.

  • keepQuotedProps,目前 noop 和别名为 keep_quoted_props 以兼容 terser

    ¥keepQuotedProps, Currently noop and aliases as keep_quoted_props for compatibility with terser.

  • maxLineLen,当前为 noop,别名为 max_line_len 以兼容 terser

    ¥maxLineLen, Currently noop, and aliases as max_line_len for compatibility with terser.

  • preamble,从 v1.3.66 开始支持。

    ¥preamble, Supported since v1.3.66.

  • quoteKeys,目前 noop 和别名为 quote_keys 以兼容 terser

    ¥quoteKeys, Currently noop and aliases as quote_keys for compatibility with terser.

  • quoteStyle,目前 noop 和别名为 quote_style 以兼容 terser

    ¥quoteStyle, Currently noop and aliases as quote_style for compatibility with terser.

  • preserveAnnotations,目前 noop 和别名为 preserve_annotations 以兼容 terser

    ¥preserveAnnotations, Currently noop and aliases as preserve_annotations for compatibility with terser.

  • safari10,目前无操作。

    ¥safari10, Currently noop.

  • semicolons,目前无操作。

    ¥semicolons, Currently noop.

  • shebang,目前无操作。

    ¥shebang, Currently noop.

  • webkit,目前无操作。

    ¥webkit, Currently noop.

  • wrapIife,目前 noop 和别名为 wrap_iife 以兼容 terser

    ¥wrapIife, Currently noop and aliases as wrap_iife for compatibility with terser.

  • wrapFuncArgs,目前 noop 和别名为 wrap_func_args 以兼容 terser

    ¥wrapFuncArgs, Currently noop and aliases as wrap_func_args for compatibility with terser.

@swc/core Usage

swc.minify(code, options)

该 API 是异步的,所有解析、缩小和代码生成都将在后台线程中完成。options 参数与 jsc.minify 对象相同。例如:

¥This API is asynchronous and all of parsing, minification, and code generation will be done in background thread. The options argument is same as jsc.minify object. For example:

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 }>

¥Returns Promise<{ code: string, map: string }>.

swc.minifySync(code, options)

该 API 存在于 @swc/core@swc/wasm@swc/wasm-web 上。

¥This API exists on @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,
  }
);
 
expect(code).toMatchInlineSnapshot(`"import a from'@src/app';console.log(a);"`);

返回 { code: string, map: string }

¥Returns { code: string, map: string }.

WebAssembly 的 API

¥APIs for WebAssembly

替换 Terser

¥Replacing Terser

你可以减少构建时间并覆盖 Terser,而无需库通过 Yarn 解析 (opens in a new tab) 更新其依赖。示例 package.json 将包括:

¥You can reduce build time and override Terser without needing a library to update their dependencies through yarn resolutions (opens in a new tab). Example package.json would include:

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

对于所有嵌套依赖,这将使用 SWC 压缩器而不是 Terser。确保删除锁定文件并重新安装依赖。

¥This will use the SWC minifier instead of Terser for all nested dependencies. Ensure you remove your lockfile and re-install your dependencies.

$ rm -rf node_modules yarn.lock
$ yarn
Last updated on November 24, 2023