Skip to content

Commit 86a2935

Browse files
committedMar 31, 2019
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: #26974 Reviewed-By: Gus Caplan <[email protected]> Reviewed-By: Michaël Zasso <[email protected]> Reviewed-By: Guy Bedford <[email protected]>
1 parent bb98f27 commit 86a2935

File tree

1 file changed

+8
-12
lines changed

1 file changed

+8
-12
lines changed
 

‎lib/internal/modules/cjs/loader.js

+8-12
Original file line numberDiff line numberDiff line change
@@ -62,13 +62,6 @@ let asyncESM;
6262
let ModuleJob;
6363
let createDynamicModule;
6464

65-
function lazyLoadESM() {
66-
asyncESM = require('internal/process/esm_loader');
67-
ModuleJob = require('internal/modules/esm/module_job');
68-
createDynamicModule = require(
69-
'internal/modules/esm/create_dynamic_module');
70-
}
71-
7265
const {
7366
CHAR_UPPERCASE_A,
7467
CHAR_LOWERCASE_A,
@@ -705,7 +698,6 @@ Module.prototype.load = function(filename) {
705698
this.loaded = true;
706699

707700
if (experimentalModules) {
708-
if (asyncESM === undefined) lazyLoadESM();
709701
const ESMLoader = asyncESM.ESMLoader;
710702
const url = `${pathToFileURL(filename)}`;
711703
const module = ESMLoader.moduleMap.get(url);
@@ -772,7 +764,6 @@ Module.prototype._compile = function(content, filename) {
772764
lineOffset: 0,
773765
displayErrors: true,
774766
importModuleDynamically: experimentalModules ? async (specifier) => {
775-
if (asyncESM === undefined) lazyLoadESM();
776767
const loader = await asyncESM.loaderPromise;
777768
return loader.import(specifier, normalizeReferrerURL(filename));
778769
} : undefined,
@@ -799,7 +790,6 @@ Module.prototype._compile = function(content, filename) {
799790
const { callbackMap } = internalBinding('module_wrap');
800791
callbackMap.set(compiledWrapper, {
801792
importModuleDynamically: async (specifier) => {
802-
if (asyncESM === undefined) lazyLoadESM();
803793
const loader = await asyncESM.loaderPromise;
804794
return loader.import(specifier, normalizeReferrerURL(filename));
805795
}
@@ -879,7 +869,6 @@ Module._extensions['.node'] = function(module, filename) {
879869
};
880870

881871
if (experimentalModules) {
882-
if (asyncESM === undefined) lazyLoadESM();
883872
Module._extensions['.mjs'] = function(module, filename) {
884873
throw new ERR_REQUIRE_ESM(filename);
885874
};
@@ -889,7 +878,6 @@ if (experimentalModules) {
889878
Module.runMain = function() {
890879
// Load the main module--the command line argument.
891880
if (experimentalModules) {
892-
if (asyncESM === undefined) lazyLoadESM();
893881
asyncESM.loaderPromise.then((loader) => {
894882
return loader.import(pathToFileURL(process.argv[1]).pathname);
895883
})
@@ -974,3 +962,11 @@ Module._initPaths();
974962

975963
// Backwards compatibility
976964
Module.Module = Module;
965+
966+
// We have to load the esm things after module.exports!
967+
if (experimentalModules) {
968+
asyncESM = require('internal/process/esm_loader');
969+
ModuleJob = require('internal/modules/esm/module_job');
970+
createDynamicModule = require(
971+
'internal/modules/esm/create_dynamic_module');
972+
}

0 commit comments

Comments
 (0)