Skip to content

elderapo/vite8-const-enum-bug

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

1 Commit
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Vite 8 (rolldown) const enum alias bug

Minimal reproduction for a bug where rolldown emits incorrect code for a const enum with string-valued alias member.

The bug

export const enum Theme {
  Light = "Light",
  Dark = "Dark",
  Default = Theme.Light, // alias member
}

console.log(Theme.Light);   // Expected: "Light", Actual: "Default"
console.log(Theme.Default); // Expected: "Light", Actual: "Light"

Reproduce

# Vite 8 (rolldown) — broken
npm install
npm run build
node dist/main.js
# Theme.Light   = Default   ← WRONG
# Theme.Dark    = Dark
# Theme.Default = Light

# Vite 7 (esbuild) — correct
npm install vite@7
npm run build
node dist/main.js
# Theme.Light   = Light     ← correct
# Theme.Dark    = Dark
# Theme.Default = Light

What happens

Rolldown emits a numeric-style reverse mapping for the alias member:

var e = function(e) {
  return e.Light = "Light",
         e.Dark = "Dark",
         e[e.Default = e.Light] = "Default",  // reverse mapping!
         e
}({});

Step by step:

  1. e.Light = "Light"{ Light: "Light" }
  2. e.Dark = "Dark"{ Light: "Light", Dark: "Dark" }
  3. e.Default = e.Lighte.Default = "Light"
  4. e["Light"] = "Default"overwrites e.Light!

Result: { Light: "Default", Dark: "Dark", Default: "Light" }

Root cause

Rolldown treats the alias member Default = Theme.Light as a numeric enum member and emits a reverse mapping (e[value] = key). TypeScript never generates reverse mappings for string enum members — only for numeric ones.

esbuild (Vite 7) handles this correctly by resolving the alias to the literal value:

var Theme = (O => (
  O.Light = "Light",
  O.Dark = "Dark",
  O.Default = "Light",  // resolved to literal
  O
))(Theme || {});

Workaround

Use the literal value instead of an alias member:

export const enum Theme {
  Light = "Light",
  Dark = "Dark",
  Default = "Light", // literal instead of Theme.Light
}

Versions

  • vite: 8.0.1
  • rolldown: bundled with vite 8
  • typescript: 5.9.3
  • node: v22.x

About

No description, website, or topics provided.

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

 
 
 

Contributors