swc-project / swc-project/plugins

[Feature Request] Dead code elimination for assert statements in production builds

Open
#574 8 comments 0 reactions 1 assignee View on GitHub

@kdy1 is already working on this.

Since Jan 14, 2026.

enhancement
Dominant language
Rust
Stars
389
Forks
106
Avg merge
10m
Merged PRs (30d)
1

Description

Describe the feature

It would be useful to have an option to strip assert statements during production builds, similar to how other languages handle assertions (e.g., Python's -O flag, C's NDEBUG).

Use case:
import assert from 'assert';

function divide(a, b) {
  assert(b !== 0, 'Division by zero');
  return a / b;
}

In development, assertions help catch bugs early. In production, they add unnecessary overhead and bundle size.
Proposed solution:
A configuration option in .swcrc:

{
  "jsc": {
    "transform": {
      "optimizer": {
        "removeAsserts": true
      }
    }
  }
}

Or via define to replace assert with a no-op that can be tree-shaken.

Alternatives considered:

Using process.env.NODE_ENV checks manually
Babel plugin (babel-plugin-unassert)
Custom SWC plugin

Babel plugin or link to the feature description

No response

Additional context

No response

Contributor guide

No contributing guide indexed for this repository

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.