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: https://github.com/nodejs/node/pull/25530 Reviewed-By: Gus Caplan <me@gus.host>
This commit is contained in:
parent
5300168296
commit
a93c825d78
@ -240,17 +240,6 @@ function startup() {
|
|||||||
'DeprecationWarning', 'DEP0062', startup, true);
|
'DeprecationWarning', 'DEP0062', startup, true);
|
||||||
}
|
}
|
||||||
|
|
||||||
const experimentalModules = getOptionValue('--experimental-modules');
|
|
||||||
const experimentalVMModules = getOptionValue('--experimental-vm-modules');
|
|
||||||
if (experimentalModules || experimentalVMModules) {
|
|
||||||
if (experimentalModules) {
|
|
||||||
process.emitWarning(
|
|
||||||
'The ESM module loader is experimental.',
|
|
||||||
'ExperimentalWarning', undefined);
|
|
||||||
}
|
|
||||||
NativeModule.require('internal/process/esm_loader').setup();
|
|
||||||
}
|
|
||||||
|
|
||||||
const { deprecate } = NativeModule.require('internal/util');
|
const { deprecate } = NativeModule.require('internal/util');
|
||||||
{
|
{
|
||||||
// Install legacy getters on the `util` binding for typechecking.
|
// Install legacy getters on the `util` binding for typechecking.
|
||||||
@ -445,6 +434,30 @@ function prepareUserCodeExecution() {
|
|||||||
delete process.env.NODE_UNIQUE_ID;
|
delete process.env.NODE_UNIQUE_ID;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
const experimentalModules = getOptionValue('--experimental-modules');
|
||||||
|
const experimentalVMModules = getOptionValue('--experimental-vm-modules');
|
||||||
|
if (experimentalModules || experimentalVMModules) {
|
||||||
|
if (experimentalModules) {
|
||||||
|
process.emitWarning(
|
||||||
|
'The ESM module loader is experimental.',
|
||||||
|
'ExperimentalWarning', undefined);
|
||||||
|
}
|
||||||
|
|
||||||
|
const {
|
||||||
|
setImportModuleDynamicallyCallback,
|
||||||
|
setInitializeImportMetaObjectCallback
|
||||||
|
} = internalBinding('module_wrap');
|
||||||
|
const esm = NativeModule.require('internal/process/esm_loader');
|
||||||
|
// Setup per-isolate callbacks that locate data or callbacks that we keep
|
||||||
|
// track of for different ESM modules.
|
||||||
|
setInitializeImportMetaObjectCallback(esm.initializeImportMetaObject);
|
||||||
|
setImportModuleDynamicallyCallback(esm.importModuleDynamicallyCallback);
|
||||||
|
const userLoader = getOptionValue('--loader');
|
||||||
|
// If --loader is specified, create a loader with user hooks. Otherwise
|
||||||
|
// create the default loader.
|
||||||
|
esm.initializeLoader(process.cwd(), userLoader);
|
||||||
|
}
|
||||||
|
|
||||||
// For user code, we preload modules if `-r` is passed
|
// For user code, we preload modules if `-r` is passed
|
||||||
const preloadModules = getOptionValue('--require');
|
const preloadModules = getOptionValue('--require');
|
||||||
if (preloadModules) {
|
if (preloadModules) {
|
||||||
|
@ -1,8 +1,6 @@
|
|||||||
'use strict';
|
'use strict';
|
||||||
|
|
||||||
const {
|
const {
|
||||||
setImportModuleDynamicallyCallback,
|
|
||||||
setInitializeImportMetaObjectCallback,
|
|
||||||
callbackMap,
|
callbackMap,
|
||||||
} = internalBinding('module_wrap');
|
} = internalBinding('module_wrap');
|
||||||
|
|
||||||
@ -15,16 +13,16 @@ const {
|
|||||||
ERR_VM_DYNAMIC_IMPORT_CALLBACK_MISSING,
|
ERR_VM_DYNAMIC_IMPORT_CALLBACK_MISSING,
|
||||||
} = require('internal/errors').codes;
|
} = require('internal/errors').codes;
|
||||||
|
|
||||||
function initializeImportMetaObject(wrap, meta) {
|
exports.initializeImportMetaObject = function(wrap, meta) {
|
||||||
if (callbackMap.has(wrap)) {
|
if (callbackMap.has(wrap)) {
|
||||||
const { initializeImportMeta } = callbackMap.get(wrap);
|
const { initializeImportMeta } = callbackMap.get(wrap);
|
||||||
if (initializeImportMeta !== undefined) {
|
if (initializeImportMeta !== undefined) {
|
||||||
initializeImportMeta(meta, wrapToModuleMap.get(wrap) || wrap);
|
initializeImportMeta(meta, wrapToModuleMap.get(wrap) || wrap);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
};
|
||||||
|
|
||||||
async function importModuleDynamicallyCallback(wrap, specifier) {
|
exports.importModuleDynamicallyCallback = async function(wrap, specifier) {
|
||||||
if (callbackMap.has(wrap)) {
|
if (callbackMap.has(wrap)) {
|
||||||
const { importModuleDynamically } = callbackMap.get(wrap);
|
const { importModuleDynamically } = callbackMap.get(wrap);
|
||||||
if (importModuleDynamically !== undefined) {
|
if (importModuleDynamically !== undefined) {
|
||||||
@ -33,10 +31,7 @@ async function importModuleDynamicallyCallback(wrap, specifier) {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
throw new ERR_VM_DYNAMIC_IMPORT_CALLBACK_MISSING();
|
throw new ERR_VM_DYNAMIC_IMPORT_CALLBACK_MISSING();
|
||||||
}
|
};
|
||||||
|
|
||||||
setInitializeImportMetaObjectCallback(initializeImportMetaObject);
|
|
||||||
setImportModuleDynamicallyCallback(importModuleDynamicallyCallback);
|
|
||||||
|
|
||||||
let loaderResolve;
|
let loaderResolve;
|
||||||
exports.loaderPromise = new Promise((resolve, reject) => {
|
exports.loaderPromise = new Promise((resolve, reject) => {
|
||||||
@ -45,13 +40,12 @@ exports.loaderPromise = new Promise((resolve, reject) => {
|
|||||||
|
|
||||||
exports.ESMLoader = undefined;
|
exports.ESMLoader = undefined;
|
||||||
|
|
||||||
exports.setup = function() {
|
exports.initializeLoader = function(cwd, userLoader) {
|
||||||
let ESMLoader = new Loader();
|
let ESMLoader = new Loader();
|
||||||
const loaderPromise = (async () => {
|
const loaderPromise = (async () => {
|
||||||
const userLoader = require('internal/options').getOptionValue('--loader');
|
|
||||||
if (userLoader) {
|
if (userLoader) {
|
||||||
const hooks = await ESMLoader.import(
|
const hooks = await ESMLoader.import(
|
||||||
userLoader, pathToFileURL(`${process.cwd()}/`).href);
|
userLoader, pathToFileURL(`${cwd}/`).href);
|
||||||
ESMLoader = new Loader();
|
ESMLoader = new Loader();
|
||||||
ESMLoader.hook(hooks);
|
ESMLoader.hook(hooks);
|
||||||
exports.ESMLoader = ESMLoader;
|
exports.ESMLoader = ESMLoader;
|
||||||
|
Loading…
x
Reference in New Issue
Block a user