Minification
This feature is still under construction. We are working on making the smallest bundle size possible, without breaking changes.
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
jsc.minify.compress
Type: boolean | object
.
Similar to the compress option 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 tofalse
.conditionals
, defaults tofalse
.dead_code
, defaults tofalse
.defaults
, defaults totrue
.directives
, defaults tofalse
.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 to ``.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 tofalse
.sequences
, defaults totrue
.side_effects
, defaults totrue
.switches
, defaults tofalse
.top_retain
, defaults to ``.toplevel
, defaults to ``.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 of terser
.
{
"jsc": {
"minify": {
"mangle": true // equivalent to {}
}
}
}
properties
, Defaults tofalse
, andtrue
is identical to{}
.topLevel
, Defaults tofalse
. 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.
@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. 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