A Babel plugin that inlines bindings when possible, tries to evaluate expressions and prunes unreachable code.
npx @tessl/cli install tessl/npm-babel-plugin-minify-dead-code-elimination@0.5.0A Babel plugin that inlines bindings when possible, tries to evaluate expressions and prunes unreachable code. It removes side-effect-free statements, unused variables and function parameters, unreachable code blocks, and optimizes conditional expressions, loops, and switch statements for dead code elimination.
npm install babel-plugin-minify-dead-code-elimination --save-devThis is a Babel plugin, so it's typically not imported directly in your code but configured through Babel's configuration system:
.babelrc configuration:
{
"plugins": ["minify-dead-code-elimination"]
}With options:
{
"plugins": ["minify-dead-code-elimination", { "optimizeRawSize": true }]
}Via Node API:
const babel = require("@babel/core");
babel.transform("code", {
plugins: ["minify-dead-code-elimination"]
});The plugin automatically transforms code during Babel compilation to eliminate dead code:
Input:
function foo() {
var x = 1;
}
function bar() {
var x = f();
}
function baz() {
var x = 1;
console.log(x);
function unused() {
return 5;
}
}Output:
function foo() {}
function bar() {
f();
}
function baz() {
console.log(1);
}Configure the dead code elimination behavior with various options.
/**
* Plugin options interface
*/
interface DeadCodeEliminationOptions {
/** Enable raw size optimization by hoisting variable declarations */
optimizeRawSize?: boolean;
/** Prevent removal of function names (useful for fn.name dependent code) */
keepFnName?: boolean;
/** Prevent removal of function parameters (useful for fn.length dependent code) */
keepFnArgs?: boolean;
/** Prevent removal of class names (useful for cls.name dependent code) */
keepClassName?: boolean;
/** Account for Temporal Dead Zone in optimizations */
tdz?: boolean;
}Configuration Examples:
Via .babelrc with options:
{
"plugins": [
["minify-dead-code-elimination", {
"optimizeRawSize": true,
"keepFnName": true,
"keepFnArgs": false,
"keepClassName": false,
"tdz": false
}]
]
}Via CLI:
babel --plugins minify-dead-code-elimination script.jsVia Node API with options:
require("@babel/core").transform("code", {
plugins: [
["minify-dead-code-elimination", {
optimizeRawSize: true,
keepFnName: false
}]
]
});Inlines constant bindings and removes unused variable declarations.
The plugin identifies variables that:
Examples:
Inlining single-reference constants:
// Input
function example() {
var x = 5;
return x + 10;
}
// Output
function example() {
return 5 + 10;
}Removing unused variables:
// Input
function example() {
var unused = 42;
var used = 10;
return used;
}
// Output
function example() {
return 10;
}Removes unused function parameters from right to left when safe to do so.
The keepFnArgs option controls this behavior:
false (default): Remove unused parameterstrue: Preserve all parameters (useful when code depends on function.length)Examples:
Removing unused parameters:
// Input
function example(a, b, unusedC, unusedD) {
return a + b;
}
// Output (with keepFnArgs: false)
function example(a, b) {
return a + b;
}Removes side-effect-free statements and unreachable code.
The plugin removes:
Examples:
Removing side-effect-free statements:
// Input
function example() {
1 + 1; // side-effect-free
console.log("hello"); // has side effects
"unused string"; // side-effect-free
}
// Output
function example() {
console.log("hello");
}Removing unreachable code:
// Input
function example() {
return 42;
console.log("unreachable"); // never executes
var x = 5; // unreachable
}
// Output
function example() {
return 42;
}Evaluates and optimizes conditional expressions, loops, and switch statements with compile-time determinable values.
Conditional Expression Optimization:
// Input
function example() {
if (true) {
console.log("always runs");
} else {
console.log("never runs");
}
}
// Output
function example() {
console.log("always runs");
}Loop Optimization:
// Input
function example() {
while (false) {
console.log("never runs");
}
}
// Output
function example() {}Switch Statement Optimization:
// Input
function example() {
switch (2) {
case 1:
console.log("case 1");
break;
case 2:
console.log("case 2");
break;
default:
console.log("default");
}
}
// Output
function example() {
console.log("case 2");
}Attempts to evaluate expressions at compile time when possible.
The plugin can evaluate:
Examples:
Arithmetic evaluation:
// Input
var result = 5 + 3 * 2;
// Output
var result = 11;Boolean evaluation:
// Input
if (true && someCondition) {
doSomething();
}
// Output
if (someCondition) {
doSomething();
}Controls removal of function expression names and class expression names.
The keepFnName option controls function name removal:
false (default): Remove unreferenced function expression namestrue: Preserve function names (useful when code depends on function.name)The keepClassName option controls class name removal:
false (default): Remove unreferenced class expression namestrue: Preserve class names (useful when code depends on class.name)Examples:
Function name removal:
// Input
var fn = function namedFunction() {
return 42;
};
// Output (with keepFnName: false)
var fn = function() {
return 42;
};Removes redundant "use strict" directives in nested scopes when parent scope already has one.
Examples:
// Input
"use strict";
function example() {
"use strict"; // redundant
return 42;
}
// Output
"use strict";
function example() {
return 42;
}When optimizeRawSize is enabled, hoists variable declarations to reduce raw code size (though this may hurt gzip compression).
Examples:
// Input (with optimizeRawSize: true)
function example() {
if (condition) {
var x = 1;
console.log(x);
}
for (var i = 0; i < 10; i++) {
var y = i * 2;
console.log(y);
}
}
// Output (variables hoisted to top)
function example() {
var x, i, y;
if (condition) {
x = 1;
console.log(x);
}
for (i = 0; i < 10; i++) {
y = i * 2;
console.log(y);
}
}booleanfalsebooleanfalsefunction.name property.booleanfalsefunction.length property.booleanfalseclass.name property.booleanfalseThe plugin is implemented as a Babel plugin that uses the visitor pattern to traverse and transform the Abstract Syntax Tree (AST). It operates in several phases:
The plugin integrates with Babel's transformation pipeline and can be combined with other minification plugins for comprehensive code optimization.
The plugin includes safety checks to avoid transformations that could change program semantics:
Invalid configurations or unsupported AST nodes are handled gracefully with appropriate error messages.