Skip to content

Commit 22d7777

Browse files
yesmeckaduh95
authored andcommittedOct 19, 2024
esm: fix inconsistency with importAssertion in resolve hook
As the documentation states, the `context.importAssertion` should be still supported and emit a warning. This is true for the `load` hook, but not correct for context of the `resolve` hook. This commit fixes the inconsistency. PR-URL: #55365 Reviewed-By: Antoine du Hamel <[email protected]> Reviewed-By: Matteo Collina <[email protected]>
1 parent 903863c commit 22d7777

File tree

3 files changed

+11
-4
lines changed

3 files changed

+11
-4
lines changed
 

‎lib/internal/modules/esm/hooks.js

+1-1
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

+7-2
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: 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 }

‎test/fixtures/es-module-loaders/hooks-input.mjs

+3-1
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,7 @@ export async function resolve(specifier, context, next) {
3737
'conditions',
3838
'importAttributes',
3939
'parentURL',
40+
'importAssertions',
4041
]);
4142
assert.ok(Array.isArray(context.conditions));
4243
assert.strictEqual(typeof next, 'function');
@@ -71,9 +72,10 @@ export async function load(url, context, next) {
7172

7273
assert.ok(new URL(url));
7374
// Ensure `context` has all and only the properties it's supposed to
74-
assert.deepStrictEqual(Object.keys(context), [
75+
assert.deepStrictEqual(Reflect.ownKeys(context), [
7576
'format',
7677
'importAttributes',
78+
'importAssertions',
7779
]);
7880
assert.strictEqual(context.format, 'test');
7981
assert.strictEqual(typeof next, 'function');

0 commit comments

Comments
 (0)
Please sign in to comment.