1
1
'use strict' ;
2
2
3
3
const {
4
+ ArrayIsArray,
4
5
ObjectCreate,
5
6
} = primordials ;
6
7
@@ -56,8 +57,24 @@ async function initializeLoader() {
56
57
57
58
const { getOptionValue } = require ( 'internal/options' ) ;
58
59
const customLoaders = getOptionValue ( '--experimental-loader' ) ;
60
+ const preloadModules = getOptionValue ( '--experimental-import' ) ;
61
+ const keyedExportsList = await loadInternalModules ( customLoaders ) ;
59
62
60
- if ( customLoaders . length === 0 ) return ;
63
+ // Hooks must then be added to external/public loader
64
+ // (so they're triggered in userland)
65
+ await esmLoader . addCustomLoaders ( keyedExportsList ) ;
66
+
67
+ // Preload after loaders are added so they can be used
68
+ if ( preloadModules ?. length ) {
69
+ await loadInternalModules ( preloadModules ) ;
70
+ }
71
+
72
+ isESMInitialized = true ;
73
+ }
74
+
75
+ async function loadInternalModules ( requests ) {
76
+ if ( ! ArrayIsArray ( requests ) || requests . length === 0 )
77
+ return ;
61
78
62
79
let cwd ;
63
80
try {
@@ -72,17 +89,11 @@ async function initializeLoader() {
72
89
const internalEsmLoader = new ESMLoader ( ) ;
73
90
74
91
// Importation must be handled by internal loader to avoid poluting userland
75
- const keyedExportsList = await internalEsmLoader . import (
76
- customLoaders ,
92
+ return internalEsmLoader . import (
93
+ requests ,
77
94
pathToFileURL ( cwd ) . href ,
78
95
ObjectCreate ( null ) ,
79
96
) ;
80
-
81
- // Hooks must then be added to external/public loader
82
- // (so they're triggered in userland)
83
- await esmLoader . addCustomLoaders ( keyedExportsList ) ;
84
-
85
- isESMInitialized = true ;
86
97
}
87
98
88
99
exports . loadESM = async function loadESM ( callback ) {
0 commit comments