Skip to content

Commit 3aab6ce

Browse files
BridgeARMylesBorins
authored andcommitted
lib: lazy load necessary loaders
PR-URL: #20567 Reviewed-By: Gus Caplan <me@gus.host> Reviewed-By: Matteo Collina <matteo.collina@gmail.com> Reviewed-By: James M Snell <jasnell@gmail.com> Reviewed-By: Jeremiah Senkpiel <fishrock123@rocketmail.com>
1 parent 486ac23 commit 3aab6ce

1 file changed

Lines changed: 19 additions & 8 deletions

File tree

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

Lines changed: 19 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -23,8 +23,6 @@
2323

2424
const{ NativeModule }=require('internal/bootstrap/loaders');
2525
constutil=require('util');
26-
const{ decorateErrorStack }=require('internal/util');
27-
const{ getURLFromFilePath }=require('internal/url');
2826
constvm=require('vm');
2927
constassert=require('assert').ok;
3028
constfs=require('fs');
@@ -53,11 +51,21 @@ const {
5351

5452
module.exports=Module;
5553

56-
// these are below module.exports for the circular reference
57-
constasyncESM=require('internal/process/esm_loader');
58-
constModuleJob=require('internal/modules/esm/module_job');
59-
constcreateDynamicModule=require(
60-
'internal/modules/esm/create_dynamic_module');
54+
letasyncESM;
55+
letModuleJob;
56+
letcreateDynamicModule;
57+
letgetURLFromFilePath;
58+
letdecorateErrorStack;
59+
60+
functionlazyLoadESM(){
61+
asyncESM=require('internal/process/esm_loader');
62+
ModuleJob=require('internal/modules/esm/module_job');
63+
createDynamicModule=require(
64+
'internal/modules/esm/create_dynamic_module');
65+
decorateErrorStack=require('internal/util').decorateErrorStack;
66+
getURLFromFilePath=require('internal/url').getURLFromFilePath;
67+
}
68+
6169
const{
6270
CHAR_UPPERCASE_A,
6371
CHAR_LOWERCASE_A,
@@ -497,6 +505,7 @@ Module._load = function(request, parent, isMain) {
497505
}
498506

499507
if(experimentalModules&&isMain){
508+
if(asyncESM===undefined)lazyLoadESM();
500509
asyncESM.loaderPromise.then((loader)=>{
501510
returnloader.import(getURLFromFilePath(request).pathname);
502511
})
@@ -604,6 +613,7 @@ Module.prototype.load = function(filename) {
604613
this.loaded=true;
605614

606615
if(experimentalModules){
616+
if(asyncESM===undefined)lazyLoadESM();
607617
constESMLoader=asyncESM.ESMLoader;
608618
consturl=getURLFromFilePath(filename);
609619
consturlString=`${url}`;
@@ -722,6 +732,7 @@ Module._extensions['.node'] = function(module, filename) {
722732
};
723733

724734
if(experimentalModules){
735+
if(asyncESM===undefined)lazyLoadESM();
725736
Module._extensions['.mjs']=function(module,filename){
726737
thrownewERR_REQUIRE_ESM(filename);
727738
};
@@ -797,5 +808,5 @@ Module._preloadModules = function(requests) {
797808

798809
Module._initPaths();
799810

800-
// backwards compatibility
811+
// Backwards compatibility
801812
Module.Module=Module;

0 commit comments

Comments
 (0)