Skip to content

Commit

Permalink
esm: fix inconsistency of importAssertion between resolve and load hook
Browse files Browse the repository at this point in the history
As the [documentation](https://nodejs.org/docs/latest/api/module.html#customization-hooks:~:text=The%20property%20context.importAssertions%20is%20replaced%20with%20context.importAttributes.%20Using%20the%20old%20name%20is%20still%20supported%20and%20will%20emit%20an%20experimental%20warning.) says, the `context.importAssertion` should be still supported and emit a warning. This is true for the load hook, but not correct for the context of resolve hook.

This PR is tring to fix the inconsistency.
  • Loading branch information
yesmeck committed Oct 12, 2024
1 parent f98d9c1 commit 8530577
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 3 deletions.
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: '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

0 comments on commit 8530577

Please sign in to comment.