@@ -122,7 +122,10 @@ export type EnvironmentName = "client" | SsrEnvironmentName;
122
122
const SSR_BUNDLE_PREFIX = "ssrBundle_" ;
123
123
type SsrEnvironmentName = "ssr" | `${typeof SSR_BUNDLE_PREFIX } ${string } `;
124
124
125
- type EnvironmentOptions = Pick < Vite . EnvironmentOptions , "build" | "resolve" > ;
125
+ type EnvironmentOptions = Pick <
126
+ Vite . EnvironmentOptions ,
127
+ "build" | "resolve" | "optimizeDeps"
128
+ > ;
126
129
127
130
type EnvironmentOptionsResolver = ( options : {
128
131
viteUserConfig : Vite . UserConfig ;
@@ -1166,6 +1169,8 @@ export const reactRouterVitePlugin: ReactRouterVitePlugin = () => {
1166
1169
1167
1170
viteChildCompiler = await vite . createServer ( {
1168
1171
...viteUserConfig ,
1172
+ // Ensure child compiler cannot overwrite the default cache directory
1173
+ cacheDir : "node_modules/.vite-child-compiler" ,
1169
1174
mode : viteConfig . mode ,
1170
1175
server : {
1171
1176
watch : viteConfig . command === "build" ? null : undefined ,
@@ -1193,6 +1198,26 @@ export const reactRouterVitePlugin: ReactRouterVitePlugin = () => {
1193
1198
plugin . name !== "react-router:route-exports" &&
1194
1199
plugin . name !== "react-router:hmr-updates"
1195
1200
) ,
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
+ } ,
1196
1221
] ,
1197
1222
} ) ;
1198
1223
await viteChildCompiler . pluginContainer . buildStart ( { } ) ;
@@ -3177,6 +3202,24 @@ export async function getEnvironmentOptionsResolvers(
3177
3202
virtual . serverBuild . id ,
3178
3203
} ,
3179
3204
} ,
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 ,
3180
3223
} ) ;
3181
3224
}
3182
3225
0 commit comments