Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

process: clarify the pre- and post-condition of esm setup #25530

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
35 changes: 24 additions & 11 deletions lib/internal/bootstrap/node.js
Original file line number Diff line number Diff line change
Expand Up @@ -210,17 +210,6 @@ function startup() {
'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');
{
// Install legacy getters on the `util` binding for typechecking.
Expand Down Expand Up @@ -427,6 +416,30 @@ function prepareUserCodeExecution() {
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
const preloadModules = getOptionValue('--require');
if (preloadModules) {
Expand Down
18 changes: 6 additions & 12 deletions lib/internal/process/esm_loader.js
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
'use strict';

const {
setImportModuleDynamicallyCallback,
setInitializeImportMetaObjectCallback,
callbackMap,
} = internalBinding('module_wrap');

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

function initializeImportMetaObject(wrap, meta) {
exports.initializeImportMetaObject = function(wrap, meta) {
if (callbackMap.has(wrap)) {
const { initializeImportMeta } = callbackMap.get(wrap);
if (initializeImportMeta !== undefined) {
initializeImportMeta(meta, wrapToModuleMap.get(wrap) || wrap);
}
}
}
};

async function importModuleDynamicallyCallback(wrap, specifier) {
exports.importModuleDynamicallyCallback = async function(wrap, specifier) {
if (callbackMap.has(wrap)) {
const { importModuleDynamically } = callbackMap.get(wrap);
if (importModuleDynamically !== undefined) {
Expand All @@ -33,10 +31,7 @@ async function importModuleDynamicallyCallback(wrap, specifier) {
}
}
throw new ERR_VM_DYNAMIC_IMPORT_CALLBACK_MISSING();
}

setInitializeImportMetaObjectCallback(initializeImportMetaObject);
setImportModuleDynamicallyCallback(importModuleDynamicallyCallback);
};

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

exports.ESMLoader = undefined;

exports.setup = function() {
exports.initializeLoader = function(cwd, userLoader) {
let ESMLoader = new Loader();
const loaderPromise = (async () => {
const userLoader = require('internal/options').getOptionValue('--loader');
if (userLoader) {
const hooks = await ESMLoader.import(
userLoader, pathToFileURL(`${process.cwd()}/`).href);
userLoader, pathToFileURL(`${cwd}/`).href);
ESMLoader = new Loader();
ESMLoader.hook(hooks);
exports.ESMLoader = ESMLoader;
Expand Down