Important: This documentation covers Yarn 1 (Classic).
For Yarn 2+ docs and migration guide, see yarnpkg.com.

Package detail

babel-dead-code-elimination

pcattori2mMIT1.0.10TypeScript support: included

Composable primitives for dead code elimination in Babel

babel, dead, code, elimination

readme

babel-dead-code-elimination

Composable primitives for dead code elimination in Babel

This package is not a Babel plugin, but rather a set of composable primitives to author your own Babel transforms and plugins.

Install

npm install babel-dead-code-elimination

deadCodeElimination

Eliminates unused code from the Babel AST by repeatedly removing unreferenced identifiers.

import { parse } from "@babel/parser"
import generate from "@babel/generator"

import { deadCodeElimination } from "babel-dead-code-elimination"

let source = "..."
let ast = parse(source, { sourceType: "module" })
deadCodeElimination(ast)
let result = generate(ast).code

findReferencedIdentifiers

Find identifiers that are currently referenced in the Babel AST.

Useful for limiting deadCodeElimination to only eliminate newly unreferenced identifiers, as a best effort to preserve any intentional side-effects in the source.

import { parse } from "@babel/parser"
import generate from "@babel/generator"
import traverse from "@babel/traverse"

import {
  deadCodeElimination,
  findReferencedIdentifiers,
} from "babel-dead-code-elimination"

let source = "..."
let ast = parse(source, { sourceType: "module" })
let referenced = findReferencedIdentifiers(ast)

traverse(ast, {
  /* ... your custom transform goes here ... */
})

deadCodeElimination(ast, referenced)
let result = generate(ast).code

Prior art

Credit to Jason Miller for the initial implementation. Thanks to these projects for exploring dead code elimination:

changelog

babel-dead-code-elimination

1.0.10

Patch Changes

  • 698706a: Respect candidates within when eliminating unreferenced identifiers in an object pattern

1.0.9

Patch Changes

  • d255b12: Preserve unused variables in object patterns when rest element is used. That way, it remains filtered out of the rest element.

    For example:

    let { a, ...rest } = { a: 1, b: 2, c: 3 }
    //    ^ `a` is unused
    
    console.log(rest)
    // { b: 2, c: 3 }

    Eliminating a would incorrectly change runtime behavior:

    let { ...rest } = { a: 1, b: 2, c: 3 }
    
    console.log(rest)
    // { a: 1, b: 2, c: 3 }
    //   ^^^^^ this shouldn't be here...

    So it is preserved as-is instead.

1.0.8

Patch Changes

  • c653dd3: Do not eliminate const- nor let-declared for-loop iterator variables

    Previously, only var-declared iterator variables were preserved within for...of and for...in loops. Now, iterator variables declared via const and let are also preserved.

1.0.7

Patch Changes

  • 81ef06b:
    • Do not eliminate for-loop iterator variables (for...of/for...in)
    • Do not eliminate reassigned variables

1.0.6

Patch Changes

  • d4690c2: Do not eliminate empty object/array pattern function parameters

    Function parameters are not dead code

1.0.5

Patch Changes

  • 3cf19e5: Fix: do not check function expressions for referenced variables

    Function expressions do not add their names to outer scopes

  • 5149b08: Do not eliminate arrow expressions

    Arrow expressions do not add names to the outer scope. Arrow expressions bound to names via variable declarators are already handled by VariableDeclarator visitor.

  • 86af914: Do not eliminate unreferenced variables from array patterns and object patterns when they do not match the provided candidates

    Previously, the candidates were passed in to deadCodeElimination were not consulted when removing unreferenced variables from within patterns. This was a bug and has now been fixed.

1.0.4

Patch Changes

  • ade9eee: Fix: do not eliminate function expressions

    Function expressions do not add their names to outer scope, so they should never be dead code eliminated

1.0.3

Patch Changes

  • ce456b5: Fix referenced variable finding within object patterns and array patterns

1.0.2

Patch Changes

  • bd5e331: Fix elimination for object patterns and array patterns

    Previously, running dead code elimination on object patterns and array patterns (aka destructuring) was brittle. For example:

    const {
      a: { b: c },
    } = z
    console.log(c)

    Dead code elimination used to incorrectly remove the entire variable declaration even though c is referenced:

    -const {
    -  a: { b: c },
    -} = z
     console.log(c);

    This was caused by erroneous detection of a and b as unreferenced variables. But a and b are not variables, they are object property keys. Only c is a variable and it is referenced.

    This is now corrected so that variables in object patterns and array patterns are detected only within values of object properties. This also correctly accounts for cases where the key and value are the same for example { a }.

1.0.1

Patch Changes

  • c2d0e23: Provide main and module fields in package.json for older bundlers

1.0.0

Major Changes

  • 8264d19: Initial release

    deadCodeElimination

    Eliminates unused code from the Babel AST by repeatedly removing unreferenced identifiers.

    deadCodeElimination(ast)

    findReferencedIdentifiers

    Find identifiers that are currently referenced in the Babel AST.

    Useful for limiting deadCodeElimination to only eliminate newly unreferenced identifiers, as a best effort to preserve any intentional side-effects in the source.

    let ast = parse(source, { sourceType: "module" })
    let referenced = findReferencedIdentifiers(ast)
    
    traverse(ast, {
      /* ... your custom transform goes here ... */
    })
    
    deadCodeElimination(ast, referenced)