1. babel-preset-minify
Babel preset for all minify plugins.
babel-preset-minify
Package: babel-preset-minify
Created by: babel
Last modified: Mon, 13 Jun 2022 04:07:27 GMT
Version: 0.5.2
License: MIT
Downloads: 1,938,727
Repository: https://github.com/babel/minify/tree/master/packages/babel-preset-minify

Install

npm install babel-preset-minify
yarn add babel-preset-minify

babel-preset-minify

Babel preset for all minify plugins.

Install

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

Usage

Via .babelrc (Recommended)

.babelrc

 {
  "presets": ["minify"]
}

or pass in options -

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

Via CLI

 babel script.js --presets minify

Via Node API

 const babel = require("@babel/core");
const fs = require("fs");

const code = fs.readFileSync("./input.js").toString();

const minified = babel.transform(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
OptionName Plugin DefaultValue
booleans transform-minify-booleans true
builtIns minify-builtins true
consecutiveAdds transform-inline-consecutive-adds true
deadcode minify-dead-code-elimination true
evaluate minify-constant-folding true
flipComparisons minify-flip-comparisons true
guards minify-guarded-expressions true
infinity minify-infinity true
mangle minify-mangle-names true
memberExpressions transform-member-expression-literals true
mergeVars transform-merge-sibling-variables true
numericLiterals minify-numeric-literals true
propertyLiterals transform-property-literals true
regexpConstructors transform-regexp-constructors true
removeConsole transform-remove-console false
removeDebugger transform-remove-debugger false
removeUndefined transform-remove-undefined true
replace minify-replace true
simplify minify-simplify true
simplifyComparisons transform-simplify-comparison-operators true
typeConstructors minify-type-constructors true
undefinedToVoid transform-undefined-to-void true

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.
OptionName Plugins
keepFnName Passed to mangle & deadcode
keepClassName Passed to mangle & deadcode
tdz Passed to builtIns, evaluate, deadcode, removeUndefined

Examples

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

Dependencies

RELATED POST

10 Must-Know Windows Shortcuts That Will Save You Time

10 Must-Know Windows Shortcuts That Will Save You Time

Arrays vs Linked Lists: Which is Better for Memory Management in Data Structures?

Arrays vs Linked Lists: Which is Better for Memory Management in Data Structures?

Navigating AWS Networking: Essential Hacks for Smooth Operation

Navigating AWS Networking: Essential Hacks for Smooth Operation

Achieving Stunning Visuals with Unity's Global Illumination

Achieving Stunning Visuals with Unity's Global Illumination

Nim's Hidden Gems: Lesser-known Features for Writing Efficient Code

Nim's Hidden Gems: Lesser-known Features for Writing Efficient Code