Install

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

Usage

.babelrc

  1. {
  2. "presets": ["minify"]
  3. }

or pass in options -

  1. {
  2. "presets": [["minify", {
  3. "mangle": {
  4. "exclude": ["MyCustomError"]
  5. },
  6. "unsafe": {
  7. "typeConstructors": false
  8. },
  9. "keepFnName": true
  10. }]]
  11. }

Via CLI

  1. babel script.js --presets minify

Via Node API

  1. require("@babel/core").transform("code", {
  2. presets: ["minify"]
  3. });

Options

Two types of options:

  • 1-1 mapping with plugin
  • 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

  1. {
  2. "presets": [["minify", {
  3. "evaluate": false,
  4. "mangle": true
  5. }]]
  6. }
  1. {
  2. "presets": [["minify", {
  3. "mangle": {
  4. "exclude": ["ParserError", "NetworkError"]
  5. }
  6. }]]
  7. }
  1. {
  2. "presets": [["minify", {
  3. "keepFnName": true
  4. }]]
  5. }
  6. // is the same as
  7. {
  8. "presets": [["minify", {
  9. "mangle": {
  10. "keepFnName": true
  11. },
  12. "deadcode": {
  13. "keepFnName": true
  14. }
  15. }]]
  16. }

You can read more about configuring preset options here