22
33const {
44 ArrayIsArray,
5- ArrayPrototypePush,
65 PromisePrototypeThen,
7- ReflectApply,
8- SafeMap,
96 SafeSet,
107 SafeWeakMap,
118 ObjectFreeze,
@@ -17,7 +14,7 @@ const {
1714} = require ( 'internal/errors' ) . codes ;
1815const { getOptionValue } = require ( 'internal/options' ) ;
1916const { pathToFileURL } = require ( 'internal/url' ) ;
20- const { kEmptyObject, createDeferredPromise } = require ( 'internal/util' ) ;
17+ const { kEmptyObject } = require ( 'internal/util' ) ;
2118const {
2219 setImportModuleDynamicallyCallback,
2320 setInitializeImportMetaObjectCallback,
@@ -95,14 +92,6 @@ async function importModuleDynamicallyCallback(wrap, specifier, assertions) {
9592}
9693
9794function initializeESM ( ) {
98- const { setESMLoader } = require ( 'internal/process/esm_loader' ) ;
99- // Minimal mock for letting `--require` work before we have any actual ESM loader.
100- setESMLoader ( { __proto__ : null , cjsCache : new SafeMap ( ) , import ( ) {
101- const { promise, resolve, reject } = createDeferredPromise ( ) ;
102- ArrayPrototypePush ( this . importRequests , { arguments : arguments , resolve, reject } ) ;
103- return promise ;
104- } , importRequests : [ ] } ) ;
105-
10695 initializeDefaultConditions ( ) ;
10796 // Setup per-isolate callbacks that locate data or callbacks that we keep
10897 // track of for different ESM modules.
@@ -126,22 +115,10 @@ async function initializeHooks() {
126115 const hooks = new Hooks ( ) ;
127116
128117 const { DefaultModuleLoader } = require ( 'internal/modules/esm/loader' ) ;
129- const { esmLoader , setESMLoader } = require ( 'internal/process/esm_loader' ) ;
118+ const { init } = require ( 'internal/process/esm_loader' ) ;
130119 class ModuleLoader extends DefaultModuleLoader {
131- constructor ( ) {
132- super ( ) ;
133- for ( const { 0 : key , 1 : value } of esmLoader . cjsCache ) {
134- // Getting back the values from the mocked loader.
135- this . cjsCache . set ( key , value ) ;
136- }
137- for ( let i = 0 ; i < esmLoader . importRequests . length ; i ++ ) {
138- PromisePrototypeThen (
139- ReflectApply ( this . import , this , esmLoader . importRequests [ i ] . arguments ) ,
140- esmLoader . importRequests [ i ] . resolve ,
141- esmLoader . importRequests [ i ] . reject ,
142- ) ;
143- }
144- }
120+ // eslint-disable-next-line no-useless-constructor
121+ constructor ( ) { super ( ) ; }
145122
146123 loaderType = 'internal' ;
147124 async #getModuleJob( specifier , parentURL , importAssertions ) {
@@ -166,8 +143,6 @@ async function initializeHooks() {
166143 }
167144 const privateModuleLoader = new ModuleLoader ( ) ;
168145
169- setESMLoader ( privateModuleLoader ) ;
170-
171146 const parentURL = pathToFileURL ( cwd ) . href ;
172147
173148 for ( let i = 0 ; i < customLoaderPaths . length ; i ++ ) {
@@ -185,6 +160,8 @@ async function initializeHooks() {
185160
186161 const preloadScripts = hooks . initializeGlobalPreload ( ) ;
187162
163+ init ( privateModuleLoader ) ;
164+
188165 return { __proto__ : null , hooks, preloadScripts } ;
189166}
190167
0 commit comments