Skip to content

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

@chunghyunleeme

Description

@chunghyunleeme

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

Metadata

Metadata

Assignees

Labels

enhancementNew feature or request

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions