Skip to main content

babel-preset-minify

Install

npm install babel-preset-minify --save-dev

Usage

babel.config.json
{
"presets": ["minify"]
}

or pass in options -

babel.config.json
{
"presets": [["minify", {
"mangle": {
"exclude": ["MyCustomError"]
},
"unsafe": {
"typeConstructors": false
},
"keepFnName": true
}]]
}

Via CLI

Shell
babel script.js --presets minify

Via Node API

JavaScript
require("@babel/core").transformSync("code", {
presets: ["minify"]
});

Options

Two types of options:

  1. 1-1 mapping with plugin
  2. The same option passed to multiple plugins

1-1 mapping with plugin

  • false - disable plugin
  • true - enable plugin
  • { ...pluginOpts } - enable plugin and pass pluginOpts to plugin
OptionNamePluginDefaultValue
booleanstransform-minify-booleanstrue
builtInsminify-builtinstrue
consecutiveAddstransform-inline-consecutive-addstrue
deadcodeminify-dead-code-eliminationtrue
evaluateminify-constant-foldingtrue
flipComparisonsminify-flip-comparisonstrue
guardsminify-guarded-expressionstrue
infinityminify-infinitytrue
mangleminify-mangle-namestrue
memberExpressionstransform-member-expression-literalstrue
mergeVarstransform-merge-sibling-variablestrue
numericLiteralsminify-numeric-literalstrue
propertyLiteralstransform-property-literalstrue
regexpConstructorstransform-regexp-constructorstrue
removeConsoletransform-remove-consolefalse
removeDebuggertransform-remove-debuggerfalse
removeUndefinedtransform-remove-undefinedtrue
replaceminify-replacetrue
simplifyminify-simplifytrue
simplifyComparisonstransform-simplify-comparison-operatorstrue
typeConstructorsminify-type-constructorstrue
undefinedToVoidtransform-undefined-to-voidtrue

The same option passed to multiple plugins

  • When multiple plugins require the same option, it's easier to declare it in one place. These options are passed on to two or more plugins.
OptionNamePlugins
keepFnNamePassed to mangle & deadcode
keepClassNamePassed to mangle & deadcode
tdzPassed to builtIns, evaluate, deadcode, removeUndefined

Examples

babel.config.json
{
"presets": [["minify", {
"evaluate": false,
"mangle": true
}]]
}
babel.config.json
{
"presets": [["minify", {
"mangle": {
"exclude": ["ParserError", "NetworkError"]
}
}]]
}
babel.config.json
{
"presets": [["minify", {
"keepFnName": true
}]]
}
// is the same as
{
"presets": [["minify", {
"mangle": {
"keepFnName": true
},
"deadcode": {
"keepFnName": true
}
}]]
}

You can read more about configuring preset options here