Skip to content

Commit

Permalink
Restore refactored hook-resolve-type test
Browse files Browse the repository at this point in the history
  • Loading branch information
GeoffreyBooth committed Sep 3, 2023
1 parent 2ad5a35 commit b0a6048
Show file tree
Hide file tree
Showing 4 changed files with 91 additions and 1 deletion.
1 change: 0 additions & 1 deletion lib/internal/modules/esm/loader.js
Original file line number Diff line number Diff line change
Expand Up @@ -168,7 +168,6 @@ class ModuleLoader {
*/
setCustomizations(customizations) {
this.#customizations = customizations;
this.isCustomized = customizations != null;
if (customizations) {
this.allowImportMetaResolve = customizations.allowImportMetaResolve;
} else {
Expand Down
43 changes: 43 additions & 0 deletions test/es-module/test-esm-loader-resolve-type.mjs
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
// Flags: --import ./test/fixtures/es-module-loaders/hook-resolve-type.mjs
import '../common/index.mjs';
import * as fixtures from '../common/fixtures.mjs';
import { getModuleTypeStats } from '../fixtures/es-module-loaders/hook-resolve-type.mjs';
import { strict as assert } from 'assert';
import * as fs from 'fs';

const { importedESM: importedESMBefore,
importedCJS: importedCJSBefore } = getModuleTypeStats();

const basePath =
new URL('./node_modules/', import.meta.url);

const rel = (file) => new URL(file, basePath);
const createDir = (path) => {
if (!fs.existsSync(path)) {
fs.mkdirSync(path);
}
};

const moduleName = 'module-counter-by-type';
const moduleDir = rel(`${moduleName}`);

try {
createDir(basePath);
createDir(moduleDir);
fs.cpSync(
fixtures.path('es-modules', moduleName),
moduleDir,
{ recursive: true }
);

await import(`${moduleName}`);
} finally {
fs.rmSync(basePath, { recursive: true, force: true });
}

const { importedESM: importedESMAfter,
importedCJS: importedCJSAfter } = getModuleTypeStats();

// Dynamic import above should increment ESM counter but not CJS counter
assert.strictEqual(importedESMBefore + 1, importedESMAfter);
assert.strictEqual(importedCJSBefore, importedCJSAfter);
18 changes: 18 additions & 0 deletions test/fixtures/es-module-loaders/hook-resolve-type-loader.mjs
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
/** @type {MessagePort} */
let port;
export function initialize(data) {
port = data.port;
}

export async function resolve(specifier, context, next) {
const nextResult = await next(specifier, context);
const { format } = nextResult;

if (format === 'module' || specifier.endsWith('.mjs')) {
port.postMessage({ type: 'module' });
} else if (format == null || format === 'commonjs') {
port.postMessage({ type: 'commonjs' });
}

return nextResult;
}
30 changes: 30 additions & 0 deletions test/fixtures/es-module-loaders/hook-resolve-type.mjs
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
import * as fixtures from '../../common/fixtures.mjs';
import { register } from 'node:module';
import { MessageChannel } from 'node:worker_threads';

let importedESM = 0;
let importedCJS = 0;
export function getModuleTypeStats() {
return { importedESM, importedCJS };
};

const { port1, port2 } = new MessageChannel();

register(fixtures.fileURL('es-module-loaders/hook-resolve-type-loader.mjs'), {
data: { port: port2 },
transferList: [port2],
});

port1.on('message', ({ type }) => {
switch (type) {
case 'module':
importedESM++;
break;
case 'commonjs':
importedCJS++;
break;
}
});

port1.unref();
port2.unref();

0 comments on commit b0a6048

Please sign in to comment.