Skip to content

Support optimizing ssr environment dependencies #13007

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 8 commits into from
Feb 17, 2025
Merged
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
5 changes: 5 additions & 0 deletions .changeset/tough-rats-sparkle.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@react-router/dev": patch
---

When `future.unstable_viteEnvironmentApi` is enabled and the `ssr` environment has `optimizeDeps.noDiscovery` disabled, define `optimizeDeps.entries` and `optimizeDeps.include`
1 change: 1 addition & 0 deletions contributors.yml
Original file line number Diff line number Diff line change
Expand Up @@ -141,6 +141,7 @@
- JaffParker
- jakkku
- JakubDrozd
- jamesopstad
- jamesrwilliams
- janpaepke
- jasikpark
Expand Down
45 changes: 44 additions & 1 deletion packages/react-router-dev/vite/plugin.ts
Original file line number Diff line number Diff line change
Expand Up @@ -122,7 +122,10 @@ export type EnvironmentName = "client" | SsrEnvironmentName;
const SSR_BUNDLE_PREFIX = "ssrBundle_";
type SsrEnvironmentName = "ssr" | `${typeof SSR_BUNDLE_PREFIX}${string}`;

type EnvironmentOptions = Pick<Vite.EnvironmentOptions, "build" | "resolve">;
type EnvironmentOptions = Pick<
Vite.EnvironmentOptions,
"build" | "resolve" | "optimizeDeps"
>;

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

viteChildCompiler = await vite.createServer({
...viteUserConfig,
// Ensure child compiler cannot overwrite the default cache directory
cacheDir: "node_modules/.vite-child-compiler",
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is to ensure that the child compiler cannot overwrite the default cache directory.

mode: viteConfig.mode,
server: {
watch: viteConfig.command === "build" ? null : undefined,
Expand Down Expand Up @@ -1193,6 +1198,26 @@ export const reactRouterVitePlugin: ReactRouterVitePlugin = () => {
plugin.name !== "react-router:route-exports" &&
plugin.name !== "react-router:hmr-updates"
),
{
name: "react-router:override-optimize-deps",
config(userConfig) {
// Prevent unnecessary dependency optimization in the child compiler
if (
ctx.reactRouterConfig.future.unstable_viteEnvironmentApi &&
userConfig.environments
) {
for (const environmentName of Object.keys(
userConfig.environments
)) {
userConfig.environments[environmentName].optimizeDeps = {
noDiscovery: true,
};
}
} else {
userConfig.optimizeDeps = { noDiscovery: true };
}
},
},
],
});
await viteChildCompiler.pluginContainer.buildStart({});
Expand Down Expand Up @@ -3177,6 +3202,24 @@ export async function getEnvironmentOptionsResolvers(
virtual.serverBuild.id,
},
},
optimizeDeps:
ctx.reactRouterConfig.future.unstable_viteEnvironmentApi &&
viteUserConfig.environments?.ssr?.optimizeDeps?.noDiscovery === false
? {
entries: [
vite.normalizePath(ctx.entryServerFilePath),
...Object.values(ctx.reactRouterConfig.routes).map((route) =>
resolveRelativeRouteFilePath(route, ctx.reactRouterConfig)
),
],
include: [
"react",
"react/jsx-dev-runtime",
"react-dom/server",
"react-router",
],
}
: undefined,
});
}

Expand Down