Skip to content

Commit aeb2e17

Browse files
Fix configuring resolve conditions for Vite v6 (#12644)
Co-authored-by: Mark Dalgleish <[email protected]>
1 parent db92c69 commit aeb2e17

File tree

4 files changed

+55
-9
lines changed

4 files changed

+55
-9
lines changed

.changeset/afraid-jeans-drive.md

+5
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
"@react-router/dev": patch
3+
---
4+
5+
Fix default external conditions in Vite v6. This fixes resolution issues with certain npm packages.

contributors.yml

+1
Original file line numberDiff line numberDiff line change
@@ -268,6 +268,7 @@
268268
- shamsup
269269
- shihanng
270270
- shivamsinghchahar
271+
- silvenon
271272
- SimenB
272273
- SkayuX
273274
- skratchdot

packages/react-router-dev/vite/cloudflare-dev-proxy.ts

+23-6
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import { type Plugin } from "vite";
44
import { type GetPlatformProxyOptions, type PlatformProxy } from "wrangler";
55

66
import { fromNodeRequest, toNodeRequest } from "./node-adapter";
7+
import { preloadVite, getVite } from "./vite";
78

89
let serverBuildId = "virtual:react-router/server-build";
910

@@ -43,13 +44,29 @@ export const cloudflareDevProxyVitePlugin = <Env, Cf extends CfProperties>(
4344

4445
return {
4546
name: PLUGIN_NAME,
46-
config: () => ({
47-
ssr: {
48-
resolve: {
49-
externalConditions: ["workerd", "worker"],
47+
config: async () => {
48+
await preloadVite();
49+
const vite = getVite();
50+
// This is a compatibility layer for Vite 5. Default conditions were
51+
// automatically added to any custom conditions in Vite 5, but Vite 6
52+
// removed this behavior. Instead, the default conditions are overridden
53+
// by any custom conditions. If we wish to retain the default
54+
// conditions, we need to manually merge them using the provided default
55+
// conditions arrays exported from Vite. In Vite 5, these default
56+
// conditions arrays do not exist.
57+
// https://vite.dev/guide/migration.html#default-value-for-resolve-conditions
58+
const serverConditions: string[] = [
59+
...(vite.defaultServerConditions ?? []),
60+
];
61+
62+
return {
63+
ssr: {
64+
resolve: {
65+
externalConditions: ["workerd", "worker", ...serverConditions],
66+
},
5067
},
51-
},
52-
}),
68+
};
69+
},
5370
configResolved: (viteConfig) => {
5471
let pluginIndex = (name: string) =>
5572
viteConfig.plugins.findIndex((plugin) => plugin.name === name);

packages/react-router-dev/vite/plugin.ts

+26-3
Original file line numberDiff line numberDiff line change
@@ -738,6 +738,21 @@ export const reactRouterVitePlugin: ReactRouterVitePlugin = () => {
738738
viteConfigEnv = _viteConfigEnv;
739739
viteCommand = viteConfigEnv.command;
740740

741+
// This is a compatibility layer for Vite 5. Default conditions were
742+
// automatically added to any custom conditions in Vite 5, but Vite 6
743+
// removed this behavior. Instead, the default conditions are overridden
744+
// by any custom conditions. If we wish to retain the default
745+
// conditions, we need to manually merge them using the provided default
746+
// conditions arrays exported from Vite. In Vite 5, these default
747+
// conditions arrays do not exist.
748+
// https://vite.dev/guide/migration.html#default-value-for-resolve-conditions
749+
let viteClientConditions: string[] = [
750+
...(vite.defaultClientConditions ?? []),
751+
];
752+
let viteServerConditions: string[] = [
753+
...(vite.defaultServerConditions ?? []),
754+
];
755+
741756
logger = vite.createLogger(viteUserConfig.logLevel, {
742757
prefix: "[react-router]",
743758
});
@@ -804,9 +819,14 @@ export const reactRouterVitePlugin: ReactRouterVitePlugin = () => {
804819
ssr: {
805820
external: ssrExternals,
806821
resolve: {
807-
conditions: viteCommand === "build" ? [] : ["development"],
822+
conditions:
823+
viteCommand === "build"
824+
? viteServerConditions
825+
: ["development", ...viteServerConditions],
808826
externalConditions:
809-
viteCommand === "build" ? [] : ["development"],
827+
viteCommand === "build"
828+
? viteServerConditions
829+
: ["development", ...viteServerConditions],
810830
},
811831
},
812832
optimizeDeps: {
@@ -853,7 +873,10 @@ export const reactRouterVitePlugin: ReactRouterVitePlugin = () => {
853873
"react-router/dom",
854874
"react-router-dom",
855875
],
856-
conditions: viteCommand === "build" ? [] : ["development"],
876+
conditions:
877+
viteCommand === "build"
878+
? viteClientConditions
879+
: ["development", ...viteClientConditions],
857880
},
858881
base: viteUserConfig.base,
859882

0 commit comments

Comments
 (0)