Skip to content

Commit

Permalink
esm: fix erroneous re-initialization of ESMLoader
Browse files Browse the repository at this point in the history
  • Loading branch information
JakobJingleheimer committed Jul 10, 2022
1 parent a933a75 commit b4a7a3f
Show file tree
Hide file tree
Showing 3 changed files with 47 additions and 1 deletion.
10 changes: 9 additions & 1 deletion lib/internal/process/esm_loader.js
Original file line number Diff line number Diff line change
Expand Up @@ -40,14 +40,20 @@ async function importModuleDynamicallyCallback(wrap, specifier, assertions) {
};

const esmLoader = new ESMLoader();

exports.esmLoader = esmLoader;

// Module.runMain() causes loadESM() to re-run (which it should do); however, this should NOT cause
// ESM to be re-initialised; doing so causes duplicate custom loaders to be added to the public
// esmLoader.
let isESMInitialized = false;

/**
* Causes side-effects: user-defined loader hooks are added to esmLoader.
* @returns {void}
*/
async function initializeLoader() {
if (isESMInitialized) { return; }

const { getOptionValue } = require('internal/options');
const customLoaders = getOptionValue('--experimental-loader');

Expand Down Expand Up @@ -75,6 +81,8 @@ async function initializeLoader() {
// Hooks must then be added to external/public loader
// (so they're triggered in userland)
await esmLoader.addCustomLoaders(keyedExportsList);

isESMInitialized = true;
}

exports.loadESM = async function loadESM(callback) {
Expand Down
31 changes: 31 additions & 0 deletions test/es-module/test-esm-initialization.mjs
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
import '../common/index.mjs';
import fixtures from '../common/fixtures.js';
import assert from 'node:assert';
import { spawnSync } from 'node:child_process';
import { fileURLToPath } from 'node:url';


{ // Verify unadulterated source is loaded when there are no loaders
const { status, stderr, stdout } = spawnSync(
process.execPath,
[
'--loader',
fixtures.fileURL('es-module-loaders', 'loader-resolve-passthru.mjs'),
'--no-warnings',
fileURLToPath(fixtures.fileURL('es-modules', 'runmain.mjs')),
],
{ encoding: 'utf8' },
);

const resolveHookRunCount = [...(stdout.matchAll(/resolve passthru/g) ?? new Array() )]
.length - 1; // less 1 because the first is the needle

assert.strictEqual(stderr, '');
/**
* resolveHookRunCount = 2:
* 1. fixtures/…/runmain.mjs
* 2. node:module (imported by fixtures/…/runmain.mjs)
*/
assert.strictEqual(resolveHookRunCount, 2);
assert.strictEqual(status, 0);
}
7 changes: 7 additions & 0 deletions test/fixtures/es-modules/runmain.mjs
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
import { runMain } from 'node:module';

try { await import.meta.resolve('doesnt-matter.mjs') } catch {}

runMain();

try { await import.meta.resolve('doesnt-matter.mjs') } catch {}

0 comments on commit b4a7a3f

Please sign in to comment.