Skip to content

Commit af2dd39

Browse files
committed
fix(moduleResolution): fixed an issue with Vite's default server conditions being misapplied to externalConditions
1 parent f33e6b7 commit af2dd39

File tree

4 files changed

+17
-11
lines changed

4 files changed

+17
-11
lines changed

.changeset/rare-tables-smile.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
"@react-router/dev": patch
3+
---
4+
5+
Fixed an issue with Vite's default server conditions being misapplied to `externalConditions`. This incorrectly enabled `module` condition for externals and broke builds with certain packages, like Emotion.

contributors.yml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@
2525
- amitdahan
2626
- AmRo045
2727
- amsal
28+
- Andarist
2829
- andreasottosson-polestar
2930
- andreiduca
3031
- antonmontrezor

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

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -59,7 +59,6 @@ export const cloudflareDevProxyVitePlugin = <Env, Cf extends CfProperties>(
5959
name: PLUGIN_NAME,
6060
config: async (config, configEnv) => {
6161
await preloadVite();
62-
const vite = getVite();
6362
// This is a compatibility layer for Vite 5. Default conditions were
6463
// automatically added to any custom conditions in Vite 5, but Vite 6
6564
// removed this behavior. Instead, the default conditions are overridden
@@ -68,9 +67,11 @@ export const cloudflareDevProxyVitePlugin = <Env, Cf extends CfProperties>(
6867
// conditions arrays exported from Vite. In Vite 5, these default
6968
// conditions arrays do not exist.
7069
// https://vite.dev/guide/migration.html#default-value-for-resolve-conditions
71-
const serverConditions: string[] = [
72-
...(vite.defaultServerConditions ?? []),
73-
];
70+
//
71+
// In addition to that, those are external conditions (do not confuse them with server conditions)
72+
// and there is no helpful export with the default external conditions (see https://github.com/vitejs/vite/pull/20279 for more details).
73+
// So, for now, we are hardcording the default here.
74+
const externalConditions: string[] = ["node"];
7475

7576
let configResult = await loadConfig({
7677
rootDirectory: config.root ?? process.cwd(),
@@ -86,7 +87,7 @@ export const cloudflareDevProxyVitePlugin = <Env, Cf extends CfProperties>(
8687
return {
8788
ssr: {
8889
resolve: {
89-
externalConditions: [...workerdConditions, ...serverConditions],
90+
externalConditions: [...workerdConditions, ...externalConditions],
9091
},
9192
},
9293
};

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

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -3451,8 +3451,7 @@ export async function getEnvironmentOptionsResolvers(
34513451
`file:///${path.join(packageRoot, "module-sync-enabled/index.mjs")}`
34523452
);
34533453
let vite = getVite();
3454-
let viteServerConditions: string[] = [
3455-
...(vite.defaultServerConditions ?? []),
3454+
let baseServerLikeConditions: string[] = [
34563455
...(moduleSyncEnabled ? ["module-sync"] : []),
34573456
];
34583457

@@ -3512,8 +3511,8 @@ export async function getEnvironmentOptionsResolvers(
35123511
}): EnvironmentOptions {
35133512
let conditions =
35143513
viteCommand === "build"
3515-
? viteServerConditions
3516-
: ["development", ...viteServerConditions];
3514+
? baseServerLikeConditions
3515+
: ["development", ...baseServerLikeConditions];
35173516

35183517
return mergeEnvironmentOptions(getBaseOptions({ viteUserConfig }), {
35193518
resolve: {
@@ -3522,8 +3521,8 @@ export async function getEnvironmentOptionsResolvers(
35223521
ctx.reactRouterConfig.future.unstable_viteEnvironmentApi
35233522
? undefined
35243523
: ssrExternals,
3525-
conditions,
3526-
externalConditions: conditions,
3524+
conditions: [...conditions, ...vite.defaultServerConditions],
3525+
externalConditions: [...conditions, "node"],
35273526
},
35283527
build: {
35293528
// We move SSR-only assets to client assets. Note that the

0 commit comments

Comments
 (0)