or run

npx @tessl/cli init
Log in

Version

Tile

Overview

Evals

Files

docs

index.md
tile.json

index.mddocs/

Babel Plugin Minify Dead Code Elimination

A 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.

Package Information

  • Package Name: babel-plugin-minify-dead-code-elimination
  • Package Type: npm
  • Language: JavaScript
  • Installation: npm install babel-plugin-minify-dead-code-elimination --save-dev

Core Imports

This 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"]
});

Basic Usage

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);
}

Capabilities

Plugin Configuration

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.js

Via Node API with options:

require("@babel/core").transform("code", {
  plugins: [
    ["minify-dead-code-elimination", {
      optimizeRawSize: true,
      keepFnName: false
    }]
  ]
});

Variable Optimization

Inlines constant bindings and removes unused variable declarations.

The plugin identifies variables that:

  • Are only referenced once and can be safely inlined
  • Are never referenced and can be removed
  • Can have their bindings inlined without side effects

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;
}

Function Parameter Optimization

Removes unused function parameters from right to left when safe to do so.

The keepFnArgs option controls this behavior:

  • false (default): Remove unused parameters
  • true: 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;
}

Statement Elimination

Removes side-effect-free statements and unreachable code.

The plugin removes:

  • Expression statements with no side effects
  • Code after return, break, or continue statements
  • Empty statements and blocks

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;
}

Control Flow Optimization

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");
}

Expression Evaluation

Attempts to evaluate expressions at compile time when possible.

The plugin can evaluate:

  • Arithmetic operations with constants
  • Boolean operations with constants
  • String operations with constants
  • Conditional expressions with constant tests

Examples:

Arithmetic evaluation:

// Input
var result = 5 + 3 * 2;

// Output
var result = 11;

Boolean evaluation:

// Input
if (true && someCondition) {
  doSomething();
}

// Output
if (someCondition) {
  doSomething();
}

Function Name and Class Name Handling

Controls removal of function expression names and class expression names.

The keepFnName option controls function name removal:

  • false (default): Remove unreferenced function expression names
  • true: Preserve function names (useful when code depends on function.name)

The keepClassName option controls class name removal:

  • false (default): Remove unreferenced class expression names
  • true: 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;
};

Use Strict Directive Optimization

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;
}

Variable Declaration Hoisting

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);
  }
}

Configuration Options

optimizeRawSize

  • Type: boolean
  • Default: false
  • Description: Enable raw size optimization by hoisting variable declarations. This may reduce raw code size but can hurt gzip compression ratios.

keepFnName

  • Type: boolean
  • Default: false
  • Description: Prevent plugin from removing function names. Useful for code depending on function.name property.

keepFnArgs

  • Type: boolean
  • Default: false
  • Description: Prevent plugin from removing function arguments. Useful for code depending on function.length property.

keepClassName

  • Type: boolean
  • Default: false
  • Description: Prevent plugin from removing class names. Useful for code depending on class.name property.

tdz

  • Type: boolean
  • Default: false
  • Description: Account for Temporal Dead Zone when optimizing let/const declarations. Enables more conservative optimizations to avoid TDZ-related runtime errors.

Plugin Architecture

The 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:

  1. Scope Analysis: Analyzes variable bindings and references across scopes
  2. Evaluation: Attempts to evaluate expressions at compile time
  3. Dead Code Identification: Identifies unreachable code, unused variables, and redundant statements
  4. Safe Removal: Removes or replaces dead code while preserving program semantics
  5. Optimization: Applies additional optimizations like variable inlining and hoisting

The plugin integrates with Babel's transformation pipeline and can be combined with other minification plugins for comprehensive code optimization.

Error Handling

The plugin includes safety checks to avoid transformations that could change program semantics:

  • Preserves side effects in expressions and statements
  • Respects function and class name dependencies when configured
  • Handles eval scopes safely by disabling optimizations
  • Validates break/continue statement contexts
  • Accounts for Temporal Dead Zone when tdz option is enabled

Invalid configurations or unsupported AST nodes are handled gracefully with appropriate error messages.