Skip to content

Commit 727569d

Browse files
BridgeARBethGriggs
authored andcommitted
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 <me@gus.host> Reviewed-By: Michaël Zasso <targos@protonmail.com> Reviewed-By: Guy Bedford <guybedford@gmail.com>
1 parent 828a643 commit 727569d

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
@@ -61,13 +61,6 @@ let asyncESM;
6161
let ModuleJob;
6262
let createDynamicModule;
6363

64-
function lazyLoadESM() {
65-
asyncESM = require('internal/process/esm_loader');
66-
ModuleJob = require('internal/modules/esm/module_job');
67-
createDynamicModule = require(
68-
'internal/modules/esm/create_dynamic_module');
69-
}
70-
7164
const {
7265
CHAR_UPPERCASE_A,
7366
CHAR_LOWERCASE_A,
@@ -681,7 +674,6 @@ Module.prototype.load = function(filename) {
681674
this.loaded = true;
682675

683676
if (experimentalModules) {
684-
if (asyncESM === undefined) lazyLoadESM();
685677
const ESMLoader = asyncESM.ESMLoader;
686678
const url = `${pathToFileURL(filename)}`;
687679
const module = ESMLoader.moduleMap.get(url);
@@ -748,7 +740,6 @@ Module.prototype._compile = function(content, filename) {
748740
lineOffset: 0,
749741
displayErrors: true,
750742
importModuleDynamically: experimentalModules ? async (specifier) => {
751-
if (asyncESM === undefined) lazyLoadESM();
752743
const loader = await asyncESM.loaderPromise;
753744
return loader.import(specifier, normalizeReferrerURL(filename));
754745
} : undefined,
@@ -775,7 +766,6 @@ Module.prototype._compile = function(content, filename) {
775766
const { callbackMap } = internalBinding('module_wrap');
776767
callbackMap.set(compiledWrapper, {
777768
importModuleDynamically: async (specifier) => {
778-
if (asyncESM === undefined) lazyLoadESM();
779769
const loader = await asyncESM.loaderPromise;
780770
return loader.import(specifier, normalizeReferrerURL(filename));
781771
}
@@ -855,7 +845,6 @@ Module._extensions['.node'] = function(module, filename) {
855845
};
856846

857847
if (experimentalModules) {
858-
if (asyncESM === undefined) lazyLoadESM();
859848
Module._extensions['.mjs'] = function(module, filename) {
860849
throw new ERR_REQUIRE_ESM(filename);
861850
};
@@ -865,7 +854,6 @@ if (experimentalModules) {
865854
Module.runMain = function() {
866855
// Load the main module--the command line argument.
867856
if (experimentalModules) {
868-
if (asyncESM === undefined) lazyLoadESM();
869857
asyncESM.loaderPromise.then((loader) => {
870858
return loader.import(pathToFileURL(process.argv[1]).pathname);
871859
})
@@ -950,3 +938,11 @@ Module._initPaths();
950938

951939
// Backwards compatibility
952940
Module.Module = Module;
941+
942+
// We have to load the esm things after module.exports!
943+
if (experimentalModules) {
944+
asyncESM = require('internal/process/esm_loader');
945+
ModuleJob = require('internal/modules/esm/module_job');
946+
createDynamicModule = require(
947+
'internal/modules/esm/create_dynamic_module');
948+
}

0 commit comments

Comments
 (0)