@@ -64,20 +64,7 @@ function rollupPluginAstroBuildCSS(options: PluginOptions): VitePlugin[] {
6464 const cssBuildPlugin : VitePlugin = {
6565 name : 'astro:rollup-plugin-build-css' ,
6666
67- transform ( _ , id ) {
68- // In the SSR build, styles that are bundled are tracked in `internals.cssChunkModuleIds`.
69- // In the client build, if we're also bundling the same style, return an empty string to
70- // deduplicate the final CSS output.
71- if ( options . target === 'client' && internals . cssChunkModuleIds . has ( id ) ) {
72- return '' ;
73- }
74- } ,
75-
7667 outputOptions ( outputOptions ) {
77- // Skip in client builds as its module graph doesn't have reference to Astro pages
78- // to be able to chunk based on it's related top-level pages.
79- if ( options . target === 'client' ) return ;
80-
8168 const assetFileNames = outputOptions . assetFileNames ;
8269 const namingIncludesHash = assetFileNames ?. toString ( ) . includes ( '[hash]' ) ;
8370 const createNameForParentPages = namingIncludesHash
@@ -89,16 +76,31 @@ function rollupPluginAstroBuildCSS(options: PluginOptions): VitePlugin[] {
8976 // For CSS, create a hash of all of the pages that use it.
9077 // This causes CSS to be built into shared chunks when used by multiple pages.
9178 if ( isBuildableCSSRequest ( id ) ) {
79+ // For client builds that has hydrated components as entrypoints, there's no way
80+ // to crawl up and find the pages that use it. So we lookup the cache during SSR
81+ // build (that has the pages information) to derive the same chunk id so they
82+ // match up on build, making sure both builds has the CSS deduped.
83+ // NOTE: Components that are only used with `client:only` may not exist in the cache
84+ // and that's okay. We can use Rollup's default chunk strategy instead as these CSS
85+ // are outside of the SSR build scope, which no dedupe is needed.
86+ if ( options . target === 'client' ) {
87+ return internals . cssModuleToChunkIdMap . get ( id ) ! ;
88+ }
89+
9290 for ( const [ pageInfo ] of walkParentInfos ( id , {
9391 getModuleInfo : meta . getModuleInfo ,
9492 } ) ) {
9593 if ( new URL ( pageInfo . id , 'file://' ) . searchParams . has ( PROPAGATED_ASSET_FLAG ) ) {
9694 // Split delayed assets to separate modules
9795 // so they can be injected where needed
98- return createNameHash ( id , [ id ] ) ;
96+ const chunkId = createNameHash ( id , [ id ] ) ;
97+ internals . cssModuleToChunkIdMap . set ( id , chunkId ) ;
98+ return chunkId ;
9999 }
100100 }
101- return createNameForParentPages ( id , meta ) ;
101+ const chunkId = createNameForParentPages ( id , meta ) ;
102+ internals . cssModuleToChunkIdMap . set ( id , chunkId ) ;
103+ return chunkId ;
102104 }
103105 } ,
104106 } ) ;
@@ -113,15 +115,6 @@ function rollupPluginAstroBuildCSS(options: PluginOptions): VitePlugin[] {
113115 // Skip if the chunk has no CSS, we want to handle CSS chunks only
114116 if ( meta . importedCss . size < 1 ) continue ;
115117
116- // In the SSR build, keep track of all CSS chunks' modules as the client build may
117- // duplicate them, e.g. for `client:load` components that render in SSR and client
118- // for hydation.
119- if ( options . target === 'server' ) {
120- for ( const id of Object . keys ( chunk . modules ) ) {
121- internals . cssChunkModuleIds . add ( id ) ;
122- }
123- }
124-
125118 // For the client build, client:only styles need to be mapped
126119 // over to their page. For this chunk, determine if it's a child of a
127120 // client:only component and if so, add its CSS to the page it belongs to.
0 commit comments