GeezerVison is showing github.com — esm: link modules synchronously when no async loader hooks are used · nodejs/node@2fafe4c
File treeExpand file treeCollapse file tree Open diff view settings Expand file treeCollapse file tree Open diff view settings Collapse file Copy file name to clipboardExpand all lines: lib/internal/modules/esm/loader.js +13-5Lines changed: 13 additions & 5 deletions | Original file line number | Diff line number | Diff line change |
|---|
@@ -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 | /** | |
Collapse file Copy file name to clipboardExpand all lines: lib/internal/modules/esm/module_job.js +5-5Lines changed: 5 additions & 5 deletions | Original file line number | Diff line number | Diff line change |
|---|
@@ -37,6 +37,7 @@ const { | 37 | 37 | }, | 38 | 38 | } = internalBinding('util'); | 39 | 39 | const { decorateErrorStack, kEmptyObject } = require('internal/util'); | | 40 | +const { isPromise } = require('internal/util/types'); | 40 | 41 | const { | 41 | 42 | getSourceMapsSupport, | 42 | 43 | } = require('internal/source_map/source_map_cache'); | @@ -138,12 +139,11 @@ class ModuleJob extends ModuleJobBase { | 138 | 139 | this.#loader = loader; | 139 | 140 |
| 140 | 141 | // Expose the promise to the ModuleWrap directly for linking below. | 141 | | - if (isForRequireInImportedCJS) { | 142 | | - this.module = moduleOrModulePromise; | 143 | | - assert(this.module instanceof ModuleWrap); | 144 | | - this.modulePromise = PromiseResolve(this.module); | 145 | | - } else { | | 142 | + if (isPromise(moduleOrModulePromise)) { | 146 | 143 | this.modulePromise = moduleOrModulePromise; | | 144 | + } else { | | 145 | + this.module = moduleOrModulePromise; | | 146 | + this.modulePromise = PromiseResolve(moduleOrModulePromise); | 147 | 147 | } | 148 | 148 |
| 149 | 149 | if (this.phase === kEvaluationPhase) { | |
Collapse file Copy file name to clipboardExpand all lines: test/es-module/test-esm-error-cache.js +3-1Lines changed: 3 additions & 1 deletion | Original file line number | Diff line number | Diff line change |
|---|
@@ -19,7 +19,9 @@ let error; | 19 | 19 | await assert.rejects( | 20 | 20 | () => import(file), | 21 | 21 | (e) => { | 22 | | - assert.strictEqual(error, e); | | 22 | + // The module may be compiled again and a new SyntaxError would be thrown but | | 23 | + // with the same content. | | 24 | + assert.deepStrictEqual(error, e); | 23 | 25 | return true; | 24 | 26 | } | 25 | 27 | ); | |
Collapse file Copy file name to clipboardExpand all lines: test/es-module/test-esm-import-attributes-errors.js +2-2Lines changed: 2 additions & 2 deletions | Original file line number | Diff line number | Diff line change |
|---|
@@ -28,7 +28,7 @@ async function test() { | 28 | 28 |
| 29 | 29 | await rejects( | 30 | 30 | import(jsModuleDataUrl, { with: { type: 'json', other: 'unsupported' } }), | 31 | | - { code: 'ERR_IMPORT_ATTRIBUTE_TYPE_INCOMPATIBLE' } | | 31 | + { code: 'ERR_IMPORT_ATTRIBUTE_UNSUPPORTED' } | 32 | 32 | ); | 33 | 33 |
| 34 | 34 | await rejects( | @@ -48,7 +48,7 @@ async function test() { | 48 | 48 |
| 49 | 49 | await rejects( | 50 | 50 | import(jsonModuleDataUrl, { with: { foo: 'bar' } }), | 51 | | - { code: 'ERR_IMPORT_ATTRIBUTE_MISSING' } | | 51 | + { code: 'ERR_IMPORT_ATTRIBUTE_UNSUPPORTED' } | 52 | 52 | ); | 53 | 53 |
| 54 | 54 | await rejects( | |
Collapse file Copy file name to clipboardExpand all lines: test/es-module/test-esm-import-attributes-errors.mjs +2-2Lines changed: 2 additions & 2 deletions | Original file line number | Diff line number | Diff line change |
|---|
@@ -23,7 +23,7 @@ await rejects( | 23 | 23 |
| 24 | 24 | await rejects( | 25 | 25 | import(jsModuleDataUrl, { with: { type: 'json', other: 'unsupported' } }), | 26 | | - { code: 'ERR_IMPORT_ATTRIBUTE_TYPE_INCOMPATIBLE' } | | 26 | + { code: 'ERR_IMPORT_ATTRIBUTE_UNSUPPORTED' } | 27 | 27 | ); | 28 | 28 |
| 29 | 29 | await rejects( | @@ -43,7 +43,7 @@ await rejects( | 43 | 43 |
| 44 | 44 | await rejects( | 45 | 45 | import(jsonModuleDataUrl, { with: { foo: 'bar' } }), | 46 | | - { code: 'ERR_IMPORT_ATTRIBUTE_MISSING' } | | 46 | + { code: 'ERR_IMPORT_ATTRIBUTE_UNSUPPORTED' } | 47 | 47 | ); | 48 | 48 |
| 49 | 49 | await rejects( | |
|