Skip to content

Commit f28c1d1

Browse files
authored
Support optimizing ssr environment dependencies (#13007)
1 parent 54d45d4 commit f28c1d1

File tree

3 files changed

+50
-1
lines changed

3 files changed

+50
-1
lines changed

.changeset/tough-rats-sparkle.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+
When `future.unstable_viteEnvironmentApi` is enabled and the `ssr` environment has `optimizeDeps.noDiscovery` disabled, define `optimizeDeps.entries` and `optimizeDeps.include`

contributors.yml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -141,6 +141,7 @@
141141
- JaffParker
142142
- jakkku
143143
- JakubDrozd
144+
- jamesopstad
144145
- jamesrwilliams
145146
- janpaepke
146147
- jasikpark

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

Lines changed: 44 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -122,7 +122,10 @@ export type EnvironmentName = "client" | SsrEnvironmentName;
122122
const SSR_BUNDLE_PREFIX = "ssrBundle_";
123123
type SsrEnvironmentName = "ssr" | `${typeof SSR_BUNDLE_PREFIX}${string}`;
124124

125-
type EnvironmentOptions = Pick<Vite.EnvironmentOptions, "build" | "resolve">;
125+
type EnvironmentOptions = Pick<
126+
Vite.EnvironmentOptions,
127+
"build" | "resolve" | "optimizeDeps"
128+
>;
126129

127130
type EnvironmentOptionsResolver = (options: {
128131
viteUserConfig: Vite.UserConfig;
@@ -1166,6 +1169,8 @@ export const reactRouterVitePlugin: ReactRouterVitePlugin = () => {
11661169

11671170
viteChildCompiler = await vite.createServer({
11681171
...viteUserConfig,
1172+
// Ensure child compiler cannot overwrite the default cache directory
1173+
cacheDir: "node_modules/.vite-child-compiler",
11691174
mode: viteConfig.mode,
11701175
server: {
11711176
watch: viteConfig.command === "build" ? null : undefined,
@@ -1193,6 +1198,26 @@ export const reactRouterVitePlugin: ReactRouterVitePlugin = () => {
11931198
plugin.name !== "react-router:route-exports" &&
11941199
plugin.name !== "react-router:hmr-updates"
11951200
),
1201+
{
1202+
name: "react-router:override-optimize-deps",
1203+
config(userConfig) {
1204+
// Prevent unnecessary dependency optimization in the child compiler
1205+
if (
1206+
ctx.reactRouterConfig.future.unstable_viteEnvironmentApi &&
1207+
userConfig.environments
1208+
) {
1209+
for (const environmentName of Object.keys(
1210+
userConfig.environments
1211+
)) {
1212+
userConfig.environments[environmentName].optimizeDeps = {
1213+
noDiscovery: true,
1214+
};
1215+
}
1216+
} else {
1217+
userConfig.optimizeDeps = { noDiscovery: true };
1218+
}
1219+
},
1220+
},
11961221
],
11971222
});
11981223
await viteChildCompiler.pluginContainer.buildStart({});
@@ -3177,6 +3202,24 @@ export async function getEnvironmentOptionsResolvers(
31773202
virtual.serverBuild.id,
31783203
},
31793204
},
3205+
optimizeDeps:
3206+
ctx.reactRouterConfig.future.unstable_viteEnvironmentApi &&
3207+
viteUserConfig.environments?.ssr?.optimizeDeps?.noDiscovery === false
3208+
? {
3209+
entries: [
3210+
vite.normalizePath(ctx.entryServerFilePath),
3211+
...Object.values(ctx.reactRouterConfig.routes).map((route) =>
3212+
resolveRelativeRouteFilePath(route, ctx.reactRouterConfig)
3213+
),
3214+
],
3215+
include: [
3216+
"react",
3217+
"react/jsx-dev-runtime",
3218+
"react-dom/server",
3219+
"react-router",
3220+
],
3221+
}
3222+
: undefined,
31803223
});
31813224
}
31823225

0 commit comments

Comments
 (0)