@@ -324,21 +324,45 @@ const getPublicModulePathForEntry = (
324324 return entryChunk ? `${ ctx . publicPath } ${ entryChunk . file } ` : undefined ;
325325} ;
326326
327+ const getCssCodeSplitDisabledFile = (
328+ ctx : ReactRouterPluginContext ,
329+ viteConfig : Vite . ResolvedConfig ,
330+ viteManifest : Vite . Manifest
331+ ) => {
332+ if ( viteConfig . build . cssCodeSplit ) {
333+ return null ;
334+ }
335+
336+ let cssFile = viteManifest [ "style.css" ] ?. file ;
337+ invariant (
338+ cssFile ,
339+ "Expected `style.css` to be present in Vite manifest when `build.cssCodeSplit` is disabled"
340+ ) ;
341+
342+ return `${ ctx . publicPath } ${ cssFile } ` ;
343+ } ;
344+
345+ const getClientEntryChunk = (
346+ ctx : ReactRouterPluginContext ,
347+ viteManifest : Vite . Manifest
348+ ) => {
349+ let filePath = ctx . entryClientFilePath ;
350+ let chunk = resolveChunk ( ctx , viteManifest , filePath ) ;
351+ invariant ( chunk , `Chunk not found: ${ filePath } ` ) ;
352+ return chunk ;
353+ } ;
354+
327355const getReactRouterManifestBuildAssets = (
328356 ctx : ReactRouterPluginContext ,
357+ viteConfig : Vite . ResolvedConfig ,
329358 viteManifest : Vite . Manifest ,
330359 entryFilePath : string ,
331- prependedAssetFilePaths : string [ ] = [ ]
360+ route : RouteManifestEntry | null
332361) : ReactRouterManifest [ "entry" ] & { css : string [ ] } => {
333362 let entryChunk = resolveChunk ( ctx , viteManifest , entryFilePath ) ;
334363 invariant ( entryChunk , `Chunk not found: ${ entryFilePath } ` ) ;
335364
336- // This is here to support prepending client entry assets to the root route
337- let prependedAssetChunks = prependedAssetFilePaths . map ( ( filePath ) => {
338- let chunk = resolveChunk ( ctx , viteManifest , filePath ) ;
339- invariant ( chunk , `Chunk not found: ${ filePath } ` ) ;
340- return chunk ;
341- } ) ;
365+ let isRootRoute = Boolean ( route && route . parentId === undefined ) ;
342366
343367 let routeModuleChunks = routeChunkNames
344368 . map ( ( routeChunkName ) =>
@@ -350,22 +374,41 @@ const getReactRouterManifestBuildAssets = (
350374 )
351375 . filter ( isNonNullable ) ;
352376
353- let chunks = resolveDependantChunks ( viteManifest , [
354- ...prependedAssetChunks ,
355- entryChunk ,
356- ...routeModuleChunks ,
357- ] ) ;
377+ let chunks = resolveDependantChunks (
378+ viteManifest ,
379+ [
380+ // If this is the root route, we also need to include assets from the
381+ // client entry file as this is a common way for consumers to import
382+ // global reset styles, etc.
383+ isRootRoute ? getClientEntryChunk ( ctx , viteManifest ) : null ,
384+ entryChunk ,
385+ routeModuleChunks ,
386+ ]
387+ . flat ( 1 )
388+ . filter ( isNonNullable )
389+ ) ;
358390
359391 return {
360392 module : `${ ctx . publicPath } ${ entryChunk . file } ` ,
361393 imports :
362394 dedupe ( chunks . flatMap ( ( e ) => e . imports ?? [ ] ) ) . map ( ( imported ) => {
363395 return `${ ctx . publicPath } ${ viteManifest [ imported ] . file } ` ;
364396 } ) ?? [ ] ,
365- css :
366- dedupe ( chunks . flatMap ( ( e ) => e . css ?? [ ] ) ) . map ( ( href ) => {
367- return `${ ctx . publicPath } ${ href } ` ;
368- } ) ?? [ ] ,
397+ css : dedupe (
398+ [
399+ // If CSS code splitting is disabled, Vite includes a singular 'style.css' asset
400+ // in the manifest that isn't tied to any route file. If we want to render these
401+ // styles correctly, we need to include them in the root route.
402+ isRootRoute
403+ ? getCssCodeSplitDisabledFile ( ctx , viteConfig , viteManifest )
404+ : null ,
405+ chunks
406+ . flatMap ( ( e ) => e . css ?? [ ] )
407+ . map ( ( href ) => `${ ctx . publicPath } ${ href } ` ) ,
408+ ]
409+ . flat ( 1 )
410+ . filter ( isNonNullable )
411+ ) ,
369412 } ;
370413} ;
371414
@@ -851,8 +894,10 @@ export const reactRouterVitePlugin: ReactRouterVitePlugin = () => {
851894 } ;
852895
853896 let generateReactRouterManifestsForBuild = async ( {
897+ viteConfig,
854898 routeIds,
855899 } : {
900+ viteConfig : Vite . ResolvedConfig ;
856901 routeIds ?: Array < string > ;
857902 } ) : Promise < {
858903 reactRouterBrowserManifest : ReactRouterManifest ;
@@ -866,8 +911,10 @@ export const reactRouterVitePlugin: ReactRouterVitePlugin = () => {
866911
867912 let entry = getReactRouterManifestBuildAssets (
868913 ctx ,
914+ viteConfig ,
869915 viteManifest ,
870- ctx . entryClientFilePath
916+ ctx . entryClientFilePath ,
917+ null
871918 ) ;
872919
873920 let browserRoutes : ReactRouterManifest [ "routes" ] = { } ;
@@ -883,7 +930,6 @@ export const reactRouterVitePlugin: ReactRouterVitePlugin = () => {
883930 for ( let route of Object . values ( ctx . reactRouterConfig . routes ) ) {
884931 let routeFile = path . join ( ctx . reactRouterConfig . appDirectory , route . file ) ;
885932 let sourceExports = routeManifestExports [ route . id ] ;
886- let isRootRoute = route . parentId === undefined ;
887933 let hasClientAction = sourceExports . includes ( "clientAction" ) ;
888934 let hasClientLoader = sourceExports . includes ( "clientLoader" ) ;
889935 let hasClientMiddleware = sourceExports . includes (
@@ -930,12 +976,10 @@ export const reactRouterVitePlugin: ReactRouterVitePlugin = () => {
930976 hasErrorBoundary : sourceExports . includes ( "ErrorBoundary" ) ,
931977 ...getReactRouterManifestBuildAssets (
932978 ctx ,
979+ viteConfig ,
933980 viteManifest ,
934981 `${ routeFile } ${ BUILD_CLIENT_ROUTE_QUERY_STRING } ` ,
935- // If this is the root route, we also need to include assets from the
936- // client entry file as this is a common way for consumers to import
937- // global reset styles, etc.
938- isRootRoute ? [ ctx . entryClientFilePath ] : [ ]
982+ route
939983 ) ,
940984 clientActionModule : hasRouteChunkByExportName . clientAction
941985 ? getPublicModulePathForEntry (
@@ -2035,10 +2079,12 @@ export const reactRouterVitePlugin: ReactRouterVitePlugin = () => {
20352079 }
20362080 case virtual . serverManifest . resolvedId : {
20372081 let routeIds = getServerBundleRouteIds ( this , ctx ) ;
2082+ invariant ( viteConfig ) ;
20382083 let reactRouterManifest =
20392084 viteCommand === "build"
20402085 ? (
20412086 await generateReactRouterManifestsForBuild ( {
2087+ viteConfig,
20422088 routeIds,
20432089 } )
20442090 ) . reactRouterServerManifest
0 commit comments