Skip to content
文档
配置
压缩

Minification

⚠️

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

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 minifcation options
  "jsc": {
    "minify": {
      "compress": {
        "unused": true
      },
      "mangle": true
    }
  }
}

Configuration

Note about comments

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

Type: boolean | object.

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

.swcrc
{
  "jsc": {
    "minify": {
      "compress": true // equivalent to {}
    }
  }
}
  • arguments, defaults to false.
  • arrows, defaults to true.
  • booleans, defaults to true.
  • booleans_as_integers, defaults to false.
  • collapse_vars, defaults to true.
  • comparisons, defaults to true.
  • computed_props, defaults to true.
  • conditionals, defaults to true.
  • dead_code, defaults to false.
  • defaults, defaults to true.
  • directives, defaults to true.
  • drop_console, defaults to false.
  • drop_debugger, defaults to true.
  • ecma, defaults to 5.
  • evaluate, defaults to true.
  • global_defs, defaults to {}.
  • hoist_funs, defaults to false.
  • hoist_props, defaults to true.
  • hoist_vars, defaults to false.
  • ie8, Ignored.
  • if_return, defaults to true.
  • inline, defaults to true.
  • join_vars, defaults to true.
  • keep_classnames, defaults to false.
  • keep_fargs, defaults to false.
  • keep_infinity, defaults to false.
  • loops, defaults to true.
  • negate_iife, defaults to true.
  • passes, defaults to 0, which means no limit.
  • properties, defaults to true.
  • pure_getters, defaults to ``.
  • pure_funcs, defaults to []. Type is an array of string.
  • reduce_funcs, defaults to false.
  • reduce_vars, defaults to true.
  • sequences, defaults to true.
  • side_effects, defaults to true.
  • switches, defaults to true.
  • top_retain, defaults to ``.
  • toplevel, defaults to true.
  • typeofs, defaults to true.
  • unsafe, defaults to false.
  • unsafe_arrows, defaults to false.
  • unsafe_comps, defaults to false.
  • unsafe_Function, defaults to false.
  • unsafe_math, defaults to false.
  • unsafe_symbols, defaults to false.
  • unsafe_methods, defaults to false.
  • unsafe_proto, defaults to false.
  • unsafe_regexp, defaults to false.
  • unsafe_undefined, defaults to false.
  • unused, defaults to true.
  • module, Ignored. Currently, all files are treated as module.

jsc.minify.mangle

Type: boolean | object.

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

.swcrc
{
  "jsc": {
    "minify": {
      "mangle": true // equivalent to {}
    }
  }
}
  • props, Defaults to false, and true is identical to {}.
  • topLevel, Defaults to true. Aliased as toplevel for compatibility with terser.
  • keepClassNames, Defaults to false. Aliased as keep_classnames for compatibility with terser.
  • keepFnNames, Defaults to false.
  • keepPrivateProps, Defaults to false. Aliased as keep_private_props for compatibility with terser.
  • reserved, Defaults to []
  • ie8, Ignored.
  • safari10, Defaults to false.

jsc.minify.mangle.properties

Type: object.

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: Don't use these names as properties.

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

  • regex: Mangle properties only if it matches this regex

jsc.minify.format

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

  • asciiOnly, Defaults to false. Implemented as v1.2.184 and aliased as ascii_only for compatibility with terser.
  • beautify, Defaults to false. Currently noop.
  • braces, Defaults to false. Currently noop.
  • comments, Defaults to false.
    • false removes all comments
    • 'some' preserves some comments
    • 'all' preserves all comments
  • ecma, Defaults to 5. Currently noop.
  • indentLevel, Currently noop and aliases as indent_level for compatibility with terser.
  • indentStart, Currently noop and aliases as indent_start for compatibility with terser.
  • inlineScript, Currently noop and aliases as inline_script for compatibility with terser.
  • keepNumbers, Currently noop and aliases as keep_numbers for compatibility with terser.
  • keepQuotedProps, Currently noop and aliases as keep_quoted_props for compatibility with terser.
  • maxLineLen, Currently noop, and aliases as max_line_len for compatibility with terser.
  • preamble, Currently noop
  • quoteKeys, Currently noop and aliases as quote_keys for compatibility with terser.
  • quoteStyle, Currently noop and aliases as quote_style for compatibility with terser.
  • preserveAnnotations, Currently noop and aliases as preserve_annotations for compatibility with terser.
  • safari10, Currently noop.
  • semicolons, Currently noop.
  • shebang, Currently noop.
  • webkit, Currently noop.
  • wrapIife, Currently noop and aliases as wrap_iife for compatibility with terser.
  • wrapFuncArgs, Currently noop and aliases as wrap_func_args for compatibility with terser.

@swc/core Usage

swc.minify(code, options)

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);"`);

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

swc.minifySync(code, options)

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);"`);

Returns { code: string, map: string }.

APIs for WebAssembly

Replacing Terser

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

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