Skip to content

Commit 3c92926

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> Signed-off-by: Beth Griggs <Bethany.Griggs@uk.ibm.com>
1 parent 8bd7909 commit 3c92926

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,
@@ -673,7 +666,6 @@ Module.prototype.load = function(filename) {
673666
this.loaded = true;
674667

675668
if (experimentalModules) {
676-
if (asyncESM === undefined) lazyLoadESM();
677669
const ESMLoader = asyncESM.ESMLoader;
678670
const url = `${pathToFileURL(filename)}`;
679671
const module = ESMLoader.moduleMap.get(url);
@@ -740,7 +732,6 @@ Module.prototype._compile = function(content, filename) {
740732
lineOffset: 0,
741733
displayErrors: true,
742734
importModuleDynamically: experimentalModules ? async (specifier) => {
743-
if (asyncESM === undefined) lazyLoadESM();
744735
const loader = await asyncESM.loaderPromise;
745736
return loader.import(specifier, normalizeReferrerURL(filename));
746737
} : undefined,
@@ -767,7 +758,6 @@ Module.prototype._compile = function(content, filename) {
767758
const { callbackMap } = internalBinding('module_wrap');
768759
callbackMap.set(compiledWrapper, {
769760
importModuleDynamically: async (specifier) => {
770-
if (asyncESM === undefined) lazyLoadESM();
771761
const loader = await asyncESM.loaderPromise;
772762
return loader.import(specifier, normalizeReferrerURL(filename));
773763
}
@@ -847,7 +837,6 @@ Module._extensions['.node'] = function(module, filename) {
847837
};
848838

849839
if (experimentalModules) {
850-
if (asyncESM === undefined) lazyLoadESM();
851840
Module._extensions['.mjs'] = function(module, filename) {
852841
throw new ERR_REQUIRE_ESM(filename);
853842
};
@@ -857,7 +846,6 @@ if (experimentalModules) {
857846
Module.runMain = function() {
858847
// Load the main module--the command line argument.
859848
if (experimentalModules) {
860-
if (asyncESM === undefined) lazyLoadESM();
861849
asyncESM.loaderPromise.then((loader) => {
862850
return loader.import(pathToFileURL(process.argv[1]).pathname);
863851
})
@@ -940,3 +928,11 @@ Module._initPaths();
940928

941929
// Backwards compatibility
942930
Module.Module = Module;
931+
932+
// We have to load the esm things after module.exports!
933+
if (experimentalModules) {
934+
asyncESM = require('internal/process/esm_loader');
935+
ModuleJob = require('internal/modules/esm/module_job');
936+
createDynamicModule = require(
937+
'internal/modules/esm/create_dynamic_module');
938+
}

0 commit comments

Comments
 (0)