@@ -324,6 +324,34 @@ 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 ,
329357 viteConfig : Vite . ResolvedConfig ,
@@ -336,30 +364,6 @@ const getReactRouterManifestBuildAssets = (
336364
337365 let isRootRoute = Boolean ( route && route . parentId === undefined ) ;
338366
339- // If this is the root route, we also need to include assets from the
340- // client entry file as this is a common way for consumers to import
341- // global reset styles, etc.
342- let prependedAssetChunks = isRootRoute
343- ? [ ctx . entryClientFilePath ] . map ( ( filePath ) => {
344- let chunk = resolveChunk ( ctx , viteManifest , filePath ) ;
345- invariant ( chunk , `Chunk not found: ${ filePath } ` ) ;
346- return chunk ;
347- } )
348- : [ ] ;
349-
350- let cssCodeSplitDisabledFiles : string [ ] = [ ] ;
351- // If CSS code splitting is disabled, Vite includes a singular 'style.css' asset
352- // in the manifest that isn't tied to any route file. If we want to render these
353- // styles correctly, we need to include them in the root route.
354- if ( ! viteConfig . build . cssCodeSplit && isRootRoute ) {
355- let cssFile = viteManifest [ "style.css" ] ?. file ;
356- invariant (
357- cssFile ,
358- "Expected `style.css` to be present in Vite manifest when `build.cssCodeSplit` is disabled"
359- ) ;
360- cssCodeSplitDisabledFiles = [ `${ ctx . publicPath } ${ cssFile } ` ] ;
361- }
362-
363367 let routeModuleChunks = routeChunkNames
364368 . map ( ( routeChunkName ) =>
365369 resolveChunk (
@@ -370,26 +374,41 @@ const getReactRouterManifestBuildAssets = (
370374 )
371375 . filter ( isNonNullable ) ;
372376
373- let chunks = resolveDependantChunks ( viteManifest , [
374- ...prependedAssetChunks ,
375- entryChunk ,
376- ...routeModuleChunks ,
377- ] ) ;
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+ ) ;
378390
379391 return {
380392 module : `${ ctx . publicPath } ${ entryChunk . file } ` ,
381393 imports :
382394 dedupe ( chunks . flatMap ( ( e ) => e . imports ?? [ ] ) ) . map ( ( imported ) => {
383395 return `${ ctx . publicPath } ${ viteManifest [ imported ] . file } ` ;
384396 } ) ?? [ ] ,
385- css : dedupe ( [
386- ...cssCodeSplitDisabledFiles ,
387- ...( chunks
388- . flatMap ( ( e ) => e . css ?? [ ] )
389- . map ( ( href ) => {
390- return `${ ctx . publicPath } ${ href } ` ;
391- } ) ?? [ ] ) ,
392- ] ) ,
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+ ) ,
393412 } ;
394413} ;
395414
0 commit comments