Minification
This section is for people who want to use swc as a last step of the build process. If you want to use the swc minifier with a bundler, see the documentation for
- swcMinify with the Terser Webpack Plugin
- SWC Minify Webpack Plugin which is a standalone tool if you only want minification.
Assumptions
SWC Minifier makes small amount of assumptions to make the minification efficient. This is similar to other minifiers.
- 
.toString()and.valueOf()don’t have side effects, and for built-in objects they have not been overridden.
- 
undefined,NaNandInfinityhave not been externally redefined.
- 
arguments.callee,arguments.callerandFunction.prototype.callerare not used.
These are not valid in strict mode.
- 
The code doesn’t expect the contents of Function.prototype.toString()orError.prototype.stackto be anything in particular.
- 
Getting and setting properties on a plain object does not cause other side effects (using .watch()orProxy).
- 
Object properties can be added, removed and modified (not prevented with Object.defineProperty(),Object.defineProperties(),Object.freeze(),Object.preventExtensions()orObject.seal()).
- 
document.allis notnull.
- 
Assigning properties to a class doesn’t have side effects and does not throw. 
- 
Acesssing declared top-level identifiers do not have side effects. 
If you declare a variable on globalThis using a getter with side-effects, SWC Minifier will likely break it.
- TDZ violation does not exist.
TDZ violation will be ignored and not preserved.
- Arithmetic expression may not have side effects.
This assumption means that you can’t rely on the exception thrown by the JS engine when adding a bigint and a number.
const a = 1n;
const b = 1;
const c = a + b;If you execute this code, the JS engine will throw an exception. But SWC Minifier will ignore it.
Related: terser - compiler assumptions
Starting with v1.2.67, you can configure SWC to minify your code by enabling minify in your .swcrc file:
{
  // Enable minification
  "minify": true,
  // Optional, configure minification options
  "jsc": {
    "minify": {
      "compress": {
        "unused": true
      },
      "mangle": true
    }
  }
}Configuration
Note about comments
If you set jsc.minify.compress to true or {}, SWC will remove all comments starting from v1.11.11.
Previous versions 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  of terser.
{
  "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- true.
- 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  of terser.
{
  "jsc": {
    "minify": {
      "mangle": true // equivalent to {}
    }
  }
}- props, Defaults to- false, and- trueis identical to- {}.
- topLevel, Defaults to- true. Aliased as- toplevelfor compatibility with- terser.
- keepClassNames, Defaults to- false. Aliased as- keep_classnamesfor compatibility with- terser.
- keepFnNames, Defaults to- false.
- keepPrivateProps, Defaults to- false. Aliased as- keep_private_propsfor compatibility with- terser.
- reserved, Defaults to- []
- ie8, Ignored.
- safari10, Defaults to- false.
jsc.minify.mangle.properties
Type: object.
Similar to the mangle properties option  of terser.
{
  "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.184and aliased as- ascii_onlyfor compatibility with- terser.
- beautify, Defaults to- false. Currently noop.
- braces, Defaults to- false. Currently noop.
- comments, Defaults to- some.- falseremoves all comments
- 'some'preserves some comments
- 'all'preserves all comments
 
- ecma, Defaults to 5. Currently noop.
- indentLevel, Currently noop and aliases as- indent_levelfor compatibility with- terser.
- indentStart, Currently noop and aliases as- indent_startfor compatibility with- terser.
- inlineScript, Aliases as- inline_scriptfor compatibility with- terserand requires- @swc/core@1.9.2or later.
- keepNumbers, Currently noop and aliases as- keep_numbersfor compatibility with- terser.
- keepQuotedProps, Currently noop and aliases as- keep_quoted_propsfor compatibility with- terser.
- maxLineLen, Currently noop, and aliases as- max_line_lenfor compatibility with- terser.
- preamble, Supported since- v1.3.66.
- quoteKeys, Currently noop and aliases as- quote_keysfor compatibility with- terser.
- quoteStyle, Currently noop and aliases as- quote_stylefor compatibility with- terser.
- preserveAnnotations, Currently noop and aliases as- preserve_annotationsfor compatibility with- terser.
- safari10, Currently noop.
- semicolons, Currently noop.
- shebang, Currently noop.
- webkit, Currently noop.
- wrapIife, Currently noop and aliases as- wrap_iifefor compatibility with- terser.
- wrapFuncArgs, Currently noop and aliases as- wrap_func_argsfor 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,
    module: 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 . Example package.json would include:
{
  "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