rolldown-vite: Class names like Error, Promise, etc. are unexpectedly renamed to Error$1, Promise$1 during build

Describe the bug

Environment:

  • rolldown-vite version: 6.3.21 (with rolldown)
  • Bundler: rolldown (SWC-based)
  • Target: es2017
  • Format: iife
  • Output: minify: ‘esbuild’ (but also happens without minify)

Problem When I use a class with a name like Error, Promise, or Map inside a module, and that module is bundled into a single IIFE (via vite build using rolldown), the class is automatically renamed to Error$1, Promise$1, etc.

Example input:

export class Error extends AbstractEvent {
  static NAME = 'SomeName';
  constructor(params: any) {
    super(Error.NAME, params);
  }
}

Expected output (like with Rollup):

class Error extends AbstractEvent { ... }

Actual output with Vite + Rolldown:

class Error$1 extends AbstractEvent { ... }
Error$1.NAME = 'SomeName';

This renaming occurs even though my code is valid and doesn’t conflict, and it breaks consistency when comparing Rollup and Rolldown outputs.

Cause After investigation, I found that this happens only when the same name is used outside the IIFE scope in a new expression. For example, if Vite injects (or preserves) the following outside of the bundled module:

throw new Error('Something went wrong');

…and then, inside the IIFE, I define:

class Error { ... }

Then SWC renames the class to Error$1 to avoid shadowing the global Error constructor. This behavior also happens for other names like Promise, Map, Set, etc. — only when they are used with new outside the IIFE.

This behavior did not occur in Rollup, even in IIFE builds, which keeps the class name untouched. The change affects output readability and breaks expectations during migration from Rollup to Rolldown.

Reproduction

https://stackblitz.com/edit/vitejs-rolldown-vite-fmbu75dy?file=dist%2Fevent.js,vite.config.mjs,src%2Fts%2Ftest%2Ferror-class.ts,src%2Fts%2Ftest%2Fthrower.ts,src%2Fts%2Ftest%2Findex.ts

Steps to reproduce

  1. run yarn build
  2. go to /dist dir and see Error$1

System Info

System:
    OS: Linux 6.10 Debian GNU/Linux 12 (bookworm) 12 (bookworm)
    CPU: (10) arm64 unknown
    Memory: 5.45 GB / 7.65 GB
    Container: Yes
    Shell: 5.2.15 - /bin/bash
  Binaries:
    Node: 20.19.2 - /usr/local/bin/node
    Yarn: 1.22.22 - /usr/local/bin/yarn
    npm: 10.8.2 - /usr/local/bin/npm
  npmPackages:
    rolldown: ^1.0.0-beta.11 => 1.0.0-beta.17 
    rolldown-vite: ^6.3.21 => 6.3.21

Used Package Manager

yarn

Logs

No response

Validations

About this issue

  • Original URL
  • State: closed
  • Created a year ago
  • Reactions: 1
  • Comments: 16 (5 by maintainers)

Most upvoted comments

upstream pr is merged, please wait for next rolldown-vite release

We generate a public api, and currently use rollup config as I showed. At the moment, when trying to get the name of the Error class via Error.name, the client will receive ‘Error’, but with the code received after generating vite + rolldown, the client in such a case (Error.name) will receive ‘Error$1’, which is a possible compatibility issue when switching from rollup to vite/rolldown.

Got it, we’ll take another look at this issue.

We generate a public api, and currently use rollup config as I showed. At the moment, when trying to get the name of the Error class via Error.name, the client will receive ‘Error’, but with the code received after generating vite + rolldown, the client in such a case (Error.name) will receive ‘Error$1’, which is a possible compatibility issue when switching from rollup to vite/rolldown.