@@ -65,6 +65,8 @@ let debug = require('internal/util/debuglog').debuglog('esm', (fn) => {
|
65 | 65 | debug = fn; |
66 | 66 | }); |
67 | 67 | |
| 68 | +const { isPromise } = require('internal/util/types'); |
| 69 | + |
68 | 70 | /** |
69 | 71 | * @typedef {import('./hooks.js').HooksProxy} HooksProxy |
70 | 72 | * @typedef {import('./module_job.js').ModuleJobBase} ModuleJobBase |
@@ -592,15 +594,21 @@ class ModuleLoader {
|
592 | 594 | |
593 | 595 | /** |
594 | 596 | * 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. |
596 | 598 | * @param {string} url URL of the module to be translated. |
597 | 599 | * @param {object} loadContext See {@link load} |
598 | 600 | * @param {boolean} isMain Whether the module to be translated is the entry point. |
599 | | - * @returns {Promise<ModuleWrap>} |
| 601 | + * @returns {Promise<ModuleWrap>|ModuleWrap} |
600 | 602 | */ |
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); |
604 | 612 | } |
605 | 613 | |
606 | 614 | /** |
|