/
hook-resolve-type.mjs
44 lines (38 loc) · 1.29 KB
/
hook-resolve-type.mjs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
let importedESM = 0;
let importedCJS = 0;
export function globalPreload({ port }) {
port.on('message', (int32) => {
port.postMessage({ importedESM, importedCJS });
Atomics.store(int32, 0, 1);
Atomics.notify(int32, 0);
});
port.unref();
return `
const { receiveMessageOnPort } = getBuiltin('worker_threads');
global.getModuleTypeStats = async function getModuleTypeStats() {
const sab = new SharedArrayBuffer(4);
const int32 = new Int32Array(sab);
port.postMessage(int32);
// Artificial timeout to keep the event loop alive.
// https://bugs.chromium.org/p/v8/issues/detail?id=13238
// TODO(targos) Remove when V8 issue is resolved.
const timeout = setTimeout(() => { throw new Error('timeout'); }, 1_000);
await Atomics.waitAsync(int32, 0, 0).value;
clearTimeout(timeout);
return receiveMessageOnPort(port).message;
};
`;
}
export async function load(url, context, next) {
return next(url);
}
export async function resolve(specifier, context, next) {
const nextResult = await next(specifier, context);
const { format } = nextResult;
if (format === 'module' || specifier.endsWith('.mjs')) {
importedESM++;
} else if (format == null || format === 'commonjs') {
importedCJS++;
}
return nextResult;
}