@@ -160,10 +160,7 @@ const CSS_DEV_HELPER_ENVIRONMENT_NAME =
160160 "__react_router_css_dev_helper__" as const ;
161161type CssDevHelperEnvironmentName = typeof CSS_DEV_HELPER_ENVIRONMENT_NAME ;
162162
163- type EnvironmentOptions = Pick <
164- Vite . EnvironmentOptions ,
165- "build" | "resolve" | "optimizeDeps"
166- > ;
163+ type EnvironmentOptions = Pick < Vite . EnvironmentOptions , "build" | "resolve" > ;
167164
168165type EnvironmentOptionsResolver = ( options : {
169166 viteUserConfig : Vite . UserConfig ;
@@ -178,19 +175,13 @@ export type EnvironmentBuildContext = {
178175 resolveOptions : EnvironmentOptionsResolver ;
179176} ;
180177
181- function isSeverBundleEnvironmentName (
182- name : string
183- ) : name is SsrEnvironmentName {
184- return name . startsWith ( SSR_BUNDLE_PREFIX ) ;
185- }
186-
187178function getServerEnvironmentEntries < T > (
188179 ctx : ReactRouterPluginContext ,
189180 record : Record < string , T >
190181) : [ SsrEnvironmentName , T ] [ ] {
191182 return Object . entries ( record ) . filter ( ( [ name ] ) =>
192183 ctx . buildManifest ?. serverBundles
193- ? isSeverBundleEnvironmentName ( name )
184+ ? isSsrBundleEnvironmentName ( name )
194185 : name === "ssr"
195186 ) as [ SsrEnvironmentName , T ] [ ] ;
196187}
@@ -1295,6 +1286,50 @@ export const reactRouterVitePlugin: ReactRouterVitePlugin = () => {
12951286 } ) ,
12961287 } ;
12971288 } ,
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+ } ,
12981333 async configResolved ( resolvedViteConfig ) {
12991334 await initEsModuleLexer ;
13001335
@@ -1531,6 +1566,7 @@ export const reactRouterVitePlugin: ReactRouterVitePlugin = () => {
15311566 let vite = getVite ( ) ;
15321567 let ssrEnvironment = viteDevServer . environments . ssr ;
15331568 if ( ! vite . isRunnableDevEnvironment ( ssrEnvironment ) ) {
1569+ next ( ) ;
15341570 return ;
15351571 }
15361572 build = ( await ssrEnvironment . runner . import (
@@ -3315,14 +3351,8 @@ export async function getEnvironmentOptionsResolvers(
33153351 return mergeEnvironmentOptions ( getBaseOptions ( { viteUserConfig } ) , {
33163352 resolve : {
33173353 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
33263356 ? undefined
33273357 : ssrExternals ,
33283358 conditions,
@@ -3435,24 +3465,6 @@ export async function getEnvironmentOptionsResolvers(
34353465 build : {
34363466 outDir : getServerBuildDirectory ( ctx . reactRouterConfig ) ,
34373467 } ,
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 ,
34563468 } ) ;
34573469 }
34583470
0 commit comments