From 90b634a5a5bd1b4131b297fedc366777f174a091 Mon Sep 17 00:00:00 2001 From: Jacob Smith <3012099+JakobJingleheimer@users.noreply.github.com> Date: Sat, 18 Jun 2022 19:22:57 +0200 Subject: [PATCH] esm: convert `resolve` hook to synchronous PR-URL: https://github.com/nodejs/node/pull/43363 Reviewed-By: Antoine du Hamel Reviewed-By: Geoffrey Booth --- doc/api/esm.md | 29 ++++-- .../modules/esm/initialize_import_meta.js | 26 ++--- lib/internal/modules/esm/loader.js | 95 +++++++++++++------ lib/internal/modules/esm/resolve.js | 6 +- .../test-esm-import-meta-resolve.mjs | 70 ++++++++------ test/es-module/test-esm-loader-chaining.mjs | 26 +++++ test/es-module/test-esm-loader-search.js | 4 +- test/es-module/test-esm-resolve-type.js | 16 ++-- .../builtin-named-exports-loader.mjs | 4 +- .../es-module-loaders/hook-resolve-type.mjs | 4 +- .../loader-invalid-format.mjs | 2 +- .../es-module-loaders/loader-invalid-url.mjs | 4 +- .../es-module-loaders/loader-resolve-42.mjs | 2 +- .../loader-resolve-async-fn.mjs | 3 + .../loader-resolve-bad-next-context.mjs | 2 +- .../loader-resolve-bad-next-specifier.mjs | 2 +- .../es-module-loaders/loader-resolve-foo.mjs | 2 +- .../loader-resolve-incomplete.mjs | 2 +- .../loader-resolve-multiple-next-calls.mjs | 6 +- .../loader-resolve-next-modified.mjs | 4 +- ...oader-resolve-passing-modified-context.mjs | 2 +- .../loader-resolve-passthru.mjs | 2 +- ...der-resolve-receiving-modified-context.mjs | 2 +- .../loader-resolve-shortcircuit.mjs | 2 +- .../es-module-loaders/loader-shared-dep.mjs | 4 +- .../loader-with-custom-condition.mjs | 4 +- .../es-module-loaders/loader-with-dep.mjs | 4 +- .../missing-dynamic-instantiate-hook.mjs | 4 +- .../es-module-loaders/mock-loader.mjs | 4 +- .../not-found-assert-loader.mjs | 6 +- 30 files changed, 216 insertions(+), 127 deletions(-) create mode 100644 test/fixtures/es-module-loaders/loader-resolve-async-fn.mjs diff --git a/doc/api/esm.md b/doc/api/esm.md index 9f80dcb094e607..c5ef7eb863f6b7 100644 --- a/doc/api/esm.md +++ b/doc/api/esm.md @@ -324,6 +324,9 @@ added: - v13.9.0 - v12.16.2 changes: + - version: REPLACEME + pr-url: https://github.com/nodejs/node/pull/43363 + description: Convert from asynchronous to synchronous. - version: - v16.2.0 - v14.18.0 @@ -339,15 +342,19 @@ command flag enabled. * `specifier` {string} The module specifier to resolve relative to `parent`. * `parent` {string|URL} The absolute parent module URL to resolve from. If none is specified, the value of `import.meta.url` is used as the default. -* Returns: {Promise} +* Returns: {string} Provides a module-relative resolution function scoped to each module, returning -the URL string. +the URL string. In alignment with browser behavior, this now returns +synchronously. + +> **Caveat** This can result in synchronous file-system operations, which +> can impact performance similarly to `require.resolve`. ```js -const dependencyAsset = await import.meta.resolve('component-lib/asset.css'); +const dependencyAsset = import.meta.resolve('component-lib/asset.css'); ``` `import.meta.resolve` also accepts a second argument which is the parent module @@ -356,11 +363,11 @@ from which to resolve from: ```js -await import.meta.resolve('./dep', import.meta.url); +import.meta.resolve('./dep', import.meta.url); ``` -This function is asynchronous because the ES module resolver in Node.js is -allowed to be asynchronous. +This function is synchronous because the ES module resolver in Node.js is +synchronous. ## Interoperability with CommonJS @@ -731,6 +738,9 @@ prevent unintentional breaks in the chain.