babel-plugin-minify-dead-code-elimination

Example

In

  1. function foo() {var x = 1;}
  2. function bar() { var x = f(); }
  3. function baz() {
  4. var x = 1;
  5. console.log(x);
  6. function unused() {
  7. return 5;
  8. }
  9. }

Out

  1. function foo() {}
  2. function bar() { f(); }
  3. function baz() {
  4. console.log(1);
  5. }

Installation

  1. npm install babel-plugin-minify-dead-code-elimination --save-dev

Usage

.babelrc

  1. // without options
  2. {
  3. "plugins": ["minify-dead-code-elimination"]
  4. }
  5. // with options
  6. {
  7. "plugins": ["minify-dead-code-elimination", { "optimizeRawSize": true }]
  8. }

Via CLI

  1. babel --plugins minify-dead-code-elimination script.js

Via Node API

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

Options

  • keepFnName - prevent plugin from removing function name. Useful for code depending on fn.name
  • keepFnArgs - prevent plugin from removing function args. Useful for code depending on fn.length
  • keepClassName - prevent plugin from removing class name. Useful for code depending on cls.name
  • tdz - Account for TDZ (Temporal Dead Zone)

You can read more about configuring plugin options here