mirror of
https://github.com/nodejs/node.git
synced 2026-11-09 15:11:11 -05:00
- For imported CJS, if it's not customized by asynchronous hooks, make sure it won't use the quirky re-invented require in all cases. - When the imported CJS module is customized by synchronous hooks, in the synthetic module evalutation step, avoid calling the respective default step again. - Make the branching of loadCJSModuleWithModuleLoad() and loadCJSModuleWithSpecialRequire() more explicit, and fold the tentative fs read in the 'commonjs' translator into the share createCJSModuleWrap() helper instead of checking it twice in the same path. Signed-off-by: Joyee Cheung <joyeec9h3@gmail.com> PR-URL: https://github.com/nodejs/node/pull/62920 Fixes: https://github.com/nodejs/node/issues/63060 Reviewed-By: Paolo Insogna <paolo@cowtech.it> Reviewed-By: Matteo Collina <matteo.collina@gmail.com> Reviewed-By: Gürgün Dayıoğlu <hey@gurgun.day>
42 lines
1.2 KiB
JavaScript
42 lines
1.2 KiB
JavaScript
'use strict';
|
|
// Test that imported CJS loaded from sync hooks are handled correctly and
|
|
// won't invoke the CJS loading steps again (in which case it would throw
|
|
// because the source is not on disk).
|
|
|
|
const common = require('../common');
|
|
const assert = require('assert');
|
|
const { registerHooks } = require('module');
|
|
|
|
const virtualModules = new Map([
|
|
['virtual:shared', 'module.exports.greet = (name) => "hello " + name;'],
|
|
['virtual:entry', 'const { greet } = require("virtual:shared");\nmodule.exports = greet("world");'],
|
|
]);
|
|
|
|
const hook = registerHooks({
|
|
resolve: common.mustCall((specifier, context, nextResolve) => {
|
|
if (virtualModules.has(specifier)) {
|
|
return {
|
|
url: specifier,
|
|
format: 'commonjs',
|
|
shortCircuit: true,
|
|
};
|
|
}
|
|
return nextResolve(specifier, context);
|
|
}, 2),
|
|
load: common.mustCall((url, context, nextLoad) => {
|
|
if (virtualModules.has(url)) {
|
|
return {
|
|
format: 'commonjs',
|
|
source: virtualModules.get(url),
|
|
shortCircuit: true,
|
|
};
|
|
}
|
|
return nextLoad(url, context);
|
|
}, 2),
|
|
});
|
|
|
|
import('virtual:entry').then(common.mustCall((ns) => {
|
|
assert.strictEqual(ns.default, 'hello world');
|
|
hook.deregister();
|
|
}));
|