Skip to content

Commit e529ea4

Browse files
joyeecheungtargos
authored andcommitted
lib: lazy-load deps in source_map_cache.js
So that the file can be snapshotted. PR-URL: #45849 Reviewed-By: Geoffrey Booth <webadmin@geoffreybooth.com> Reviewed-By: Chengzhong Wu <legendecas@gmail.com>
1 parent 943852a commit e529ea4

File tree

2 files changed

+12
-9
lines changed

2 files changed

+12
-9
lines changed

lib/internal/source_map/source_map_cache.js

+12-8
Original file line numberDiff line numberDiff line change
@@ -24,17 +24,19 @@ let debug = require('internal/util/debuglog').debuglog('source_map', (fn) => {
2424
debug = fn;
2525
});
2626
const { getOptionValue } = require('internal/options');
27-
const { IterableWeakMap } = require('internal/util/iterable_weak_map');
28-
const {
29-
normalizeReferrerURL,
30-
} = require('internal/modules/helpers');
27+
3128
const { validateBoolean } = require('internal/validators');
3229
const { 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:
3941
const 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
4446
const kSourceURLMagicComment = /\/[*/]#\s+sourceURL=(?<sourceURL>[^\s]+)/g;
4547

4648
const { fileURLToPath, pathToFileURL, URL } = require('internal/url');
49+
4750
let SourceMap;
4851

4952
let 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

293297
function 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) {

test/parallel/test-bootstrap-modules.js

-1
Original file line numberDiff line numberDiff line change
@@ -87,7 +87,6 @@ const expectedModules = new Set([
8787
'NativeModule internal/util',
8888
'NativeModule internal/util/debuglog',
8989
'NativeModule internal/util/inspect',
90-
'NativeModule internal/util/iterable_weak_map',
9190
'NativeModule internal/util/types',
9291
'NativeModule internal/v8/startup_snapshot',
9392
'NativeModule internal/validators',

0 commit comments

Comments
 (0)