@@ -289,6 +289,41 @@ const cssModulesCache = new WeakMap<
289289 Map < string , Record < string , string > >
290290> ( )
291291
292+ const renderedModulesCache = new WeakMap <
293+ Record < string , RenderedChunk > ,
294+ {
295+ modules : Map < string , RenderedModule >
296+ chunks : ArrayIterator < string >
297+ }
298+ > ( )
299+
300+ function getRenderedModule (
301+ chunks : Record < string , RenderedChunk > ,
302+ importerId : string ,
303+ ) : RenderedModule | undefined {
304+ let cache = renderedModulesCache . get ( chunks )
305+ if ( ! cache ) {
306+ cache = {
307+ modules : new Map ( ) ,
308+ chunks : Object . keys ( chunks ) . values ( ) ,
309+ }
310+ renderedModulesCache . set ( chunks , cache )
311+ }
312+
313+ const cached = cache . modules . get ( importerId )
314+ if ( cached ) return cached
315+
316+ // Resume indexing at the next chunk, stopping once the importer is found.
317+ for ( const name of cache . chunks ) {
318+ const modules = chunks [ name ] . modules
319+ for ( const id in modules ) {
320+ if ( ! cache . modules . has ( id ) ) cache . modules . set ( id , modules [ id ] )
321+ }
322+ const importer = cache . modules . get ( importerId )
323+ if ( importer ) return importer
324+ }
325+ }
326+
292327export const removedPureCssFilesCache : WeakMap <
293328 ResolvedConfig ,
294329 Map < string , RenderedChunk >
@@ -675,20 +710,6 @@ export function cssPostPlugin(config: ResolvedConfig): Plugin {
675710 ? {
676711 async renderChunk ( code , chunk , opts , meta ) {
677712 let chunkCSS : string | undefined
678- const renderedModules = new Proxy (
679- { } as Record < string , RenderedModule | undefined > ,
680- {
681- get ( _target , p ) {
682- for ( const name in meta . chunks ) {
683- const modules = meta . chunks [ name ] . modules
684- const module = modules [ p as string ]
685- if ( module ) {
686- return module
687- }
688- }
689- } ,
690- } ,
691- )
692713 // the chunk is empty if it's a dynamic entry chunk that only contains a CSS import
693714 const isJsChunkEmpty = code === '' && ! chunk . isEntry
694715 let isPureCssChunk = chunk . exports . length === 0
@@ -706,7 +727,7 @@ export function cssPostPlugin(config: ResolvedConfig): Plugin {
706727 this . getModuleInfo ( id ) ?. meta ?. vite ?. cssScopeTo
707728 if (
708729 cssScopeTo &&
709- ! isCssScopeToRendered ( cssScopeTo , renderedModules )
730+ ! isCssScopeToRendered ( cssScopeTo , meta . chunks )
710731 ) {
711732 continue
712733 }
@@ -1308,10 +1329,10 @@ export function cssAnalysisPlugin(config: ResolvedConfig): Plugin {
13081329
13091330function isCssScopeToRendered (
13101331 cssScopeTo : Exclude < CustomPluginOptionsVite [ 'cssScopeTo' ] , undefined > ,
1311- renderedModules : Record < string , RenderedModule | undefined > ,
1332+ chunks : Record < string , RenderedChunk > ,
13121333) {
13131334 const [ importerId , exp ] = cssScopeTo
1314- const importer = renderedModules [ importerId ]
1335+ const importer = getRenderedModule ( chunks , importerId )
13151336 return (
13161337 importer && ( exp === undefined || importer . renderedExports . includes ( exp ) )
13171338 )
0 commit comments