Skip to content

Commit 647a37f

Browse files
joyeecheungaddaleax
authored andcommitted
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 <me@gus.host>
1 parent ecd358b commit 647a37f

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
@@ -210,17 +210,6 @@ function startup() {
210210
'DeprecationWarning', 'DEP0062', startup, true);
211211
}
212212

213-
const experimentalModules = getOptionValue('--experimental-modules');
214-
const experimentalVMModules = getOptionValue('--experimental-vm-modules');
215-
if (experimentalModules || experimentalVMModules) {
216-
if (experimentalModules) {
217-
process.emitWarning(
218-
'The ESM module loader is experimental.',
219-
'ExperimentalWarning', undefined);
220-
}
221-
NativeModule.require('internal/process/esm_loader').setup();
222-
}
223-
224213
const { deprecate } = NativeModule.require('internal/util');
225214
{
226215
// Install legacy getters on the `util` binding for typechecking.
@@ -451,6 +440,30 @@ function prepareUserCodeExecution() {
451440
delete process.env.NODE_UNIQUE_ID;
452441
}
453442

443+
const experimentalModules = getOptionValue('--experimental-modules');
444+
const experimentalVMModules = getOptionValue('--experimental-vm-modules');
445+
if (experimentalModules || experimentalVMModules) {
446+
if (experimentalModules) {
447+
process.emitWarning(
448+
'The ESM module loader is experimental.',
449+
'ExperimentalWarning', undefined);
450+
}
451+
452+
const {
453+
setImportModuleDynamicallyCallback,
454+
setInitializeImportMetaObjectCallback
455+
} = internalBinding('module_wrap');
456+
const esm = NativeModule.require('internal/process/esm_loader');
457+
// Setup per-isolate callbacks that locate data or callbacks that we keep
458+
// track of for different ESM modules.
459+
setInitializeImportMetaObjectCallback(esm.initializeImportMetaObject);
460+
setImportModuleDynamicallyCallback(esm.importModuleDynamicallyCallback);
461+
const userLoader = getOptionValue('--loader');
462+
// If --loader is specified, create a loader with user hooks. Otherwise
463+
// create the default loader.
464+
esm.initializeLoader(process.cwd(), userLoader);
465+
}
466+
454467
// For user code, we preload modules if `-r` is passed
455468
const preloadModules = getOptionValue('--require');
456469
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)