Skip to content

Commit

Permalink
esm: remove erroneous context.parentURL property passed to load hook
Browse files Browse the repository at this point in the history
  • Loading branch information
JakobJingleheimer committed Feb 15, 2022
1 parent ceaa299 commit 5e2ba51
Show file tree
Hide file tree
Showing 2 changed files with 70 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 @@ -282,7 +282,6 @@ class ESMLoader {
} = await this.load(url, {
format,
importAssertions,
parentURL,
});

const translator = translators.get(finalFormat);
Expand Down
70 changes: 70 additions & 0 deletions test/es-module/test-esm-loader-hooks.mjs
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
// Flags: --expose-internals
import '../common/index.mjs';
import mod from 'internal/modules/esm/loader';
import assert from 'assert';

const { ESMLoader } = mod;

{
const esmLoader = new ESMLoader();

const originalSpecifier = 'foo/bar';
const importAssertions = { type: 'json' };
const parentURL = 'file:///entrypoint.js';
const resolvedURL = 'file:///foo/bar.js';
const suggestedFormat = 'test';

const customLoader1 = {
resolve(specifier, context, defaultResolve) {
assert.equal(specifier, originalSpecifier);
assert.deepEqual(Object.keys(context), [
'conditions',
'importAssertions',
'parentURL',
]);
assert.ok(Array.isArray(context.conditions));
assert.equal(context.importAssertions, importAssertions);
assert.equal(context.parentURL, parentURL);
assert.equal(typeof defaultResolve, 'function');

// This doesn't matter. just to avoid errors
return {
format: suggestedFormat,
url: resolvedURL,
};
},
load(resolvedURL, context, defaultLoad) {
assert.equal(resolvedURL, resolvedURL);
assert.ok(new URL(resolvedURL));
assert.deepEqual(Object.keys(context), [
'format',
'importAssertions',
]);
assert.equal(context.format, suggestedFormat);
assert.equal(context.importAssertions, importAssertions);
assert.equal(typeof defaultLoad, 'function');

// This doesn't matter. just to avoid errors
return {
format: 'module',
source: '',
};
},
};

esmLoader.addCustomLoaders(customLoader1);

esmLoader.resolve(
originalSpecifier,
parentURL,
importAssertions,
);
esmLoader.load(
resolvedURL,
{
format: suggestedFormat,
importAssertions,
},
function mockDefaultLoad() {},
);
}

0 comments on commit 5e2ba51

Please sign in to comment.