# babel-dead-code-elimination

> Composable primitives for dead code elimination in Babel

Latest version **2.0.0** (published 2026-08-12) · MIT license · 0 weekly downloads

## Install

```sh
npm install babel-dead-code-elimination
pnpm add babel-dead-code-elimination
yarn add babel-dead-code-elimination
bun add babel-dead-code-elimination
```

## Health

**Score 65/100 (B)** — status: active.

Positive: esm support; no vulnerabilities; has provenance; recently updated; high maintenance score.

Warnings: low downloads; no types.

## Facts

| | |
|---|---|
| Version | 2.0.0 |
| Published | 2026-08-12 |
| First published | 2024-05-29 |
| Weekly downloads | 0 |
| License | MIT |
| TypeScript types | none |
| Module format | ESM |
| Node | >=22.12.0 |
| Dependencies | 4 |
| Unpacked size | 19.5 KB |
| Known vulnerabilities | 0 |
| Install scripts | no |
| Provenance | attested (GitHub Actions) |
| GitHub stars | 66 |
| Author | pcattori |
| Maintainers | pcattori |
| Keywords | babel, dead, code, elimination |

## Links

- npm: https://www.npmjs.com/package/babel-dead-code-elimination
- Repository: https://github.com/pcattori/babel-dead-code-elimination
- Homepage: https://github.com/pcattori/babel-dead-code-elimination#readme
- Issues: https://github.com/pcattori/babel-dead-code-elimination/issues
- npm.io page: https://npm.io/package/babel-dead-code-elimination

## Dependencies (4)

- [@babel/core](https://npm.io/package/@babel/core.md) ^7.23.7
- [@babel/types](https://npm.io/package/@babel/types.md) ^7.23.6
- [@babel/parser](https://npm.io/package/@babel/parser.md) ^7.23.6
- [@babel/traverse](https://npm.io/package/@babel/traverse.md) ^7.23.7

## Alternatives

- [flatbuffers](https://npm.io/package/flatbuffers.md) — 6.0M weekly downloads
- [jwt-simple](https://npm.io/package/jwt-simple.md) — 259.5K weekly downloads
- [@exodus/patch-broken-hermes-typed-arrays](https://npm.io/package/@exodus/patch-broken-hermes-typed-arrays.md) — 28.5K weekly downloads
- [@native-to-anchor/buffer-layout](https://npm.io/package/@native-to-anchor/buffer-layout.md) — 12.2K weekly downloads
- [binary-parser-encoder](https://npm.io/package/binary-parser-encoder.md) — 5.3K weekly downloads

## Recent versions

- 2.0.0 (latest) — 2026-08-12
- 1.0.12 — 2026-01-13
- 1.0.11 — 2025-12-17
- 1.0.10 — 2025-03-29
- 1.0.9 — 2025-02-08
- 1.0.8 — 2024-12-17
- 1.0.7 — 2024-12-16
- 1.0.6 — 2024-07-01
- 1.0.5 — 2024-06-28
- 1.0.4 — 2024-06-28
- 1.0.3 — 2024-06-28
- 1.0.2 — 2024-06-27
- 1.0.1 — 2024-06-05
- 1.0.0 — 2024-05-29
- 0.0.0 — 2024-05-29

## README

# babel-dead-code-elimination

Composable primitives for dead code elimination in Babel

This package is **not a Babel plugin**.
It is a library for authoring your own Babel transforms and plugins.

## Install

```sh
npm install babel-dead-code-elimination
```

## Usage

You have a Babel AST and want to transform it:

```ts
import type { ParseResult } from "@babel/parser"
import type { File } from "@babel/types"

type AST = ParseResult<File>

export function transform(ast: AST) {
  // your code to mutate the AST goes here...
}
```

> [!TIP]
> This example assumes that you already have a Babel AST. If you need to parse source code or generate source code, use [`@babel/parser`](https://babeljs.io/docs/babel-parser) and [`@babel/generator`](https://babeljs.io/docs/babel-generator).

### Dead code elimination

Use `deadCodeElimination` when you want to remove unreferenced identifiers:

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

export function transform(ast: AST) {
  deadCodeElimination(ast)
}
```

For example:

```ts
// Before
const name = "Ada"
const greeting = "Hello, " + name
const unusedMessage = "Goodbye"

console.log(greeting)

// After
const name = "Ada"
const greeting = "Hello, " + name

console.log(greeting)
```

### Only target certain identifiers for removal

`deadCodeElimination` has a second argument for specifying which identifiers to consider for removal.
When provided, any identifiers not specified **will not be removed**.

This can be extremely useful if you want to run dead code elimination _after_ a custom transform.
In such cases, you may want to preserve identifiers that were already unused in the original source.
That way dead code elimination only removes identifiers that are unused _because_ of your transform.

For example, use `findReferencedIdentifiers` to get a set of referenced identifiers _prior_ to your transform and only target those for elimination:

```ts
import traverse from "@babel/traverse"

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

export function transform(ast: AST) {
  const referenced = findReferencedIdentifiers(ast)

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

  deadCodeElimination(ast, referenced)
}
```

For example, if the custom transform removes the `console.log` call:

```ts
// Before the custom transform
const startup = initializeApplication()
const greeting = "Hello"

console.log(greeting)

// After the custom transform
const startup = initializeApplication()
const greeting = "Hello"

// After dead code elimination
const startup = initializeApplication()
```

`greeting` was referenced before the custom transform and became unused afterward, so it can be removed. `startup` was already unused, so it is preserved along with its potentially side-effectful initializer.

## Prior art

Credit to [Jason Miller](https://github.com/developit) for the initial implementation.
Thanks to these projects for exploring dead code elimination:

- [Next.js](https://github.com/vercel/next.js/pull/9652)
- [babel-plugin-eliminator](https://github.com/egoist/babel-plugin-eliminator/blob/d47034ed765352c02d588afdaa40510967332b21/src/index.ts)
- [bling](https://github.com/TanStack/bling/blob/c8ee1d1ae3009ecefc747edaad45a7dfba9ecc9f/packages/bling/src/compilers.ts)

---
_Source: https://npm.io/package/babel-dead-code-elimination · Machine-readable twin of the npm.io package page. Health data is recomputed on every publish._
