babel-plugin-transform-dead-code-elimination
raw JSON → 2.2.3 verified Sat Apr 25 auth: no javascript deprecated
A Babel 6 plugin that eliminates dead (unreachable) code, including unused variables, unreachable statements, and dead logical operators. It is a fork of babel-plugin-dead-code-elimination with additional fixes and features. Current stable version is v2.2.3, with releases in 2016 (last release 2.2.3). It is specific to Babel 6 and not compatible with Babel 7+. Key differentiators: supports experimental inlining (but warns it may break code), handles hoisted async functions correctly, and properly eliminates repeated var/function declarations.
Common errors
error Error: Plugin transform-dead-code-elimination requires a Babel 6 version ↓
cause Using plugin with Babel 7 or later.
fix
Use @babel/plugin-transform-dead-code-elimination for Babel 7.
error AssertionError [ERR_ASSERTION]: false == true ↓
cause Bug with repeated var/function declarations in older versions.
fix
Upgrade to version 2.2.2 or later.
Warnings
gotcha experimentalInlining option can break code; use with caution. ↓
fix Avoid setting experimentalInlining: true unless you thoroughly test output.
deprecated This plugin is for Babel 6 only. Not compatible with Babel 7+. ↓
fix Use @babel/plugin-transform-dead-code-elimination or equivalent for Babel 7.
gotcha Plugin may incorrectly eliminate hoisted async functions if transform-async-to-generator is not used. ↓
fix Upgrade to version 2.2.1 or later.
Install
npm install babel-plugin-transform-dead-code-elimination yarn add babel-plugin-transform-dead-code-elimination pnpm add babel-plugin-transform-dead-code-elimination Imports
- default wrong
const plugin = require('babel-plugin-transform-dead-code-elimination')correctimport plugin from 'babel-plugin-transform-dead-code-elimination'
Quickstart
// .babelrc
{
"plugins": [
"transform-dead-code-elimination"
]
}
// Input: dead code
function example() {
if (false) {
return 1;
}
return 2;
var x = 3; // unused
}
// Output: dead code removed
function example() {
return 2;
}