@@ -24,17 +24,19 @@ let debug = require('internal/util/debuglog').debuglog('source_map', (fn) => { |
24 | 24 | debug = fn; |
25 | 25 | }); |
26 | 26 | const { getOptionValue } = require('internal/options'); |
27 | | -const { IterableWeakMap } = require('internal/util/iterable_weak_map'); |
28 | | -const { |
29 | | - normalizeReferrerURL, |
30 | | -} = require('internal/modules/helpers'); |
| 27 | + |
31 | 28 | const { validateBoolean } = require('internal/validators'); |
32 | 29 | const { setMaybeCacheGeneratedSourceMap } = internalBinding('errors'); |
| 30 | +const { getLazy } = require('internal/util'); |
33 | 31 |
|
34 | 32 | // Since the CJS module cache is mutable, which leads to memory leaks when |
35 | 33 | // modules are deleted, we use a WeakMap so that the source map cache will |
36 | 34 | // 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 | + |
38 | 40 | // The esm cache is not mutable, so we can use a Map without memory concerns: |
39 | 41 | const esmSourceMapCache = new SafeMap(); |
40 | 42 | // The generated sources is not mutable, so we can use a Map without memory concerns: |
@@ -44,6 +46,7 @@ const kSourceMappingURLMagicComment = /\/[*/]#\s+sourceMappingURL=(?<sourceMappi |
44 | 46 | const kSourceURLMagicComment = /\/[*/]#\s+sourceURL=(?<sourceURL>[^\s]+)/g; |
45 | 47 |
|
46 | 48 | const { fileURLToPath, pathToFileURL, URL } = require('internal/url'); |
| 49 | + |
47 | 50 | let SourceMap; |
48 | 51 |
|
49 | 52 | let sourceMapsEnabled; |
@@ -114,6 +117,7 @@ function maybeCacheSourceMap(filename, content, cjsModuleInstance, isGeneratedSo |
114 | 117 | const sourceMapsEnabled = getSourceMapsEnabled(); |
115 | 118 | if (!(process.env.NODE_V8_COVERAGE || sourceMapsEnabled)) return; |
116 | 119 | try { |
| 120 | + const { normalizeReferrerURL } = require('internal/modules/helpers'); |
117 | 121 | filename = normalizeReferrerURL(filename); |
118 | 122 | } catch (err) { |
119 | 123 | // This is most likely an invalid filename in sourceURL of [eval]-wrapper. |
@@ -137,7 +141,7 @@ function maybeCacheSourceMap(filename, content, cjsModuleInstance, isGeneratedSo |
137 | 141 | const data = dataFromUrl(filename, sourceMapURL); |
138 | 142 | const url = data ? null : sourceMapURL; |
139 | 143 | if (cjsModuleInstance) { |
140 | | - cjsSourceMapCache.set(cjsModuleInstance, { |
| 144 | + getCjsSourceMapCache().set(cjsModuleInstance, { |
141 | 145 | filename, |
142 | 146 | lineLengths: lineLengths(content), |
143 | 147 | data, |
@@ -291,7 +295,7 @@ function sourceMapCacheToObject() { |
291 | 295 | } |
292 | 296 |
|
293 | 297 | function appendCJSCache(obj) { |
294 | | - for (const value of cjsSourceMapCache) { |
| 298 | + for (const value of getCjsSourceMapCache()) { |
295 | 299 | obj[ObjectGetValueSafe(value, 'filename')] = { |
296 | 300 | lineLengths: ObjectGetValueSafe(value, 'lineLengths'), |
297 | 301 | data: ObjectGetValueSafe(value, 'data'), |
@@ -309,7 +313,7 @@ function findSourceMap(sourceURL) { |
309 | 313 | } |
310 | 314 | let sourceMap = esmSourceMapCache.get(sourceURL) ?? generatedSourceMapCache.get(sourceURL); |
311 | 315 | if (sourceMap === undefined) { |
312 | | - for (const value of cjsSourceMapCache) { |
| 316 | + for (const value of getCjsSourceMapCache()) { |
313 | 317 | const filename = ObjectGetValueSafe(value, 'filename'); |
314 | 318 | const cachedSourceURL = ObjectGetValueSafe(value, 'sourceURL'); |
315 | 319 | if (sourceURL === filename || sourceURL === cachedSourceURL) { |
|