Skip to content

Rolldown emits __commonJS helper call sites without the definition (vite 8.1.x Storybook build → __commonJS is not defined) #10048

Description

@stevensacks

Summary

Building a Storybook (bundled via @storybook/react-vite) with Vite 8.1.x produces an iframe chunk that calls Rolldown's full __commonJS CJS-interop helper but never emits its definition. The build succeeds; the bundle throws __commonJS is not defined as soon as it loads, so the Storybook UI never initializes. Vite 8.0.16 builds the same project cleanly — there the helper is defined where it is used. The regression tracks the Rolldown bump that shipped in Vite 8.1.0 (Rolldown 1.1.2) and persists through Vite 8.1.2 (Rolldown 1.1.3).

The tell is that the sibling helper __commonJSMin is emitted and imported correctly in the very same 8.1.x bundle, and so are Rolldown's numbered __commonJS$1 / __commonJS$2 / … instances. Only the bare __commonJS binding used by one group of call sites is referenced without ever being assigned, declared, or imported into any loaded chunk — which reads as a Rolldown codegen / chunking bug.

Environment

vite (broken) 8.1.0 and 8.1.2
vite (working) 8.0.16
rolldown bindings 1.1.2 (via vite 8.1.0), 1.1.3 (via vite 8.1.2)
storybook / @storybook/react-vite 10.4.6
node 24.18.0
pnpm 11.9.0
OS macOS (darwin arm64)

A self-contained minimal reproduction (bare @storybook/react-vite + one trivial story) is linked in the comments, along with a headless-browser check that observes the runtime error.

Steps to reproduce

  1. A project using @storybook/react-vite with vite@8.1.2 installed.

  2. storybook build — the build succeeds.

  3. Serve / open the built storybook-static (or let Chromatic publish + verify it).

  4. The iframe bundle throws on load:

    ReferenceError: __commonJS is not defined
    

Evidence

From the built iframe chunk (build.minify = false, so the helper keeps its readable name). The bare __commonJS is called four times — wrapping a CommonJS chain pulled in by react-i18next (react-i18next → hoist-non-react-statics → react-is):

$ grep -oE '__commonJS\(\{ "[^"]+"' storybook-static/assets/iframe-*.js
__commonJS({ "…/react-is/cjs/react-is.production.min.js"
__commonJS({ "…/react-is/cjs/react-is.development.js"
__commonJS({ "…/react-is/index.js"
__commonJS({ "…/hoist-non-react-statics/dist/hoist-non-react-statics.cjs.js"

…but its definition — as an assignment, a declaration, or an import — is emitted into no chunk in the bundle:

# bare __commonJS: assignment `__commonJS =`, declaration, or `import { … as __commonJS }`
$ grep -rnE '(^|[^0-9$A-Za-z_])__commonJS[[:space:]]*=|as __commonJS[[:space:],}]|(var|let|const|function)[[:space:]]+__commonJS\b' storybook-static/assets
(no match)

# meanwhile the sibling helper IS defined and imported in the same build:
$ grep -rl '__commonJSMin[[:space:]]*=' storybook-static/assets   # defined
$ grep -rl 'as __commonJSMin' storybook-static/assets             # imported

Note on grepping: matching only __commonJS = is misleading, because Rolldown also emits the helper via bare assignment and via numbered variants (__commonJS$1 =, __commonJS2 =, …). Those numbered instances are defined here; it is specifically the bare __commonJS used by the four call sites above that is missing. Under default minification the same holds — the orphaned helper is just renamed (e.g. K) and still never defined. The reproduction includes a minify-agnostic runtime check that loads the iframe in headless Chromium and catches the actual ReferenceError.

Expected

The __commonJS interop helper is defined in (or imported into) every chunk that calls it, as under Vite 8.0.16.

Actual

The bare __commonJS call sites are emitted without the definition → runtime ReferenceError: __commonJS is not defined.

What appears to trigger it

It is a graph-size threshold rather than a single switch. In the reproduction, none of these alone trips it — they have to be combined:

  • the react-i18next → hoist-non-react-statics → react-is CommonJS chain (a dual production/development CJS package),
  • the Storybook addon set (addon-docs, addon-links, storybook-react-i18next, @vueless/storybook-dark-mode),
  • lodash forced in-bundle via ssr.noExternal.

Below the threshold Rolldown inlines a working __commonJS per chunk; at/above it, Rolldown appears to hoist the helper toward a shared chunk and then emits neither its definition nor an import for it. Plain Vite (no Storybook) and a stripped-down Storybook both build correctly, so the fault surfaces through @storybook/builder-vite's single-virtual-entry iframe build once the CommonJS graph is large enough.

Workaround

Pin vite to 8.0.16.

Notes

  • The downstream app build (a React Router SSR build) is unaffected; only the Storybook build trips the fault.

Related

Metadata

Metadata

Labels

No labels
No labels

Type

No type

Fields

Priority

None yet

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions