Skip to content

Commit 63c3846

Browse files
GeoffreyBoothnodejs-github-bot
authored andcommitted
esm: refactor test-esm-loader-resolve-type
PR-URL: #49493 Reviewed-By: Jacob Smith <[email protected]> Reviewed-By: Antoine du Hamel <[email protected]>
1 parent a927ade commit 63c3846

File tree

3 files changed

+47
-41
lines changed

3 files changed

+47
-41
lines changed

test/es-module/test-esm-loader-resolve-type.mjs

+6-4
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ try {
1616
{ recursive: true }
1717
);
1818

19-
deepStrictEqual(await spawnPromisified(
19+
const output = await spawnPromisified(
2020
execPath,
2121
[
2222
'--no-warnings',
@@ -29,15 +29,17 @@ try {
2929
console.log(JSON.stringify({ before, after }));`,
3030
],
3131
{ cwd: base },
32-
), {
32+
);
33+
34+
deepStrictEqual(output, {
35+
code: 0,
36+
signal: null,
3337
stderr: '',
3438
stdout: JSON.stringify({
3539
before: { importedESM: 0, importedCJS: 0 },
3640
// Dynamic import in the eval script should increment ESM counter but not CJS counter
3741
after: { importedESM: 1, importedCJS: 0 },
3842
}) + '\n',
39-
code: 0,
40-
signal: null,
4143
});
4244
} finally {
4345
await rm(base, { recursive: true, force: true });
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
/** @type {MessagePort} */
2+
let port;
3+
export function initialize(data) {
4+
port = data.port;
5+
}
6+
7+
export async function resolve(specifier, context, next) {
8+
const nextResult = await next(specifier, context);
9+
const { format } = nextResult;
10+
11+
if (format === 'module' || specifier.endsWith('.mjs')) {
12+
port.postMessage({ type: 'module' });
13+
} else if (format == null || format === 'commonjs') {
14+
port.postMessage({ type: 'commonjs' });
15+
}
16+
17+
return nextResult;
18+
}
Original file line numberDiff line numberDiff line change
@@ -1,44 +1,30 @@
1+
import * as fixtures from '../../common/fixtures.mjs';
2+
import { register } from 'node:module';
3+
import { MessageChannel } from 'node:worker_threads';
4+
15
let importedESM = 0;
26
let importedCJS = 0;
7+
export function getModuleTypeStats() {
8+
return { importedESM, importedCJS };
9+
};
310

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();
3112

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+
});
3517

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;
4026
}
27+
});
4128

42-
return nextResult;
43-
}
44-
29+
port1.unref();
30+
port2.unref();

0 commit comments

Comments
 (0)