Minification
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 minifcation 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.
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
.
{
"jsc": {
"minify": {
"compress": true // equivalent to {}
}
}
}
arguments
, defaults tofalse
.arrows
, defaults totrue
.booleans
, defaults totrue
.booleans_as_integers
, defaults tofalse
.collapse_vars
, defaults totrue
.comparisons
, defaults totrue
.computed_props
, defaults totrue
.conditionals
, defaults totrue
.dead_code
, defaults tofalse
.defaults
, defaults totrue
.directives
, defaults totrue
.drop_console
, defaults tofalse
.drop_debugger
, defaults totrue
.ecma
, defaults to5
.evaluate
, defaults totrue
.global_defs
, defaults to{}
.hoist_funs
, defaults tofalse
.hoist_props
, defaults totrue
.hoist_vars
, defaults tofalse
.ie8
, Ignored.if_return
, defaults totrue
.inline
, defaults totrue
.join_vars
, defaults totrue
.keep_classnames
, defaults tofalse
.keep_fargs
, defaults tofalse
.keep_infinity
, defaults tofalse
.loops
, defaults totrue
.negate_iife
, defaults totrue
.passes
, defaults to0
, which means no limit.properties
, defaults totrue
.pure_getters
, defaults to ``.pure_funcs
, defaults to[]
. Type is an array of string.reduce_funcs
, defaults tofalse
.reduce_vars
, defaults totrue
.sequences
, defaults totrue
.side_effects
, defaults totrue
.switches
, defaults totrue
.top_retain
, defaults to ``.toplevel
, defaults totrue
.typeofs
, defaults totrue
.unsafe
, defaults tofalse
.unsafe_arrows
, defaults tofalse
.unsafe_comps
, defaults tofalse
.unsafe_Function
, defaults tofalse
.unsafe_math
, defaults tofalse
.unsafe_symbols
, defaults tofalse
.unsafe_methods
, defaults tofalse
.unsafe_proto
, defaults tofalse
.unsafe_regexp
, defaults tofalse
.unsafe_undefined
, defaults tofalse
.unused
, defaults totrue
.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
.
{
"jsc": {
"minify": {
"mangle": true // equivalent to {}
}
}
}
properties
, Defaults tofalse
, andtrue
is identical to{}
.topLevel
, Defaults totrue
. Aliased astoplevel
for compatibility withterser
.keepClassnames
, Defaults tofalse
. Aliased askeep_classnames
for compatibility withterser
.keepFnames
, Defaults tofalse
.keepPrivateProps
, Defaults tofalse
. Aliased askeep_private_props
for compatibility withterser
.reserved
, Defaults to[]
ie8
, Ignored.safari10
, Not implemented yet.
jsc.minify.mangle.properties
Type: object
.
Similar to the mangle properties option (opens in a new tab) 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 delcared. -
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 tofalse
. Implemented asv1.2.184
and aliased asascii_only
for compatibility withterser
.beautify
, Defaults tofalse
. Currently noop.braces
, Defaults tofalse
. Currently noop.comments
, Defaults tofalse
.false
removes all comments'some'
preserves some comments'all'
preserves all comments
ecma
, Defaults to 5. Currently noop.indentLevel
, Currently noop and aliases asindent_level
for compatibility withterser
.indentStart
, Currently noop and aliases asindent_start
for compatibility withterser
.inlineScript
, Currently noop and aliases asinline_script
for compatibility withterser
.keepNumbers
, Currently noop and aliases askeep_numbers
for compatibility withterser
.keepQuotedProps
, Currently noop and aliases askeep_quoted_props
for compatibility withterser
.maxLineLen
, Currently noop, and aliases asmax_line_len
for compatibility withterser
.preamble
, Currently noopquoteKeys
, Currently noop and aliases asquote_keys
for compatibility withterser
.quoteStyle
, Currently noop and aliases asquote_style
for compatibility withterser
.preserveAnnotations
, Currently noop and aliases aspreserve_annotations
for compatibility withterser
.safari10
, Currently noop.semicolons
, Currently noop.shebang
, Currently noop.webkit
, Currently noop.wrapIife
, Currently noop and aliases aswrap_iife
for compatibility withterser
.wrapFuncArgs
, Currently noop and aliases aswrap_func_args
for compatibility withterser
.
@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:
{
"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