Minify dead code elimination

Inlines bindings when possible. Tries to evaluate expressions and prunes unreachable as a result

示例

输入

function foo() {var x = 1;}
function bar() { var x = f(); }
function baz() {
  var x = 1;
  console.log(x);
  function unused() {
    return 5;
  }
}

输出

function foo() {}
function bar() { f(); }
function baz() {
  console.log(1);
}

安装

npm install babel-plugin-minify-dead-code-elimination

使用

.babelrc

// without options
{
  "plugins": ["minify-dead-code-elimination"]
}

// with options
{
  "plugins": ["minify-dead-code-elimination", { "optimizeRawSize": true }]
}

通过 CLI

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

通过 Node API

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

可选项

  • keepFnName - 防止插件删除函数名。适用于代码,具体取决于fn.name
  • keepFnArgs - 防止插件删除函数参数。适用于代码,具体取决于fn.length
  • keepClassName - 防止插件删除类名。 适用于代码,具体取决于cls.name
  • tdz - TDZ的配置 (Temporal Dead Zone)(暂时性死区)