mirror of
https://github.com/zebrajr/node.git
synced 2025-12-06 00:20:08 +01:00
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: https://github.com/nodejs/node/pull/55365 Reviewed-By: Antoine du Hamel <duhamelantoine1995@gmail.com> Reviewed-By: Matteo Collina <matteo.collina@gmail.com>
This commit is contained in:
parent
4dc76a990f
commit
c0aebed4b3
|
|
@ -237,7 +237,7 @@ class Hooks {
|
||||||
|
|
||||||
const nextResolve = nextHookFactory(chain[chain.length - 1], meta, { validateArgs, validateOutput });
|
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
|
const { hookErrIdentifier } = meta; // Retrieve the value after all settled
|
||||||
|
|
||||||
validateOutput(hookErrIdentifier, resolution);
|
validateOutput(hookErrIdentifier, resolution);
|
||||||
|
|
|
||||||
|
|
@ -7,6 +7,11 @@ await Promise.all([
|
||||||
`data:text/javascript,export ${encodeURIComponent(function resolve() {
|
`data:text/javascript,export ${encodeURIComponent(function resolve() {
|
||||||
return { shortCircuit: true, url: 'data:application/json,1', importAssertions: { type: 'json' } };
|
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: type ?? 'json' } };
|
||||||
|
})}`,
|
||||||
// Setting importAssertions on the context object of the load hook should warn but still work.
|
// 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) {
|
`data:text/javascript,export ${encodeURIComponent(function load(u, c, n) {
|
||||||
c.importAssertions = { type: 'json' };
|
c.importAssertions = { type: 'json' };
|
||||||
|
|
@ -22,9 +27,9 @@ await Promise.all([
|
||||||
'--eval', `
|
'--eval', `
|
||||||
import assert from 'node:assert';
|
import assert from 'node:assert';
|
||||||
import { register } from 'node:module';
|
import { register } from 'node:module';
|
||||||
|
|
||||||
register(${JSON.stringify(loaderURL)});
|
register(${JSON.stringify(loaderURL)});
|
||||||
|
|
||||||
assert.deepStrictEqual(
|
assert.deepStrictEqual(
|
||||||
{ ...await import('data:') },
|
{ ...await import('data:') },
|
||||||
{ default: 1 }
|
{ default: 1 }
|
||||||
|
|
|
||||||
|
|
@ -37,6 +37,7 @@ export async function resolve(specifier, context, next) {
|
||||||
'conditions',
|
'conditions',
|
||||||
'importAttributes',
|
'importAttributes',
|
||||||
'parentURL',
|
'parentURL',
|
||||||
|
'importAssertions',
|
||||||
]);
|
]);
|
||||||
assert.ok(Array.isArray(context.conditions));
|
assert.ok(Array.isArray(context.conditions));
|
||||||
assert.strictEqual(typeof next, 'function');
|
assert.strictEqual(typeof next, 'function');
|
||||||
|
|
@ -71,9 +72,10 @@ export async function load(url, context, next) {
|
||||||
|
|
||||||
assert.ok(new URL(url));
|
assert.ok(new URL(url));
|
||||||
// Ensure `context` has all and only the properties it's supposed to
|
// Ensure `context` has all and only the properties it's supposed to
|
||||||
assert.deepStrictEqual(Object.keys(context), [
|
assert.deepStrictEqual(Reflect.ownKeys(context), [
|
||||||
'format',
|
'format',
|
||||||
'importAttributes',
|
'importAttributes',
|
||||||
|
'importAssertions',
|
||||||
]);
|
]);
|
||||||
assert.strictEqual(context.format, 'test');
|
assert.strictEqual(context.format, 'test');
|
||||||
assert.strictEqual(typeof next, 'function');
|
assert.strictEqual(typeof next, 'function');
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue
Block a user