Skip to content

Commit a93c825

Browse files
committed
process: clarify the pre- and post-condition of esm setup
This patch: - Clarifies the dependency of the ESM loader initialization (`process.cwd()` and the value of `--loader`) in `node.js`. - Moves the initialization of the per-isolate `importModuleDynamically` and `initializeImportMetaObject` callbacks into `node.js` - Moves the initialization of the ESM loader into `prepareUserCodeExecution()` since it potentially involves execution of user code (similar to `--require` for CJS modules). PR-URL: #25530 Reviewed-By: Gus Caplan <[email protected]>
1 parent 5300168 commit a93c825

File tree

2 files changed

+30
-23
lines changed

2 files changed

+30
-23
lines changed

lib/internal/bootstrap/node.js

+24-11
Original file line numberDiff line numberDiff line change
@@ -240,17 +240,6 @@ function startup() {
240240
'DeprecationWarning', 'DEP0062', startup, true);
241241
}
242242

243-
const experimentalModules = getOptionValue('--experimental-modules');
244-
const experimentalVMModules = getOptionValue('--experimental-vm-modules');
245-
if (experimentalModules || experimentalVMModules) {
246-
if (experimentalModules) {
247-
process.emitWarning(
248-
'The ESM module loader is experimental.',
249-
'ExperimentalWarning', undefined);
250-
}
251-
NativeModule.require('internal/process/esm_loader').setup();
252-
}
253-
254243
const { deprecate } = NativeModule.require('internal/util');
255244
{
256245
// Install legacy getters on the `util` binding for typechecking.
@@ -445,6 +434,30 @@ function prepareUserCodeExecution() {
445434
delete process.env.NODE_UNIQUE_ID;
446435
}
447436

437+
const experimentalModules = getOptionValue('--experimental-modules');
438+
const experimentalVMModules = getOptionValue('--experimental-vm-modules');
439+
if (experimentalModules || experimentalVMModules) {
440+
if (experimentalModules) {
441+
process.emitWarning(
442+
'The ESM module loader is experimental.',
443+
'ExperimentalWarning', undefined);
444+
}
445+
446+
const {
447+
setImportModuleDynamicallyCallback,
448+
setInitializeImportMetaObjectCallback
449+
} = internalBinding('module_wrap');
450+
const esm = NativeModule.require('internal/process/esm_loader');
451+
// Setup per-isolate callbacks that locate data or callbacks that we keep
452+
// track of for different ESM modules.
453+
setInitializeImportMetaObjectCallback(esm.initializeImportMetaObject);
454+
setImportModuleDynamicallyCallback(esm.importModuleDynamicallyCallback);
455+
const userLoader = getOptionValue('--loader');
456+
// If --loader is specified, create a loader with user hooks. Otherwise
457+
// create the default loader.
458+
esm.initializeLoader(process.cwd(), userLoader);
459+
}
460+
448461
// For user code, we preload modules if `-r` is passed
449462
const preloadModules = getOptionValue('--require');
450463
if (preloadModules) {

lib/internal/process/esm_loader.js

+6-12
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,6 @@
11
'use strict';
22

33
const {
4-
setImportModuleDynamicallyCallback,
5-
setInitializeImportMetaObjectCallback,
64
callbackMap,
75
} = internalBinding('module_wrap');
86

@@ -15,16 +13,16 @@ const {
1513
ERR_VM_DYNAMIC_IMPORT_CALLBACK_MISSING,
1614
} = require('internal/errors').codes;
1715

18-
function initializeImportMetaObject(wrap, meta) {
16+
exports.initializeImportMetaObject = function(wrap, meta) {
1917
if (callbackMap.has(wrap)) {
2018
const { initializeImportMeta } = callbackMap.get(wrap);
2119
if (initializeImportMeta !== undefined) {
2220
initializeImportMeta(meta, wrapToModuleMap.get(wrap) || wrap);
2321
}
2422
}
25-
}
23+
};
2624

27-
async function importModuleDynamicallyCallback(wrap, specifier) {
25+
exports.importModuleDynamicallyCallback = async function(wrap, specifier) {
2826
if (callbackMap.has(wrap)) {
2927
const { importModuleDynamically } = callbackMap.get(wrap);
3028
if (importModuleDynamically !== undefined) {
@@ -33,10 +31,7 @@ async function importModuleDynamicallyCallback(wrap, specifier) {
3331
}
3432
}
3533
throw new ERR_VM_DYNAMIC_IMPORT_CALLBACK_MISSING();
36-
}
37-
38-
setInitializeImportMetaObjectCallback(initializeImportMetaObject);
39-
setImportModuleDynamicallyCallback(importModuleDynamicallyCallback);
34+
};
4035

4136
let loaderResolve;
4237
exports.loaderPromise = new Promise((resolve, reject) => {
@@ -45,13 +40,12 @@ exports.loaderPromise = new Promise((resolve, reject) => {
4540

4641
exports.ESMLoader = undefined;
4742

48-
exports.setup = function() {
43+
exports.initializeLoader = function(cwd, userLoader) {
4944
let ESMLoader = new Loader();
5045
const loaderPromise = (async () => {
51-
const userLoader = require('internal/options').getOptionValue('--loader');
5246
if (userLoader) {
5347
const hooks = await ESMLoader.import(
54-
userLoader, pathToFileURL(`${process.cwd()}/`).href);
48+
userLoader, pathToFileURL(`${cwd}/`).href);
5549
ESMLoader = new Loader();
5650
ESMLoader.hook(hooks);
5751
exports.ESMLoader = ESMLoader;

0 commit comments

Comments
 (0)