Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

esm: fix inconsistency with importAssertion in resolve hook #55365

Merged
merged 2 commits into from
Oct 15, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion lib/internal/modules/esm/hooks.js
Original file line number Diff line number Diff line change
Expand Up @@ -237,7 +237,7 @@ class Hooks {

const nextResolve = nextHookFactory(chain[chain.length - 1], meta, { validateArgs, validateOutput });

const resolution = await nextResolve(originalSpecifier, context);
const resolution = await nextResolve(originalSpecifier, defineImportAssertionAlias(context));
const { hookErrIdentifier } = meta; // Retrieve the value after all settled

validateOutput(hookErrIdentifier, resolution);
Expand Down
9 changes: 7 additions & 2 deletions test/es-module/test-esm-import-assertion-warning.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,11 @@ await Promise.all([
`data:text/javascript,export ${encodeURIComponent(function resolve() {
return { shortCircuit: true, url: 'data:application/json,1', importAssertions: { type: 'json' } };
})}`,
// Using importAssertions on the context object of the resolve hook should warn but still work.
`data:text/javascript,export ${encodeURIComponent(function resolve(s, c, n) {
const type = c.importAssertions.type;
return { shortCircuit: true, url: 'data:application/json,1', importAttributes: { type: type ?? 'json' } };
})}`,
// Setting importAssertions on the context object of the load hook should warn but still work.
`data:text/javascript,export ${encodeURIComponent(function load(u, c, n) {
c.importAssertions = { type: 'json' };
Expand All @@ -22,9 +27,9 @@ await Promise.all([
'--eval', `
import assert from 'node:assert';
import { register } from 'node:module';
register(${JSON.stringify(loaderURL)});
assert.deepStrictEqual(
{ ...await import('data:') },
{ default: 1 }
Expand Down
6 changes: 4 additions & 2 deletions test/fixtures/es-module-loaders/hooks-input.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ export async function resolve(specifier, context, next) {
'conditions',
'importAttributes',
'parentURL',
'importAssertions',
]);
assert.ok(Array.isArray(context.conditions));
assert.strictEqual(typeof next, 'function');
Expand Down Expand Up @@ -71,9 +72,10 @@ export async function load(url, context, next) {

assert.ok(new URL(url));
// Ensure `context` has all and only the properties it's supposed to
assert.deepStrictEqual(Object.keys(context), [
assert.deepStrictEqual(Reflect.ownKeys(context), [
'format',
'importAttributes',
'importAssertions',
]);
assert.strictEqual(context.format, 'test');
assert.strictEqual(typeof next, 'function');
Expand All @@ -87,4 +89,4 @@ export async function load(url, context, next) {
writeSync(1, JSON.stringify(returnValue) + '\n'); // For the test to validate when it parses stdout

return returnValue;
}
}
aduh95 marked this conversation as resolved.
Show resolved Hide resolved
Loading