@@ -160,10 +160,7 @@ const CSS_DEV_HELPER_ENVIRONMENT_NAME =
160
160
"__react_router_css_dev_helper__" as const ;
161
161
type CssDevHelperEnvironmentName = typeof CSS_DEV_HELPER_ENVIRONMENT_NAME ;
162
162
163
- type EnvironmentOptions = Pick <
164
- Vite . EnvironmentOptions ,
165
- "build" | "resolve" | "optimizeDeps"
166
- > ;
163
+ type EnvironmentOptions = Pick < Vite . EnvironmentOptions , "build" | "resolve" > ;
167
164
168
165
type EnvironmentOptionsResolver = ( options : {
169
166
viteUserConfig : Vite . UserConfig ;
@@ -178,19 +175,13 @@ export type EnvironmentBuildContext = {
178
175
resolveOptions : EnvironmentOptionsResolver ;
179
176
} ;
180
177
181
- function isSeverBundleEnvironmentName (
182
- name : string
183
- ) : name is SsrEnvironmentName {
184
- return name . startsWith ( SSR_BUNDLE_PREFIX ) ;
185
- }
186
-
187
178
function getServerEnvironmentEntries < T > (
188
179
ctx : ReactRouterPluginContext ,
189
180
record : Record < string , T >
190
181
) : [ SsrEnvironmentName , T ] [ ] {
191
182
return Object . entries ( record ) . filter ( ( [ name ] ) =>
192
183
ctx . buildManifest ?. serverBundles
193
- ? isSeverBundleEnvironmentName ( name )
184
+ ? isSsrBundleEnvironmentName ( name )
194
185
: name === "ssr"
195
186
) as [ SsrEnvironmentName , T ] [ ] ;
196
187
}
@@ -1295,6 +1286,50 @@ export const reactRouterVitePlugin: ReactRouterVitePlugin = () => {
1295
1286
} ) ,
1296
1287
} ;
1297
1288
} ,
1289
+ configEnvironment ( name , options ) {
1290
+ if (
1291
+ ctx . reactRouterConfig . future . unstable_viteEnvironmentApi &&
1292
+ ( ctx . buildManifest ?. serverBundles
1293
+ ? isSsrBundleEnvironmentName ( name )
1294
+ : name === "ssr" )
1295
+ ) {
1296
+ const vite = getVite ( ) ;
1297
+
1298
+ return {
1299
+ resolve : {
1300
+ external :
1301
+ // This check is required to honor the "noExternal: true" config
1302
+ // provided by vite-plugin-cloudflare within this repo. When compiling
1303
+ // for Cloudflare, all server dependencies are pre-bundled, but our
1304
+ // `ssrExternals` config inadvertently overrides this. This doesn't
1305
+ // impact consumers because for them `ssrExternals` is undefined and
1306
+ // Cloudflare's "noExternal: true" config remains intact.
1307
+ options . resolve ?. noExternal === true ? undefined : ssrExternals ,
1308
+ } ,
1309
+ optimizeDeps :
1310
+ options . optimizeDeps ?. noDiscovery === false
1311
+ ? {
1312
+ entries : [
1313
+ vite . normalizePath ( ctx . entryServerFilePath ) ,
1314
+ ...Object . values ( ctx . reactRouterConfig . routes ) . map (
1315
+ ( route ) =>
1316
+ resolveRelativeRouteFilePath (
1317
+ route ,
1318
+ ctx . reactRouterConfig
1319
+ )
1320
+ ) ,
1321
+ ] ,
1322
+ include : [
1323
+ "react" ,
1324
+ "react/jsx-dev-runtime" ,
1325
+ "react-dom/server" ,
1326
+ "react-router" ,
1327
+ ] ,
1328
+ }
1329
+ : undefined ,
1330
+ } ;
1331
+ }
1332
+ } ,
1298
1333
async configResolved ( resolvedViteConfig ) {
1299
1334
await initEsModuleLexer ;
1300
1335
@@ -1531,6 +1566,7 @@ export const reactRouterVitePlugin: ReactRouterVitePlugin = () => {
1531
1566
let vite = getVite ( ) ;
1532
1567
let ssrEnvironment = viteDevServer . environments . ssr ;
1533
1568
if ( ! vite . isRunnableDevEnvironment ( ssrEnvironment ) ) {
1569
+ next ( ) ;
1534
1570
return ;
1535
1571
}
1536
1572
build = ( await ssrEnvironment . runner . import (
@@ -3315,14 +3351,8 @@ export async function getEnvironmentOptionsResolvers(
3315
3351
return mergeEnvironmentOptions ( getBaseOptions ( { viteUserConfig } ) , {
3316
3352
resolve : {
3317
3353
external :
3318
- // This check is required to honor the "noExternal: true" config
3319
- // provided by vite-plugin-cloudflare within this repo. When compiling
3320
- // for Cloudflare, all server dependencies are externalized, but our
3321
- // `ssrExternals` config inadvertently overrides this. This doesn't
3322
- // impact consumers because for them `ssrExternals` is undefined and
3323
- // Cloudflare's "noExternal: true" config remains intact.
3324
- ctx . reactRouterConfig . future . unstable_viteEnvironmentApi &&
3325
- viteUserConfig . environments ?. ssr ?. resolve ?. noExternal === true
3354
+ // If `unstable_viteEnvironmentApi` is `true`, `resolve.external` is set in the `configEnvironment` hook
3355
+ ctx . reactRouterConfig . future . unstable_viteEnvironmentApi
3326
3356
? undefined
3327
3357
: ssrExternals ,
3328
3358
conditions,
@@ -3435,24 +3465,6 @@ export async function getEnvironmentOptionsResolvers(
3435
3465
build : {
3436
3466
outDir : getServerBuildDirectory ( ctx . reactRouterConfig ) ,
3437
3467
} ,
3438
- optimizeDeps :
3439
- ctx . reactRouterConfig . future . unstable_viteEnvironmentApi &&
3440
- viteUserConfig . environments ?. ssr ?. optimizeDeps ?. noDiscovery === false
3441
- ? {
3442
- entries : [
3443
- vite . normalizePath ( ctx . entryServerFilePath ) ,
3444
- ...Object . values ( ctx . reactRouterConfig . routes ) . map ( ( route ) =>
3445
- resolveRelativeRouteFilePath ( route , ctx . reactRouterConfig )
3446
- ) ,
3447
- ] ,
3448
- include : [
3449
- "react" ,
3450
- "react/jsx-dev-runtime" ,
3451
- "react-dom/server" ,
3452
- "react-router" ,
3453
- ] ,
3454
- }
3455
- : undefined ,
3456
3468
} ) ;
3457
3469
}
3458
3470
0 commit comments