module: simpler esm loading

This simplifies loading the experimental modules. Instead of always
checking for them we should eagerly load the functions in case the
experimental modules flag is passed through.

PR-URL: https://github.com/nodejs/node/pull/26974
Reviewed-By: Gus Caplan <me@gus.host>
Reviewed-By: Michaël Zasso <targos@protonmail.com>
Reviewed-By: Guy Bedford <guybedford@gmail.com>
This commit is contained in:
Ruben Bridgewater 2019-03-29 03:21:31 +01:00
parent bb98f27181
commit 86a29356f4
No known key found for this signature in database
GPG Key ID: F07496B3EB3C1762

View File

@ -62,13 +62,6 @@ let asyncESM;
let ModuleJob; let ModuleJob;
let createDynamicModule; let createDynamicModule;
function lazyLoadESM() {
asyncESM = require('internal/process/esm_loader');
ModuleJob = require('internal/modules/esm/module_job');
createDynamicModule = require(
'internal/modules/esm/create_dynamic_module');
}
const { const {
CHAR_UPPERCASE_A, CHAR_UPPERCASE_A,
CHAR_LOWERCASE_A, CHAR_LOWERCASE_A,
@ -705,7 +698,6 @@ Module.prototype.load = function(filename) {
this.loaded = true; this.loaded = true;
if (experimentalModules) { if (experimentalModules) {
if (asyncESM === undefined) lazyLoadESM();
const ESMLoader = asyncESM.ESMLoader; const ESMLoader = asyncESM.ESMLoader;
const url = `${pathToFileURL(filename)}`; const url = `${pathToFileURL(filename)}`;
const module = ESMLoader.moduleMap.get(url); const module = ESMLoader.moduleMap.get(url);
@ -772,7 +764,6 @@ Module.prototype._compile = function(content, filename) {
lineOffset: 0, lineOffset: 0,
displayErrors: true, displayErrors: true,
importModuleDynamically: experimentalModules ? async (specifier) => { importModuleDynamically: experimentalModules ? async (specifier) => {
if (asyncESM === undefined) lazyLoadESM();
const loader = await asyncESM.loaderPromise; const loader = await asyncESM.loaderPromise;
return loader.import(specifier, normalizeReferrerURL(filename)); return loader.import(specifier, normalizeReferrerURL(filename));
} : undefined, } : undefined,
@ -799,7 +790,6 @@ Module.prototype._compile = function(content, filename) {
const { callbackMap } = internalBinding('module_wrap'); const { callbackMap } = internalBinding('module_wrap');
callbackMap.set(compiledWrapper, { callbackMap.set(compiledWrapper, {
importModuleDynamically: async (specifier) => { importModuleDynamically: async (specifier) => {
if (asyncESM === undefined) lazyLoadESM();
const loader = await asyncESM.loaderPromise; const loader = await asyncESM.loaderPromise;
return loader.import(specifier, normalizeReferrerURL(filename)); return loader.import(specifier, normalizeReferrerURL(filename));
} }
@ -879,7 +869,6 @@ Module._extensions['.node'] = function(module, filename) {
}; };
if (experimentalModules) { if (experimentalModules) {
if (asyncESM === undefined) lazyLoadESM();
Module._extensions['.mjs'] = function(module, filename) { Module._extensions['.mjs'] = function(module, filename) {
throw new ERR_REQUIRE_ESM(filename); throw new ERR_REQUIRE_ESM(filename);
}; };
@ -889,7 +878,6 @@ if (experimentalModules) {
Module.runMain = function() { Module.runMain = function() {
// Load the main module--the command line argument. // Load the main module--the command line argument.
if (experimentalModules) { if (experimentalModules) {
if (asyncESM === undefined) lazyLoadESM();
asyncESM.loaderPromise.then((loader) => { asyncESM.loaderPromise.then((loader) => {
return loader.import(pathToFileURL(process.argv[1]).pathname); return loader.import(pathToFileURL(process.argv[1]).pathname);
}) })
@ -974,3 +962,11 @@ Module._initPaths();
// Backwards compatibility // Backwards compatibility
Module.Module = Module; Module.Module = Module;
// We have to load the esm things after module.exports!
if (experimentalModules) {
asyncESM = require('internal/process/esm_loader');
ModuleJob = require('internal/modules/esm/module_job');
createDynamicModule = require(
'internal/modules/esm/create_dynamic_module');
}