11'use strict' ;
22
33const {
4+ ArrayIsArray,
45 ObjectCreate,
56} = primordials ;
67
@@ -56,8 +57,24 @@ async function initializeLoader() {
5657
5758 const { getOptionValue } = require ( 'internal/options' ) ;
5859 const customLoaders = getOptionValue ( '--experimental-loader' ) ;
60+ const preloadModules = getOptionValue ( '--experimental-import' ) ;
61+ const keyedExportsList = await loadInternalModules ( customLoaders ) ;
5962
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 ;
6178
6279 let cwd ;
6380 try {
@@ -72,17 +89,11 @@ async function initializeLoader() {
7289 const internalEsmLoader = new ESMLoader ( ) ;
7390
7491 // 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 ,
7794 pathToFileURL ( cwd ) . href ,
7895 ObjectCreate ( null ) ,
7996 ) ;
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 ;
8697}
8798
8899exports . loadESM = async function loadESM ( callback ) {
0 commit comments