Skip to content

Commit b0a6048

Browse files
committed
Restore refactored hook-resolve-type test
1 parent 2ad5a35 commit b0a6048

File tree

4 files changed

+91
-1
lines changed

4 files changed

+91
-1
lines changed

lib/internal/modules/esm/loader.js

-1
Original file line numberDiff line numberDiff line change
@@ -168,7 +168,6 @@ class ModuleLoader {
168168
*/
169169
setCustomizations(customizations) {
170170
this.#customizations = customizations;
171-
this.isCustomized = customizations != null;
172171
if (customizations) {
173172
this.allowImportMetaResolve = customizations.allowImportMetaResolve;
174173
} else {
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
// Flags: --import ./test/fixtures/es-module-loaders/hook-resolve-type.mjs
2+
import '../common/index.mjs';
3+
import * as fixtures from '../common/fixtures.mjs';
4+
import { getModuleTypeStats } from '../fixtures/es-module-loaders/hook-resolve-type.mjs';
5+
import { strict as assert } from 'assert';
6+
import * as fs from 'fs';
7+
8+
const { importedESM: importedESMBefore,
9+
importedCJS: importedCJSBefore } = getModuleTypeStats();
10+
11+
const basePath =
12+
new URL('./node_modules/', import.meta.url);
13+
14+
const rel = (file) => new URL(file, basePath);
15+
const createDir = (path) => {
16+
if (!fs.existsSync(path)) {
17+
fs.mkdirSync(path);
18+
}
19+
};
20+
21+
const moduleName = 'module-counter-by-type';
22+
const moduleDir = rel(`${moduleName}`);
23+
24+
try {
25+
createDir(basePath);
26+
createDir(moduleDir);
27+
fs.cpSync(
28+
fixtures.path('es-modules', moduleName),
29+
moduleDir,
30+
{ recursive: true }
31+
);
32+
33+
await import(`${moduleName}`);
34+
} finally {
35+
fs.rmSync(basePath, { recursive: true, force: true });
36+
}
37+
38+
const { importedESM: importedESMAfter,
39+
importedCJS: importedCJSAfter } = getModuleTypeStats();
40+
41+
// Dynamic import above should increment ESM counter but not CJS counter
42+
assert.strictEqual(importedESMBefore + 1, importedESMAfter);
43+
assert.strictEqual(importedCJSBefore, importedCJSAfter);
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
@@ -0,0 +1,30 @@
1+
import * as fixtures from '../../common/fixtures.mjs';
2+
import { register } from 'node:module';
3+
import { MessageChannel } from 'node:worker_threads';
4+
5+
let importedESM = 0;
6+
let importedCJS = 0;
7+
export function getModuleTypeStats() {
8+
return { importedESM, importedCJS };
9+
};
10+
11+
const { port1, port2 } = new MessageChannel();
12+
13+
register(fixtures.fileURL('es-module-loaders/hook-resolve-type-loader.mjs'), {
14+
data: { port: port2 },
15+
transferList: [port2],
16+
});
17+
18+
port1.on('message', ({ type }) => {
19+
switch (type) {
20+
case 'module':
21+
importedESM++;
22+
break;
23+
case 'commonjs':
24+
importedCJS++;
25+
break;
26+
}
27+
});
28+
29+
port1.unref();
30+
port2.unref();

0 commit comments

Comments
 (0)