Back to all fixes
CuratedTypeScriptVite 8local

Vite 8/Rolldown: module-level side effects run in wrong order after code splitting

Problem

A side-effect import (e.g. reflect-metadata, a polyfill, or a class registered via a global side effect like Lit's hydrate-support) that must run before dependent code executes now runs too late once the build is chunked, e.g. 'Uncaught TypeError: Class extends value undefined is not a constructor or null' or a polyfill never applying. Works fine in dev, breaks only in the production build. Happens whenever Rolldown's automatic or manual code splitting separates a side-effect-only module from its dependents.

Solution

typescript
Root cause fixed upstream in rolldown PR #8899, released in Rolldown 1.0.0-rc.13 / Vite 8.0.6 for the common case (upgrade to at least that version). For cases where code splitting itself (manualChunks or automatic chunking) still reorders execution, set in vite.config.ts:
```ts
build: {
  rolldownOptions: {
    output: { strictExecutionOrder: true }
  }
}
```
This forces Rolldown to preserve import-order execution at a small bundle-size cost. Maintainers confirm this is the only reliable workaround for the general case; it is not something Vite/Rolldown intend to fix further since code splitting fundamentally cannot always preserve order.