Skip to content

fix(rsc): convert vendored react-server-dom-webpack to ESM#1203

Closed
remorses wants to merge 2 commits intovitejs:mainfrom
remorses:fix/vendor-esm-for-esm-runtimes
Closed

fix(rsc): convert vendored react-server-dom-webpack to ESM#1203
remorses wants to merge 2 commits intovitejs:mainfrom
remorses:fix/vendor-esm-for-esm-runtimes

Conversation

@remorses
Copy link
Copy Markdown

@remorses remorses commented Apr 28, 2026

Fixes #1204

Reproduction: https://github.com/remorses/vite-rsc-cloudflare-cjs-repro

@vitejs/plugin-rsc vendors react-server-dom-webpack into dist/vendor/ as raw CJS. These use require() and module.exports, which break on pure ESM runtimes (Cloudflare Workers, Deno Deploy).

optimizeDeps handles CJS-to-ESM conversion in dev mode, but it doesn't run during vite build. In production, Rolldown handles CJS files directly by wrapping them in __commonJS + a __require shim. This shim falls back to the real require global when its internal routing can't resolve the target. On Cloudflare Workers, require doesn't exist.

Shipping ESM vendor files eliminates the problem entirely since there's no CJS interop layer involved.

Adds an esbuild post-processing step in tsdown.config.ts that converts each CJS entry file to self-contained ESM with proper import/export statements. Removes the cjs/ subdirectory after conversion. All 424 existing tests pass.

@remorses remorses marked this pull request as draft April 28, 2026 15:48
The vendor-react-server-dom build plugin copies react-server-dom-webpack
into dist/vendor/ as raw CJS. These use require() and module.exports
which break on pure ESM runtimes (Cloudflare Workers, Deno Deploy) when
bundled with resolve.noExternal: true.

Adds an esbuild conversion step in the tsdown build config that converts
each CJS entry file to self-contained ESM in-place, then removes the
now-unused cjs/ subdirectory.

Session: ses_22b5a7d8affeczTeFI1DGoYPd5
Do not force process.env.NODE_ENV to production while converting vendored React Server DOM files. Leaving the condition for the final Vite build keeps React Server DOM in the same development or production mode as React itself, including builds that run under NODE_ENV=test.

Session: ses_22b5a7d8affeczTeFI1DGoYPd5
@hi-ogawa
Copy link
Copy Markdown
Contributor

Closing as the reproduction doesn't look right.

@hi-ogawa hi-ogawa closed this Apr 29, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Vendored react-server-dom-webpack CJS breaks on ESM-only runtimes (Cloudflare Workers)

2 participants