mirror of
https://github.com/nodejs/node.git
synced 2025-08-15 13:48:44 +02:00

1. Make sure that the conditions are converted into arrays when being passed into user hooks. 2. Pass the conditions from user hooks into the ESM resolution so that it takes effect. PR-URL: https://github.com/nodejs/node/pull/59011 Fixes: https://github.com/nodejs/node/issues/59003 Reviewed-By: Zeyu "Alex" Yang <himself65@outlook.com> Reviewed-By: Jacob Smith <jacob@frende.me>
53 lines
1.9 KiB
JavaScript
53 lines
1.9 KiB
JavaScript
// This tests that custom conditions can be used in module resolution hooks.
|
|
import '../common/index.mjs';
|
|
import { registerHooks } from 'node:module';
|
|
import assert from 'node:assert';
|
|
import { cjs, esm } from '../fixtures/es-modules/custom-condition/load.cjs';
|
|
|
|
// Without hooks, the default condition is used.
|
|
assert.strictEqual(cjs('foo').result, 'default');
|
|
assert.strictEqual((await esm('foo')).result, 'default');
|
|
|
|
// Prepending 'foo' to the conditions array in the resolve hook should
|
|
// allow a CJS to be resolved with that condition.
|
|
{
|
|
const hooks = registerHooks({
|
|
resolve(specifier, context, nextResolve) {
|
|
assert(Array.isArray(context.conditions));
|
|
context.conditions = ['foo', ...context.conditions];
|
|
return nextResolve(specifier, context);
|
|
},
|
|
});
|
|
assert.strictEqual(cjs('foo/second').result, 'foo');
|
|
assert.strictEqual((await esm('foo/second')).result, 'foo');
|
|
hooks.deregister();
|
|
}
|
|
|
|
// Prepending 'foo-esm' to the conditions array in the resolve hook should
|
|
// allow a ESM to be resolved with that condition.
|
|
{
|
|
const hooks = registerHooks({
|
|
resolve(specifier, context, nextResolve) {
|
|
assert(Array.isArray(context.conditions));
|
|
context.conditions = ['foo-esm', ...context.conditions];
|
|
return nextResolve(specifier, context);
|
|
},
|
|
});
|
|
assert.strictEqual(cjs('foo/third').result, 'foo-esm');
|
|
assert.strictEqual((await esm('foo/third')).result, 'foo-esm');
|
|
hooks.deregister();
|
|
}
|
|
|
|
// Duplicating the 'foo' condition in the resolve hook should not change the result.
|
|
{
|
|
const hooks = registerHooks({
|
|
resolve(specifier, context, nextResolve) {
|
|
assert(Array.isArray(context.conditions));
|
|
context.conditions = ['foo', ...context.conditions, 'foo'];
|
|
return nextResolve(specifier, context);
|
|
},
|
|
});
|
|
assert.strictEqual(cjs('foo/fourth').result, 'foo');
|
|
assert.strictEqual((await esm('foo/fourth')).result, 'foo');
|
|
hooks.deregister();
|
|
}
|