22
33const {
44 ArrayIsArray,
5- ObjectCreate ,
5+ ArrayPrototypePushApply ,
66} = primordials ;
77
88const {
@@ -13,6 +13,7 @@ const {
1313 hasUncaughtExceptionCaptureCallback,
1414} = require ( 'internal/process/execution' ) ;
1515const { pathToFileURL } = require ( 'internal/url' ) ;
16+ const { kEmptyObject } = require ( 'internal/util' ) ;
1617const {
1718 getModuleFromWrap,
1819} = require ( 'internal/vm/module' ) ;
@@ -58,15 +59,41 @@ async function initializeLoader() {
5859 const { getOptionValue } = require ( 'internal/options' ) ;
5960 const customLoaders = getOptionValue ( '--experimental-loader' ) ;
6061 const preloadModules = getOptionValue ( '--import' ) ;
61- const loaders = await loadModulesInIsolation ( customLoaders ) ;
62+
63+ let cwd ;
64+ try {
65+ cwd = process . cwd ( ) + '/' ;
66+ } catch {
67+ cwd = '/' ;
68+ }
69+
70+ const internalEsmLoader = new ESMLoader ( ) ;
71+ const allLoaders = [ ] ;
72+
73+ const parentUrl = pathToFileURL ( cwd ) . href ;
74+
75+ for ( let i = 0 ; i < customLoaders . length ; i ++ ) {
76+ const customLoader = customLoaders [ i ] ;
77+
78+ // Importation must be handled by internal loader to avoid polluting user-land
79+ const keyedExportsSublist = await internalEsmLoader . import (
80+ [ customLoader ] ,
81+ parentUrl ,
82+ kEmptyObject ,
83+ ) ;
84+
85+ internalEsmLoader . addCustomLoaders ( keyedExportsSublist ) ;
86+ ArrayPrototypePushApply ( allLoaders , keyedExportsSublist ) ;
87+ }
6288
6389 // Hooks must then be added to external/public loader
6490 // (so they're triggered in userland)
65- esmLoader . addCustomLoaders ( loaders ) ;
91+ esmLoader . addCustomLoaders ( allLoaders ) ;
92+ esmLoader . preload ( ) ;
6693
6794 // Preload after loaders are added so they can be used
6895 if ( preloadModules ?. length ) {
69- await loadModulesInIsolation ( preloadModules , loaders ) ;
96+ await loadModulesInIsolation ( preloadModules , allLoaders ) ;
7097 }
7198
7299 isESMInitialized = true ;
@@ -79,20 +106,21 @@ function loadModulesInIsolation(specifiers, loaders = []) {
79106 try {
80107 cwd = process . cwd ( ) + '/' ;
81108 } catch {
82- cwd = 'file:// /' ;
109+ cwd = '/' ;
83110 }
84111
85112 // A separate loader instance is necessary to avoid cross-contamination
86113 // between internal Node.js and userland. For example, a module with internal
87114 // state (such as a counter) should be independent.
88115 const internalEsmLoader = new ESMLoader ( ) ;
89116 internalEsmLoader . addCustomLoaders ( loaders ) ;
117+ internalEsmLoader . preload ( ) ;
90118
91119 // Importation must be handled by internal loader to avoid poluting userland
92120 return internalEsmLoader . import (
93121 specifiers ,
94122 pathToFileURL ( cwd ) . href ,
95- ObjectCreate ( null ) ,
123+ kEmptyObject ,
96124 ) ;
97125}
98126
0 commit comments