mirror of
https://github.com/zebrajr/node.git
synced 2025-12-06 12:20:27 +01:00
The old import assertions proposal has been
renamed to "import attributes" with the follwing major changes:
1. The keyword is now `with` instead of `assert`.
2. Unknown assertions cause an error rather than being ignored,
This commit updates the documentation to encourage folks to use the new
syntax, and add aliases for module customization hooks.
PR-URL: https://github.com/nodejs/node/pull/50140
Fixes: https://github.com/nodejs/node/issues/50134
Refs: 159c82c5e6
Reviewed-By: Geoffrey Booth <webadmin@geoffreybooth.com>
Reviewed-By: Jacob Smith <jacob@frende.me>
Reviewed-By: Benjamin Gruenbaum <benjamingr@gmail.com>
91 lines
2.4 KiB
JavaScript
91 lines
2.4 KiB
JavaScript
// This is expected to be used by test-esm-loader-hooks.mjs via:
|
|
// node --loader ./test/fixtures/es-module-loaders/hooks-input.mjs ./test/fixtures/es-modules/json-modules.mjs
|
|
|
|
import assert from 'assert';
|
|
import { writeSync } from 'fs';
|
|
import { readFile } from 'fs/promises';
|
|
import { fileURLToPath } from 'url';
|
|
|
|
|
|
let resolveCalls = 0;
|
|
let loadCalls = 0;
|
|
|
|
export async function resolve(specifier, context, next) {
|
|
resolveCalls++;
|
|
let url;
|
|
|
|
if (resolveCalls === 1) {
|
|
url = new URL(specifier).href;
|
|
assert.match(specifier, /json-modules\.mjs$/);
|
|
|
|
if (!(/\[eval\d*\]$/).test(context.parentURL)) {
|
|
assert.strictEqual(context.parentURL, undefined);
|
|
}
|
|
|
|
assert.deepStrictEqual(context.importAttributes, {});
|
|
} else if (resolveCalls === 2) {
|
|
url = new URL(specifier, context.parentURL).href;
|
|
assert.match(specifier, /experimental\.json$/);
|
|
assert.match(context.parentURL, /json-modules\.mjs$/);
|
|
assert.deepStrictEqual(context.importAttributes, {
|
|
type: 'json',
|
|
});
|
|
}
|
|
|
|
// Ensure `context` has all and only the properties it's supposed to
|
|
assert.deepStrictEqual(Reflect.ownKeys(context), [
|
|
'conditions',
|
|
'importAttributes',
|
|
'parentURL',
|
|
]);
|
|
assert.ok(Array.isArray(context.conditions));
|
|
assert.strictEqual(typeof next, 'function');
|
|
|
|
const returnValue = {
|
|
url,
|
|
format: 'test',
|
|
shortCircuit: true,
|
|
}
|
|
|
|
writeSync(1, JSON.stringify(returnValue) + '\n'); // For the test to validate when it parses stdout
|
|
|
|
return returnValue;
|
|
}
|
|
|
|
export async function load(url, context, next) {
|
|
loadCalls++;
|
|
const source = await readFile(fileURLToPath(url));
|
|
let format;
|
|
|
|
if (loadCalls === 1) {
|
|
assert.match(url, /json-modules\.mjs$/);
|
|
assert.deepStrictEqual(context.importAttributes, {});
|
|
format = 'module';
|
|
} else if (loadCalls === 2) {
|
|
assert.match(url, /experimental\.json$/);
|
|
assert.deepStrictEqual(context.importAttributes, {
|
|
type: 'json',
|
|
});
|
|
format = 'json';
|
|
}
|
|
|
|
assert.ok(new URL(url));
|
|
// Ensure `context` has all and only the properties it's supposed to
|
|
assert.deepStrictEqual(Object.keys(context), [
|
|
'format',
|
|
'importAttributes',
|
|
]);
|
|
assert.strictEqual(context.format, 'test');
|
|
assert.strictEqual(typeof next, 'function');
|
|
|
|
const returnValue = {
|
|
source,
|
|
format,
|
|
shortCircuit: true,
|
|
};
|
|
|
|
writeSync(1, JSON.stringify(returnValue) + '\n'); // For the test to validate when it parses stdout
|
|
|
|
return returnValue;
|
|
}
|