Skip to content

Commit 8530577

Browse files
committed
esm: fix inconsistency of importAssertion between resolve and load hook
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.
1 parent f98d9c1 commit 8530577

File tree

2 files changed

+8
-3
lines changed

2 files changed

+8
-3
lines changed

lib/internal/modules/esm/hooks.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -237,7 +237,7 @@ class Hooks {
237237

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

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

243243
validateOutput(hookErrIdentifier, resolution);

test/es-module/test-esm-import-assertion-warning.mjs

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,11 @@ await Promise.all([
77
`data:text/javascript,export ${encodeURIComponent(function resolve() {
88
return { shortCircuit: true, url: 'data:application/json,1', importAssertions: { type: 'json' } };
99
})}`,
10+
// Using importAssertions on the context object of the resolve hook should warn but still work.
11+
`data:text/javascript,export ${encodeURIComponent(function resolve(s, c, n) {
12+
const type = c.importAssertions.type;
13+
return { shortCircuit: true, url: 'data:application/json,1', importAttributes: { type: 'json' } };
14+
})}`,
1015
// Setting importAssertions on the context object of the load hook should warn but still work.
1116
`data:text/javascript,export ${encodeURIComponent(function load(u, c, n) {
1217
c.importAssertions = { type: 'json' };
@@ -22,9 +27,9 @@ await Promise.all([
2227
'--eval', `
2328
import assert from 'node:assert';
2429
import { register } from 'node:module';
25-
30+
2631
register(${JSON.stringify(loaderURL)});
27-
32+
2833
assert.deepStrictEqual(
2934
{ ...await import('data:') },
3035
{ default: 1 }

0 commit comments

Comments
 (0)