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