Skip to content

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

Open

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

Activity

  1. kdy1 commented on Jan 14, 2026

    @kdy1
    Member

    I think this should be a Wasm plugin. Are you fine with that?

  2. magic-akari commented on Jan 14, 2026

    @magic-akari
    Member

    It should be straightforward to implement. You can simply fork the existing remove-console plugin and make a few small adjustments to compile it as a Wasm plugin.

  3. chunghyunleeme commented on Jan 15, 2026

    @chunghyunleeme
    Author

    I think this should be a Wasm plugin. Are you fine with that?

    Sure, a Wasm plugin sounds good to me. I'll give it a try.

  4. chunghyunleeme commented on Jan 15, 2026

    @chunghyunleeme
    Author

    It should be straightforward to implement. You can simply fork the existing remove-console plugin and make a few small adjustments to compile it as a Wasm plugin.

    Thanks for the pointer! I'll check out the remove-console plugin and use it as a reference.

  5. kdy1 commented on Jan 19, 2026

    @kdy1
    Member
  6. claude commented on Jan 19, 2026

    @claude
  7. added a commit that references this issue on Jan 19, 2026
    9cc1298
  8. kdy1 commented on Feb 5, 2026

    @kdy1
    Member

    Claude (@claude) Implement it.

  9. claude commented on Feb 5, 2026

    @claude

    Claude finished Donny/강동윤 (@kdy1)'s task in 5m 32s —— View job • claude/issue-574-20260205-2359 • Create PR ➔


    Implementation Complete! ✅

    Successfully implemented the @swc/plugin-remove-assert Wasm plugin for dead code elimination of assert statements in production builds.

    Tasks

    • Read repository guidelines
    • Examine remove-console plugin structure
    • Create remove-assert plugin structure
    • Implement assert removal logic
    • Set up configuration files
    • Build and test
    • Commit and push changes
    • Provide PR creation link

    What was implemented

    Plugin capabilities:

    • ✅ Removes direct assert() calls
    • ✅ Removes assert.method() calls (e.g., assert.strictEqual, assert.deepEqual)
    • ✅ Configurable exclude option to preserve specific assert methods
    • ✅ Follows the same architecture as the existing remove-console plugin

    Files created:

    packages/remove-assert/
    ├── src/lib.rs                    # SWC plugin wrapper
    ├── transform/
    │   ├── src/lib.rs                # Core transformation logic
    │   └── Cargo.toml                # Transform crate config
    ├── __test__/
    │   ├── wasm.test.ts              # Test suite
    │   └── fixtures/input.js         # Test fixtures
    ├── Cargo.toml                    # Main crate config
    ├── package.json                  # NPM package
    ├── README.tmpl.md                # Documentation
    ├── CHANGELOG.md                  # Version history
    └── .npmignore                    # NPM ignore rules
    

    Usage example:

    {
      "jsc": {
        "experimental": {
          "plugins": [
            ["@swc/plugin-remove-assert", {}]
          ]
        }
      }
    }

    Or with exclusions:

    {
      "jsc": {
        "experimental": {
          "plugins": [
            ["@swc/plugin-remove-assert", { "exclude": ["strictEqual"] }]
          ]
        }
      }
    }

    Example transformation:

    Input:

    import assert from 'assert';
    
    function divide(a, b) {
      assert(b !== 0, 'Division by zero');
      assert.strictEqual(typeof a, 'number');
      return a / b;
    }

    Output:

    import assert from 'assert';
    
    function divide(a, b) {
      return a / b;
    }

    --- | Branch: claude/issue-574-20260205-2359

  10. added a commit that references this issue on Feb 6, 2026
    c0a3f41
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Labels

enhancement

Type

No type

Projects

No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions