@@ -24,17 +24,19 @@ let debug = require('internal/util/debuglog').debuglog('source_map', (fn) => {
2424 debug = fn ;
2525} ) ;
2626const { getOptionValue } = require ( 'internal/options' ) ;
27- const { IterableWeakMap } = require ( 'internal/util/iterable_weak_map' ) ;
28- const {
29- normalizeReferrerURL,
30- } = require ( 'internal/modules/helpers' ) ;
27+
3128const { validateBoolean } = require ( 'internal/validators' ) ;
3229const { setMaybeCacheGeneratedSourceMap } = internalBinding ( 'errors' ) ;
30+ const { getLazy } = require ( 'internal/util' ) ;
3331
3432// Since the CJS module cache is mutable, which leads to memory leaks when
3533// modules are deleted, we use a WeakMap so that the source map cache will
3634// be purged automatically:
37- const cjsSourceMapCache = new IterableWeakMap ( ) ;
35+ const getCjsSourceMapCache = getLazy ( ( ) => {
36+ const { IterableWeakMap } = require ( 'internal/util/iterable_weak_map' ) ;
37+ return new IterableWeakMap ( ) ;
38+ } ) ;
39+
3840// The esm cache is not mutable, so we can use a Map without memory concerns:
3941const esmSourceMapCache = new SafeMap ( ) ;
4042// The generated sources is not mutable, so we can use a Map without memory concerns:
@@ -44,6 +46,7 @@ const kSourceMappingURLMagicComment = /\/[*/]#\s+sourceMappingURL=(?<sourceMappi
4446const kSourceURLMagicComment = / \/ [ * / ] # \s + s o u r c e U R L = (?< sourceURL > [ ^ \s ] + ) / g;
4547
4648const { fileURLToPath, pathToFileURL, URL } = require ( 'internal/url' ) ;
49+
4750let SourceMap ;
4851
4952let sourceMapsEnabled ;
@@ -114,6 +117,7 @@ function maybeCacheSourceMap(filename, content, cjsModuleInstance, isGeneratedSo
114117 const sourceMapsEnabled = getSourceMapsEnabled ( ) ;
115118 if ( ! ( process . env . NODE_V8_COVERAGE || sourceMapsEnabled ) ) return ;
116119 try {
120+ const { normalizeReferrerURL } = require ( 'internal/modules/helpers' ) ;
117121 filename = normalizeReferrerURL ( filename ) ;
118122 } catch ( err ) {
119123 // This is most likely an invalid filename in sourceURL of [eval]-wrapper.
@@ -137,7 +141,7 @@ function maybeCacheSourceMap(filename, content, cjsModuleInstance, isGeneratedSo
137141 const data = dataFromUrl ( filename , sourceMapURL ) ;
138142 const url = data ? null : sourceMapURL ;
139143 if ( cjsModuleInstance ) {
140- cjsSourceMapCache . set ( cjsModuleInstance , {
144+ getCjsSourceMapCache ( ) . set ( cjsModuleInstance , {
141145 filename,
142146 lineLengths : lineLengths ( content ) ,
143147 data,
@@ -291,7 +295,7 @@ function sourceMapCacheToObject() {
291295}
292296
293297function appendCJSCache ( obj ) {
294- for ( const value of cjsSourceMapCache ) {
298+ for ( const value of getCjsSourceMapCache ( ) ) {
295299 obj [ ObjectGetValueSafe ( value , 'filename' ) ] = {
296300 lineLengths : ObjectGetValueSafe ( value , 'lineLengths' ) ,
297301 data : ObjectGetValueSafe ( value , 'data' ) ,
@@ -309,7 +313,7 @@ function findSourceMap(sourceURL) {
309313 }
310314 let sourceMap = esmSourceMapCache . get ( sourceURL ) ?? generatedSourceMapCache . get ( sourceURL ) ;
311315 if ( sourceMap === undefined ) {
312- for ( const value of cjsSourceMapCache ) {
316+ for ( const value of getCjsSourceMapCache ( ) ) {
313317 const filename = ObjectGetValueSafe ( value , 'filename' ) ;
314318 const cachedSourceURL = ObjectGetValueSafe ( value , 'sourceURL' ) ;
315319 if ( sourceURL === filename || sourceURL === cachedSourceURL ) {
0 commit comments