Skip to content

Commit 22d7777

Browse files
yesmeckaduh95
authored andcommitted
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 <duhamelantoine1995@gmail.com> Reviewed-By: Matteo Collina <matteo.collina@gmail.com>
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

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: 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

Lines changed: 3 additions & 1 deletion
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)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy