2022-12-31 21:23:36 +00:00
|
|
|
// 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';
|
2023-04-13 07:35:17 +00:00
|
|
|
import { writeSync } from 'fs';
|
2022-12-31 21:23:36 +00:00
|
|
|
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$/);
|
2023-06-12 00:00:46 +00:00
|
|
|
|
|
|
|
if (!(/\[eval\d*\]$/).test(context.parentURL)) {
|
|
|
|
assert.strictEqual(context.parentURL, undefined);
|
|
|
|
}
|
|
|
|
|
2023-10-14 03:52:38 +00:00
|
|
|
assert.deepStrictEqual(context.importAttributes, {});
|
2022-12-31 21:23:36 +00:00
|
|
|
} else if (resolveCalls === 2) {
|
|
|
|
url = new URL(specifier, context.parentURL).href;
|
|
|
|
assert.match(specifier, /experimental\.json$/);
|
|
|
|
assert.match(context.parentURL, /json-modules\.mjs$/);
|
2023-10-14 03:52:38 +00:00
|
|
|
assert.deepStrictEqual(context.importAttributes, {
|
2022-12-31 21:23:36 +00:00
|
|
|
type: 'json',
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
// Ensure `context` has all and only the properties it's supposed to
|
|
|
|
assert.deepStrictEqual(Reflect.ownKeys(context), [
|
|
|
|
'conditions',
|
2023-10-14 03:52:38 +00:00
|
|
|
'importAttributes',
|
2022-12-31 21:23:36 +00:00
|
|
|
'parentURL',
|
2024-10-15 19:02:07 +00:00
|
|
|
'importAssertions',
|
2022-12-31 21:23:36 +00:00
|
|
|
]);
|
|
|
|
assert.ok(Array.isArray(context.conditions));
|
|
|
|
assert.strictEqual(typeof next, 'function');
|
|
|
|
|
|
|
|
const returnValue = {
|
|
|
|
url,
|
|
|
|
format: 'test',
|
|
|
|
shortCircuit: true,
|
|
|
|
}
|
|
|
|
|
2023-04-13 07:35:17 +00:00
|
|
|
writeSync(1, JSON.stringify(returnValue) + '\n'); // For the test to validate when it parses stdout
|
2022-12-31 21:23:36 +00:00
|
|
|
|
|
|
|
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$/);
|
2023-10-14 03:52:38 +00:00
|
|
|
assert.deepStrictEqual(context.importAttributes, {});
|
2022-12-31 21:23:36 +00:00
|
|
|
format = 'module';
|
|
|
|
} else if (loadCalls === 2) {
|
|
|
|
assert.match(url, /experimental\.json$/);
|
2023-10-14 03:52:38 +00:00
|
|
|
assert.deepStrictEqual(context.importAttributes, {
|
2022-12-31 21:23:36 +00:00
|
|
|
type: 'json',
|
|
|
|
});
|
|
|
|
format = 'json';
|
|
|
|
}
|
|
|
|
|
|
|
|
assert.ok(new URL(url));
|
|
|
|
// Ensure `context` has all and only the properties it's supposed to
|
2024-10-15 19:02:07 +00:00
|
|
|
assert.deepStrictEqual(Reflect.ownKeys(context), [
|
2022-12-31 21:23:36 +00:00
|
|
|
'format',
|
2023-10-14 03:52:38 +00:00
|
|
|
'importAttributes',
|
2024-10-15 19:02:07 +00:00
|
|
|
'importAssertions',
|
2022-12-31 21:23:36 +00:00
|
|
|
]);
|
|
|
|
assert.strictEqual(context.format, 'test');
|
|
|
|
assert.strictEqual(typeof next, 'function');
|
|
|
|
|
|
|
|
const returnValue = {
|
|
|
|
source,
|
|
|
|
format,
|
|
|
|
shortCircuit: true,
|
|
|
|
};
|
|
|
|
|
2023-04-13 07:35:17 +00:00
|
|
|
writeSync(1, JSON.stringify(returnValue) + '\n'); // For the test to validate when it parses stdout
|
2022-12-31 21:23:36 +00:00
|
|
|
|
|
|
|
return returnValue;
|
|
|
|
}
|