@@ -36,10 +36,13 @@ const getModuleSourceMapCache = getLazy(() => {
3636 return new SourceMapCacheMap ( ) ;
3737} ) ;
3838
39- // The generated source module/script instance is not accessible, so we can use
40- // a Map without memory concerns. Separate generated source entries with the module
41- // source entries to avoid overriding the module source entries with arbitrary
42- // source url magic comments.
39+ // The generated source module/script instance is not accessible, so these entries
40+ // cannot be keyed weakly like the module source entries are. The cache is bounded
41+ // instead, or generated sources evaluated under a changing source url would
42+ // retain every payload they ever mapped.
43+ // Separate generated source entries with the module source entries to avoid
44+ // overriding the module source entries with arbitrary source url magic comments.
45+ const kGeneratedSourceMapCacheLimit = 256 ;
4346const generatedSourceMapCache = new SafeMap ( ) ;
4447const kLeadingProtocol = / ^ \w + : \/ \/ / ;
4548const kSourceMappingURLMagicComment = / \/ [ * / ] # \s + s o u r c e M a p p i n g U R L = (?< sourceMappingURL > [ ^ \s ] + ) / g;
@@ -189,7 +192,12 @@ function maybeCacheSourceMap(filename, content, moduleInstance, isGeneratedSourc
189192 } ;
190193
191194 if ( isGeneratedSource ) {
195+ // Delete first so that re-evaluating a source moves it back to the newest end.
196+ generatedSourceMapCache . delete ( filename ) ;
192197 generatedSourceMapCache . set ( filename , entry ) ;
198+ if ( generatedSourceMapCache . size > kGeneratedSourceMapCacheLimit ) {
199+ generatedSourceMapCache . delete ( generatedSourceMapCache . keys ( ) . next ( ) . value ) ;
200+ }
193201 return ;
194202 }
195203 // If it is not a generated source, we assume we are in a "cjs/esm"
0 commit comments