Babel Dead Code Elimination
raw JSON → 1.0.12 verified Sat Apr 25 auth: no javascript
A library providing composable primitives for dead code elimination in Babel AST, version 1.0.12. It is not a Babel plugin but a set of utilities (deadCodeElimination, findReferencedIdentifiers) to integrate into custom transforms. Released as a stable package with frequent patches, it differs from full-featured plugins by offering fine-grained control and working directly with the AST. Main use cases: removing unused variables, handling side-effects carefully via referenced identifier tracking, and preserving runtime behavior (e.g., object rest patterns). Ships TypeScript types.
Common errors
error Cannot find module 'babel-dead-code-elimination' ↓
cause Package not installed or CommonJS/ESM mismatch (e.g., using require in ESM project).
fix
Run npm install babel-dead-code-elimination. Use import { ... } from 'babel-dead-code-elimination' for ESM, or const { ... } = require('babel-dead-code-elimination') for CommonJS.
error TypeError: deadCodeElimination is not a function ↓
cause Using default import instead of named import.
fix
Use named import: import { deadCodeElimination } from 'babel-dead-code-elimination'.
error Cannot use import statement outside a module ↓
cause Running TypeScript without module configuration or using ESM syntax in CommonJS environment.
fix
Set type: 'module' in package.json or use .mjs extension. Alternatively, use require.
Warnings
gotcha deadCodeElimination will remove variables even if they have side-effects in their initializer (e.g., const x = sideEffect()) unless you pass a set of referenced identifiers. ↓
fix Use findReferencedIdentifiers before transforms and pass the set as second argument to deadCodeElimination to preserve existing references.
deprecated In v1.0.5 and earlier, function expressions were incorrectly eliminated. This was fixed in v1.0.4 and v1.0.5. ↓
fix Upgrade to >=1.0.5 to avoid eliminating function expressions.
breaking In v1.0.9, object pattern rest elements behave differently: unused variables before rest are preserved to avoid changing rest output. ↓
fix Ensure your code does not rely on elimination of unused variables in object patterns with rest elements (e.g., let { a, ...rest } = obj;).
gotcha Arrow functions and function expressions are never eliminated because they do not add names to outer scope. Only variable declarators referencing them can be eliminated. ↓
fix If you rely on elimination of arrow functions, reconsider the design; they are safe from removal.
gotcha For-loop iterator variables (var, let, const) in for...of/for...in are not eliminated since v1.0.7, but earlier versions only preserved var. ↓
fix Upgrade to >=1.0.8 to ensure const/let iterators are preserved.
Install
npm install babel-dead-code-elimination yarn add babel-dead-code-elimination pnpm add babel-dead-code-elimination Imports
- deadCodeElimination wrong
import deadCodeElimination from 'babel-dead-code-elimination'correctimport { deadCodeElimination } from 'babel-dead-code-elimination' - findReferencedIdentifiers wrong
const findReferencedIdentifiers = require('babel-dead-code-elimination').findReferencedIdentifierscorrectimport { findReferencedIdentifiers } from 'babel-dead-code-elimination' - types
import type { ... } from 'babel-dead-code-elimination'
Quickstart
import { parse } from '@babel/parser'
import generate from '@babel/generator'
import { deadCodeElimination } from 'babel-dead-code-elimination'
const source = `
const a = 1
const b = 2
console.log(a)
`
const ast = parse(source, { sourceType: 'module' })
deadCodeElimination(ast)
const result = generate(ast).code
console.log(result)
// Expected output: "const a = 1;\nconsole.log(a);"