Skip to content

Commit aed758d

Browse files
yesmeckruyadorno
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 a3b793d commit aed758d

3 files changed

Lines changed: 11 additions & 4 deletions

File tree

‎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
constnextResolve=nextHookFactory(chain[chain.length-1],meta,{ validateArgs, validateOutput });
239239

240-
constresolution=awaitnextResolve(originalSpecifier,context);
240+
constresolution=awaitnextResolve(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(functionresolve(){
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(functionresolve(s,c,n){
12+
consttype=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(functionload(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(typeofnext,'function');
@@ -71,9 +72,10 @@ export async function load(url, context, next) {
7172

7273
assert.ok(newURL(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(typeofnext,'function');

0 commit comments

Comments
 (0)