@@ -96,7 +96,9 @@ export async function resolveViteConfig({
9696 return viteConfig ;
9797}
9898
99- export async function extractPluginContext ( viteConfig : Vite . ResolvedConfig ) {
99+ export function extractPluginContext (
100+ viteConfig : Vite . ResolvedConfig | Vite . UserConfig
101+ ) {
100102 return viteConfig [ "__reactRouterPluginContext" as keyof typeof viteConfig ] as
101103 | ReactRouterPluginContext
102104 | undefined ;
@@ -163,7 +165,6 @@ type EnvironmentOptionsResolvers = Partial<
163165export type EnvironmentBuildContext = {
164166 name : EnvironmentName ;
165167 resolveOptions : EnvironmentOptionsResolver ;
166- buildManifest : BuildManifest ;
167168} ;
168169
169170function isSeverBundleEnvironmentName (
@@ -173,30 +174,28 @@ function isSeverBundleEnvironmentName(
173174}
174175
175176function getServerEnvironmentEntries < T > (
176- record : Record < string , T > ,
177- buildManifest : BuildManifest
177+ ctx : ReactRouterPluginContext ,
178+ record : Record < string , T >
178179) : [ SsrEnvironmentName , T ] [ ] {
179180 return Object . entries ( record ) . filter ( ( [ name ] ) =>
180- buildManifest . serverBundles
181+ ctx . buildManifest ? .serverBundles
181182 ? isSeverBundleEnvironmentName ( name )
182183 : name === "ssr"
183184 ) as [ SsrEnvironmentName , T ] [ ] ;
184185}
185186
186187export function getServerEnvironmentKeys (
187- record : Record < string , unknown > ,
188- buildManifest : BuildManifest
188+ ctx : ReactRouterPluginContext ,
189+ record : Record < string , unknown >
189190) : SsrEnvironmentName [ ] {
190- return getServerEnvironmentEntries ( record , buildManifest ) . map ( ( [ key ] ) => key ) ;
191+ return getServerEnvironmentEntries ( ctx , record ) . map ( ( [ key ] ) => key ) ;
191192}
192193
193194export function getServerEnvironmentValues < T > (
194- record : Record < string , T > ,
195- buildManifest : BuildManifest
195+ ctx : ReactRouterPluginContext ,
196+ record : Record < string , T >
196197) : T [ ] {
197- return getServerEnvironmentEntries ( record , buildManifest ) . map (
198- ( [ , value ] ) => value
199- ) ;
198+ return getServerEnvironmentEntries ( ctx , record ) . map ( ( [ , value ] ) => value ) ;
200199}
201200
202201const isRouteEntryModuleId = ( id : string ) : boolean => {
@@ -237,18 +236,17 @@ export type ServerBundleBuildConfig = {
237236type ResolvedEnvironmentBuildContext = {
238237 name : EnvironmentName ;
239238 options : EnvironmentOptions ;
240- buildManifest : BuildManifest ;
241239} ;
242240
243241type ReactRouterPluginContext = {
244242 environmentBuildContext : ResolvedEnvironmentBuildContext | null ;
243+ buildManifest : BuildManifest | null ;
245244 rootDirectory : string ;
246245 entryClientFilePath : string ;
247246 entryServerFilePath : string ;
248247 publicPath : string ;
249248 reactRouterConfig : ResolvedReactRouterConfig ;
250249 viteManifestEnabled : boolean ;
251- buildManifest : BuildManifest ;
252250} ;
253251
254252let virtualHmrRuntime = VirtualModule . create ( "hmr-runtime" ) ;
@@ -504,7 +502,6 @@ const resolveEnvironmentBuildContext = ({
504502 let resolvedBuildContext : ResolvedEnvironmentBuildContext = {
505503 name : buildContext . name ,
506504 options : buildContext . resolveOptions ( { viteUserConfig } ) ,
507- buildManifest : buildContext . buildManifest ,
508505 } ;
509506
510507 return resolvedBuildContext ;
@@ -525,24 +522,22 @@ let getClientBuildDirectory = (reactRouterConfig: ResolvedReactRouterConfig) =>
525522
526523let getServerBundleRouteIds = (
527524 vitePluginContext : Vite . Rollup . PluginContext ,
528- {
529- ctx,
530- buildManifest,
531- } : {
532- ctx : ReactRouterPluginContext ;
533- buildManifest : BuildManifest ;
534- }
525+ ctx : ReactRouterPluginContext
535526) : string [ ] | undefined => {
536527 let environmentName = ctx . reactRouterConfig . future . unstable_viteEnvironmentApi
537528 ? vitePluginContext . environment . name
538529 : ctx . environmentBuildContext ?. name ;
539530
540- if ( ! environmentName || ! isSsrBundleEnvironmentName ( environmentName ) ) {
531+ if (
532+ ! ctx . buildManifest ||
533+ ! environmentName ||
534+ ! isSsrBundleEnvironmentName ( environmentName )
535+ ) {
541536 return undefined ;
542537 }
543538
544539 let serverBundleId = environmentName . replace ( SSR_BUNDLE_PREFIX , "" ) ;
545- let routesByServerBundleId = getRoutesByServerBundleId ( buildManifest ) ;
540+ let routesByServerBundleId = getRoutesByServerBundleId ( ctx . buildManifest ) ;
546541 let serverBundleRoutes = routesByServerBundleId [ serverBundleId ] ;
547542
548543 invariant (
@@ -589,7 +584,6 @@ export const reactRouterVitePlugin: ReactRouterVitePlugin = () => {
589584 let viteUserConfig : Vite . UserConfig ;
590585 let viteConfigEnv : Vite . ConfigEnv ;
591586 let viteConfig : Vite . ResolvedConfig | undefined ;
592- let buildManifest : BuildManifest | undefined ;
593587 let cssModulesManifest : Record < string , string > = { } ;
594588 let viteChildCompiler : Vite . ViteDevServer | null = null ;
595589 let cache : Cache = new Map ( ) ;
@@ -607,6 +601,20 @@ export const reactRouterVitePlugin: ReactRouterVitePlugin = () => {
607601
608602 /** Mutates `ctx` as a side-effect */
609603 let updatePluginContext = async ( ) : Promise < void > => {
604+ // This `injectedPluginContext` logic is so we can support injecting an
605+ // already-resolved plugin context into the build. This is so we can resolve
606+ // the plugin context once for the entire build process and avoid resolving
607+ // it every time the Vite config is resolved. This is most important for
608+ // resolving the `buildManifest` object since it involves executing the
609+ // `serverBundles` function, which we want to avoid doing multiple times.
610+ let injectedPluginContext =
611+ viteCommand === "build"
612+ ? extractPluginContext ( viteUserConfig )
613+ : undefined ;
614+ if ( injectedPluginContext ) {
615+ ctx = injectedPluginContext ;
616+ }
617+
610618 let reactRouterConfig : ResolvedReactRouterConfig ;
611619 let reactRouterConfigResult = await reactRouterConfigLoader . getConfig ( ) ;
612620
@@ -645,16 +653,16 @@ export const reactRouterVitePlugin: ReactRouterVitePlugin = () => {
645653
646654 let viteManifestEnabled = viteUserConfig . build ?. manifest === true ;
647655
656+ let buildManifest =
657+ viteCommand === "build"
658+ ? await getBuildManifest ( { reactRouterConfig, rootDirectory } )
659+ : null ;
660+
648661 let environmentBuildContext : ResolvedEnvironmentBuildContext | null =
649662 viteCommand === "build"
650663 ? resolveEnvironmentBuildContext ( { viteCommand, viteUserConfig } )
651664 : null ;
652665
653- buildManifest =
654- ctx ?. buildManifest ??
655- ctx ?. environmentBuildContext ?. buildManifest ??
656- ( await getBuildManifest ( { reactRouterConfig, rootDirectory } ) ) ;
657-
658666 firstLoad = false ;
659667
660668 ctx = {
@@ -1141,8 +1149,8 @@ export const reactRouterVitePlugin: ReactRouterVitePlugin = () => {
11411149 } ) ;
11421150
11431151 let serverEnvironment = getServerEnvironmentValues (
1144- environments ,
1145- ctx . buildManifest
1152+ ctx ,
1153+ environments
11461154 ) [ 0 ] ;
11471155 invariant ( serverEnvironment ) ;
11481156
@@ -1235,8 +1243,6 @@ export const reactRouterVitePlugin: ReactRouterVitePlugin = () => {
12351243 sharedPlugins : true ,
12361244 async buildApp ( builder ) {
12371245 invariant ( viteConfig ) ;
1238- invariant ( buildManifest ) ;
1239-
12401246 viteConfig . logger . info (
12411247 "Using Vite Environment API (experimental)"
12421248 ) ;
@@ -1248,14 +1254,17 @@ export const reactRouterVitePlugin: ReactRouterVitePlugin = () => {
12481254 await builder . build ( builder . environments . client ) ;
12491255
12501256 let serverEnvironments = getServerEnvironmentValues (
1251- builder . environments ,
1252- buildManifest
1257+ ctx ,
1258+ builder . environments
12531259 ) ;
12541260
12551261 await Promise . all ( serverEnvironments . map ( builder . build ) ) ;
12561262
12571263 await cleanViteManifests ( environments , ctx ) ;
12581264
1265+ let { buildManifest } = ctx ;
1266+ invariant ( buildManifest , "Expected build manifest" ) ;
1267+
12591268 await reactRouterConfig . buildEnd ?.( {
12601269 buildManifest,
12611270 reactRouterConfig,
@@ -1838,19 +1847,11 @@ export const reactRouterVitePlugin: ReactRouterVitePlugin = () => {
18381847 async load ( id ) {
18391848 switch ( id ) {
18401849 case virtual . serverBuild . resolvedId : {
1841- invariant ( buildManifest ) ;
1842- let routeIds = getServerBundleRouteIds ( this , {
1843- ctx,
1844- buildManifest,
1845- } ) ;
1850+ let routeIds = getServerBundleRouteIds ( this , ctx ) ;
18461851 return await getServerEntry ( { routeIds } ) ;
18471852 }
18481853 case virtual . serverManifest . resolvedId : {
1849- invariant ( buildManifest ) ;
1850- let routeIds = getServerBundleRouteIds ( this , {
1851- ctx,
1852- buildManifest,
1853- } ) ;
1854+ let routeIds = getServerBundleRouteIds ( this , ctx ) ;
18541855 let reactRouterManifest =
18551856 viteCommand === "build"
18561857 ? (
@@ -2957,9 +2958,9 @@ function getRouteBranch(routes: RouteManifest, routeId: string) {
29572958 return branch . reverse ( ) ;
29582959}
29592960
2960- function getServerBundleIds ( buildManifest : BuildManifest ) {
2961- return buildManifest . serverBundles
2962- ? Object . keys ( buildManifest . serverBundles )
2961+ function getServerBundleIds ( ctx : ReactRouterPluginContext ) {
2962+ return ctx . buildManifest ? .serverBundles
2963+ ? Object . keys ( ctx . buildManifest . serverBundles )
29632964 : undefined ;
29642965}
29652966
@@ -3405,7 +3406,7 @@ export async function getEnvironmentOptionsResolvers(
34053406 } ) ,
34063407 } ;
34073408
3408- let serverBundleIds = getServerBundleIds ( ctx . buildManifest ) ;
3409+ let serverBundleIds = getServerBundleIds ( ctx ) ;
34093410 if ( serverBundleIds ) {
34103411 for ( let serverBundleId of serverBundleIds ) {
34113412 const environmentName = `${ SSR_BUNDLE_PREFIX } ${ serverBundleId } ` as const ;
0 commit comments