Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 7 additions & 0 deletions .changeset/fix-vite-node-condition.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
---
"@react-router/dev": patch
---

fix(vite): do not force `node` condition in non-Node environments

When `v8_viteEnvironmentApi` is enabled, the `"node"` condition is no longer unconditionally added to `externalConditions` for all SSR environments. Instead, it is set per-environment in the `configEnvironment` hook, using `noExternal: true` as the signal that the target runtime is not Node.js (e.g. Cloudflare Workers). This fixes builds for non-Node runtimes that cannot import Node builtins like `http` and `https`.
1 change: 1 addition & 0 deletions contributors.yml
Original file line number Diff line number Diff line change
Expand Up @@ -215,6 +215,7 @@
- justjavac
- kachun333
- Kakamotobi
- Kanevry
- kantuni
- kaoths
- kapil-patel
Expand Down
21 changes: 19 additions & 2 deletions packages/react-router-dev/vite/plugin.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1474,6 +1474,18 @@ export const reactRouterVitePlugin: ReactRouterVitePlugin = () => {
// impact consumers because for them `ssrExternals` is undefined and
// Cloudflare's "noExternal: true" config remains intact.
options.resolve?.noExternal === true ? undefined : ssrExternals,
// Only add "node" to externalConditions for Node.js environments.
// Non-Node runtimes like Cloudflare Workers signal via
// `noExternal: true` and their adapter plugins set their own
// conditions (e.g. "workerd", "worker").
...(options.resolve?.noExternal !== true
? {
externalConditions: [
...(options.resolve?.externalConditions ?? []),
"node",
],
}
: {}),
},
optimizeDeps:
options.optimizeDeps?.noDiscovery === false
Expand Down Expand Up @@ -4059,7 +4071,10 @@ export async function getEnvironmentOptionsResolvers(

// There is no helpful export with the default external conditions (see
// https://github.com/vitejs/vite/pull/20279 for more details). So, for now,
// we are hardcording the default here.
// we are hardcoding the default here. When `v8_viteEnvironmentApi` is
// enabled, external conditions are set per-environment in the
// `configEnvironment` hook so that non-Node runtimes (e.g. Cloudflare
// Workers) are not forced to use the "node" condition.
let defaultExternalConditions = ["node"];

let baseConditions = [
Expand All @@ -4075,7 +4090,9 @@ export async function getEnvironmentOptionsResolvers(
? undefined
: ssrExternals,
conditions: [...baseConditions, ...maybeDefaultServerConditions],
externalConditions: [...baseConditions, ...defaultExternalConditions],
externalConditions: ctx.reactRouterConfig.future.v8_viteEnvironmentApi
? [...baseConditions]
: [...baseConditions, ...defaultExternalConditions],
},
build: {
// We move SSR-only assets to client assets. Note that the
Expand Down
Loading