babel-preset-minify

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

The same option passed to multiple plugins

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