diff --git a/packages/plugin-rsc/examples/react-router/cf/vite.config.ts b/packages/plugin-rsc/examples/react-router/cf/vite.config.ts index 4f77cb175..a595713df 100644 --- a/packages/plugin-rsc/examples/react-router/cf/vite.config.ts +++ b/packages/plugin-rsc/examples/react-router/cf/vite.config.ts @@ -43,12 +43,12 @@ export default defineConfig({ }, ssr: { optimizeDeps: { - exclude: ['react-router'], + exclude: ['react-router', 'react', 'react-dom'], }, }, rsc: { optimizeDeps: { - exclude: ['react-router'], + exclude: ['react-router', 'react', 'react-dom'], }, }, }, diff --git a/packages/plugin-rsc/examples/starter-cf-single/vite.config.ts b/packages/plugin-rsc/examples/starter-cf-single/vite.config.ts index afb3648a8..5efd899b5 100644 --- a/packages/plugin-rsc/examples/starter-cf-single/vite.config.ts +++ b/packages/plugin-rsc/examples/starter-cf-single/vite.config.ts @@ -36,6 +36,7 @@ export default defineConfig({ }, optimizeDeps: { include: ['turbo-stream'], + exclude: ['react', 'react-dom'], }, }, ssr: { diff --git a/packages/plugin-rsc/src/plugin.ts b/packages/plugin-rsc/src/plugin.ts index 1710dff01..3889ddf76 100644 --- a/packages/plugin-rsc/src/plugin.ts +++ b/packages/plugin-rsc/src/plugin.ts @@ -365,15 +365,6 @@ export default function vitePluginRsc( noExternal, }, optimizeDeps: { - include: [ - 'react', - 'react-dom', - 'react/jsx-runtime', - 'react/jsx-dev-runtime', - 'react-dom/server.edge', - 'react-dom/static.edge', - `${REACT_SERVER_DOM_NAME}/client.edge`, - ], exclude: [PKG_NAME], }, }, @@ -392,14 +383,6 @@ export default function vitePluginRsc( noExternal, }, optimizeDeps: { - include: [ - 'react', - 'react-dom', - 'react/jsx-runtime', - 'react/jsx-dev-runtime', - `${REACT_SERVER_DOM_NAME}/server.edge`, - `${REACT_SERVER_DOM_NAME}/client.edge`, - ], exclude: [PKG_NAME], }, }, @@ -976,6 +959,7 @@ import.meta.hot.on("rsc:update", () => { return code }, ), + ...cjsModuleRunnerPlugin(), { // make `AsyncLocalStorage` available globally for React edge build (required for React.cache, ssr preload, etc.) // https://github.com/facebook/react/blob/f14d7f0d2597ea25da12bcf97772e8803f2a394c/packages/react-server/src/forks/ReactFlightServerConfig.dom-edge.js#L16-L19 @@ -1008,7 +992,6 @@ import.meta.hot.on("rsc:update", () => { ? [validateImportPlugin()] : []), scanBuildStripPlugin({ manager }), - ...cjsModuleRunnerPlugin(), ] } diff --git a/packages/plugin-rsc/src/plugins/cjs.ts b/packages/plugin-rsc/src/plugins/cjs.ts index dc0e62160..f167f185d 100644 --- a/packages/plugin-rsc/src/plugins/cjs.ts +++ b/packages/plugin-rsc/src/plugins/cjs.ts @@ -19,7 +19,7 @@ export function cjsModuleRunnerPlugin(): Plugin[] { applyToEnvironment: (env) => env.config.dev.moduleRunnerTransform, async transform(code, id) { if ( - id.includes('/node_modules/') && + (id.includes('/node_modules/') || isDevCjs(id)) && !id.startsWith(this.environment.config.cacheDir) && /\b(require|exports)\b/.test(code) ) { @@ -72,6 +72,10 @@ export default module.exports; } function extractPackageKey(id: string): string { + if (isDevCjs(id)) { + return '@vitejs/plugin-rsc' + } + // .../.yarn/cache/abc/... => abc const yarnMatch = id.match(/\/.yarn\/cache\/([^/]+)/) if (yarnMatch) { @@ -89,3 +93,11 @@ function extractPackageKey(id: string): string { } return id } + +// this is needed only when developing package itself within pnpm workspace +function isDevCjs(id: string): boolean { + return ( + !import.meta.url.includes('/node_modules/') && + id.includes('/vendor/react-server-dom/') + ) +}