From 3c9292642df8006a0e1a4106acffba84c84d7779 Mon Sep 17 00:00:00 2001 From: Ruben Bridgewater Date: Fri, 29 Mar 2019 03:21:31 +0100 Subject: [PATCH] module: simpler esm loading MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 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 Reviewed-By: Michaƫl Zasso Reviewed-By: Guy Bedford Signed-off-by: Beth Griggs --- lib/internal/modules/cjs/loader.js | 20 ++++++++------------ 1 file changed, 8 insertions(+), 12 deletions(-) diff --git a/lib/internal/modules/cjs/loader.js b/lib/internal/modules/cjs/loader.js index fcbe16ca4893f8..7564f8176bf91e 100644 --- a/lib/internal/modules/cjs/loader.js +++ b/lib/internal/modules/cjs/loader.js @@ -61,13 +61,6 @@ let asyncESM; let ModuleJob; 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 { CHAR_UPPERCASE_A, CHAR_LOWERCASE_A, @@ -673,7 +666,6 @@ Module.prototype.load = function(filename) { this.loaded = true; if (experimentalModules) { - if (asyncESM === undefined) lazyLoadESM(); const ESMLoader = asyncESM.ESMLoader; const url = `${pathToFileURL(filename)}`; const module = ESMLoader.moduleMap.get(url); @@ -740,7 +732,6 @@ Module.prototype._compile = function(content, filename) { lineOffset: 0, displayErrors: true, importModuleDynamically: experimentalModules ? async (specifier) => { - if (asyncESM === undefined) lazyLoadESM(); const loader = await asyncESM.loaderPromise; return loader.import(specifier, normalizeReferrerURL(filename)); } : undefined, @@ -767,7 +758,6 @@ Module.prototype._compile = function(content, filename) { const { callbackMap } = internalBinding('module_wrap'); callbackMap.set(compiledWrapper, { importModuleDynamically: async (specifier) => { - if (asyncESM === undefined) lazyLoadESM(); const loader = await asyncESM.loaderPromise; return loader.import(specifier, normalizeReferrerURL(filename)); } @@ -847,7 +837,6 @@ Module._extensions['.node'] = function(module, filename) { }; if (experimentalModules) { - if (asyncESM === undefined) lazyLoadESM(); Module._extensions['.mjs'] = function(module, filename) { throw new ERR_REQUIRE_ESM(filename); }; @@ -857,7 +846,6 @@ if (experimentalModules) { Module.runMain = function() { // Load the main module--the command line argument. if (experimentalModules) { - if (asyncESM === undefined) lazyLoadESM(); asyncESM.loaderPromise.then((loader) => { return loader.import(pathToFileURL(process.argv[1]).pathname); }) @@ -940,3 +928,11 @@ Module._initPaths(); // Backwards compatibility 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'); +}