@@ -65,6 +65,8 @@ let debug = require('internal/util/debuglog').debuglog('esm', (fn) => {
6565 debug = fn ;
6666} ) ;
6767
68+ const { isPromise } = require ( 'internal/util/types' ) ;
69+
6870/**
6971 * @typedef {import('./hooks.js').HooksProxy } HooksProxy
7072 * @typedef {import('./module_job.js').ModuleJobBase } ModuleJobBase
@@ -592,15 +594,21 @@ class ModuleLoader {
592594
593595 /**
594596 * Load a module and translate it into a ModuleWrap for ordinary imported ESM.
595- * This is run asynchronously.
597+ * This may be run asynchronously if there are asynchronous module loader hooks registered .
596598 * @param {string } url URL of the module to be translated.
597599 * @param {object } loadContext See {@link load}
598600 * @param {boolean } isMain Whether the module to be translated is the entry point.
599- * @returns {Promise<ModuleWrap> }
601+ * @returns {Promise<ModuleWrap>|ModuleWrap }
600602 */
601- async loadAndTranslate ( url , loadContext , isMain ) {
602- const { format, source } = await this . load ( url , loadContext ) ;
603- return this . #translate( url , format , source , isMain ) ;
603+ loadAndTranslate ( url , loadContext , isMain ) {
604+ const maybePromise = this . load ( url , loadContext ) ;
605+ const afterLoad = ( { format, source } ) => {
606+ return this . #translate( url , format , source , isMain ) ;
607+ } ;
608+ if ( isPromise ( maybePromise ) ) {
609+ return maybePromise . then ( afterLoad ) ;
610+ }
611+ return afterLoad ( maybePromise ) ;
604612 }
605613
606614 /**
0 commit comments