Skip to content

Commit 21fc6e5

Browse files
fix(dev): fix incorrect usage of defaultServerConditions as externalConditions (#13871)
Co-authored-by: Mark Dalgleish <[email protected]>
1 parent d0b08bf commit 21fc6e5

File tree

4 files changed

+44
-15
lines changed

4 files changed

+44
-15
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+
Fix incorrectly configured `externalConditions` which had 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
@@ -26,6 +26,7 @@
2626
- amitdahan
2727
- AmRo045
2828
- amsal
29+
- Andarist
2930
- andreasottosson-polestar
3031
- andreiduca
3132
- antonmontrezor

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

Lines changed: 7 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,12 @@ 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, these are external conditions (do not confuse them
72+
// with server conditions) and there is no helpful export with the default
73+
// external conditions (see https://github.com/vitejs/vite/pull/20279 for
74+
// more details). So, for now, we are hardcording the default here.
75+
const externalConditions: string[] = ["node"];
7476

7577
let configResult = await loadConfig({
7678
rootDirectory: config.root ?? process.cwd(),
@@ -86,7 +88,7 @@ export const cloudflareDevProxyVitePlugin = <Env, Cf extends CfProperties>(
8688
return {
8789
ssr: {
8890
resolve: {
89-
externalConditions: [...workerdConditions, ...serverConditions],
91+
externalConditions: [...workerdConditions, ...externalConditions],
9092
},
9193
},
9294
};

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

Lines changed: 31 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -3462,10 +3462,6 @@ export async function getEnvironmentOptionsResolvers(
34623462
`file:///${path.join(packageRoot, "module-sync-enabled/index.mjs")}`
34633463
);
34643464
let vite = getVite();
3465-
let viteServerConditions: string[] = [
3466-
...(vite.defaultServerConditions ?? []),
3467-
...(moduleSyncEnabled ? ["module-sync"] : []),
3468-
];
34693465

34703466
function getBaseOptions({
34713467
viteUserConfig,
@@ -3521,10 +3517,35 @@ export async function getEnvironmentOptionsResolvers(
35213517
}: {
35223518
viteUserConfig: Vite.UserConfig;
35233519
}): EnvironmentOptions {
3524-
let conditions =
3525-
viteCommand === "build"
3526-
? viteServerConditions
3527-
: ["development", ...viteServerConditions];
3520+
// We're using the module-sync condition, but Vite
3521+
// doesn't support it by default.
3522+
// See https://github.com/vitest-dev/vitest/issues/7692
3523+
let maybeModuleSyncConditions: string[] = [
3524+
...(moduleSyncEnabled ? ["module-sync"] : []),
3525+
];
3526+
3527+
let maybeDevelopmentConditions =
3528+
viteCommand === "build" ? [] : ["development"];
3529+
3530+
// This is a compatibility layer for Vite 5. Default conditions were
3531+
// automatically added to any custom conditions in Vite 5, but Vite 6
3532+
// removed this behavior. Instead, the default conditions are overridden
3533+
// by any custom conditions. If we wish to retain the default
3534+
// conditions, we need to manually merge them using the provided default
3535+
// conditions arrays exported from Vite. In Vite 5, these default
3536+
// conditions arrays do not exist.
3537+
// https://vite.dev/guide/migration.html#default-value-for-resolve-conditions
3538+
let maybeDefaultServerConditions = vite.defaultServerConditions || [];
3539+
3540+
// There is no helpful export with the default external conditions (see
3541+
// https://github.com/vitejs/vite/pull/20279 for more details). So, for now,
3542+
// we are hardcording the default here.
3543+
let defaultExternalConditions = ["node"];
3544+
3545+
let baseConditions = [
3546+
...maybeDevelopmentConditions,
3547+
...maybeModuleSyncConditions,
3548+
];
35283549

35293550
return mergeEnvironmentOptions(getBaseOptions({ viteUserConfig }), {
35303551
resolve: {
@@ -3533,8 +3554,8 @@ export async function getEnvironmentOptionsResolvers(
35333554
ctx.reactRouterConfig.future.unstable_viteEnvironmentApi
35343555
? undefined
35353556
: ssrExternals,
3536-
conditions,
3537-
externalConditions: conditions,
3557+
conditions: [...baseConditions, ...maybeDefaultServerConditions],
3558+
externalConditions: [...baseConditions, ...defaultExternalConditions],
35383559
},
35393560
build: {
35403561
// We move SSR-only assets to client assets. Note that the

0 commit comments

Comments
 (0)