Skip to content

Commit 1410c00

Browse files
committed
fixup! module: add clearCache for CJS and ESM
1 parent 7e9a7c5 commit 1410c00

1 file changed

Lines changed: 41 additions & 0 deletions

File tree

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
// Flags: --expose-internals
2+
// Verifies that ModuleWrap instances created by import() are garbage-
3+
// collectible after clearCache with caches: 'all'. This is the strongest
4+
// guarantee that clearing works end-to-end: V8 itself can reclaim the
5+
// underlying module.
6+
'use strict';
7+
8+
const common = require('../common');
9+
10+
const { pathToFileURL } = require('node:url');
11+
const { clearCache } = require('node:module');
12+
const { internalBinding } = require('internal/test/binding');
13+
const { ModuleWrap } = internalBinding('module_wrap');
14+
const { checkIfCollectableByCounting } = require('../common/gc');
15+
16+
const fixtureURL = new URL(
17+
'../fixtures/module-cache/esm-counter.mjs',
18+
pathToFileURL(__filename),
19+
);
20+
const parentURL = pathToFileURL(__filename).href;
21+
22+
const outer = 8;
23+
const inner = 4;
24+
25+
const runIteration = common.mustCallAtLeast(async (i) => {
26+
for (let j = 0; j < inner; j++) {
27+
const url = `${fixtureURL.href}?hmr=${i}-${j}`;
28+
await import(url);
29+
clearCache(url, {
30+
parentURL,
31+
resolver: 'import',
32+
caches: 'all',
33+
});
34+
}
35+
return inner;
36+
});
37+
38+
checkIfCollectableByCounting(runIteration, ModuleWrap, outer)
39+
.then(common.mustCall(() => {
40+
delete globalThis.__module_cache_esm_counter;
41+
}));

0 commit comments

Comments
 (0)