@@ -837,6 +837,32 @@ const __filename = fileURLToPath(import.meta.url);
837837const __dirname = dirname(__filename);
838838` ` `
839839
840+ ### No ` require.resolve`
841+
842+ Former use cases relying on ` require.resolve` to determine the resolved path
843+ of a module can be supported via ` import.meta.resolve` , which is experimental
844+ and supported via the ` --experimental-import-meta-resolve` flag:
845+
846+ ` ` ` js
847+ (async () => {
848+ const dependencyAsset = await import.meta.resolve('component-lib/asset.css');
849+ })();
850+ ` ` `
851+
852+ ` import.meta.resolve` also accepts a second argument which is the parent module
853+ from which to resolve from:
854+
855+ ` ` ` js
856+ (async () => {
857+ // Equivalent to import.meta.resolve('./dep')
858+ await import.meta.resolve('./dep', import.meta.url);
859+ })();
860+ ` ` `
861+
862+ This function is asynchronous since the ES module resolver in Node.js is
863+ asynchronous. With the introduction of [Top-Level Await][], these use cases
864+ will be easier as they won't require an async function wrapper.
865+
840866### No `require.extensions`
841867
842868`require.extensions` is not used by `import`. The expectation is that loader
@@ -1405,13 +1431,14 @@ The resolver has the following properties:
14051431
14061432The algorithm to load an ES module specifier is given through the
14071433**ESM_RESOLVE** method below. It returns the resolved URL for a
1408- module specifier relative to a parentURL, in addition to the unique module
1409- format for that resolved URL given by the **ESM_FORMAT** routine.
1434+ module specifier relative to a parentURL.
14101435
1411- The _"module"_ format is returned for an ECMAScript Module, while the
1412- _"commonjs"_ format is used to indicate loading through the legacy
1413- CommonJS loader. Additional formats such as _"addon"_ can be extended in future
1414- updates.
1436+ The algorithm to determine the module format of a resolved URL is
1437+ provided by **ESM_FORMAT**, which returns the unique module
1438+ format for any file. The _"module"_ format is returned for an ECMAScript
1439+ Module, while the _"commonjs"_ format is used to indicate loading through the
1440+ legacy CommonJS loader. Additional formats such as _"addon"_ can be extended in
1441+ future updates.
14151442
14161443In the following algorithms, all subroutine errors are propagated as errors
14171444of these top-level routines unless stated otherwise.
@@ -1440,11 +1467,13 @@ _defaultEnv_ is the conditional environment name priority array,
14401467> 1. If _resolvedURL_ contains any percent encodings of _"/"_ or _"\\ "_ (_"%2f"_
14411468> and _"%5C"_ respectively), then
14421469> 1. Throw an _Invalid Specifier_ error.
1443- > 1. If the file at _resolvedURL_ does not exist, then
1470+ > 1. If _resolvedURL_ does not end with a trailing _"/"_ and the file at
1471+ > _resolvedURL_ does not exist, then
14441472> 1. Throw a _Module Not Found_ error.
14451473> 1. Set _resolvedURL_ to the real path of _resolvedURL_.
14461474> 1. Let _format_ be the result of **ESM_FORMAT**(_resolvedURL_).
14471475> 1. Load _resolvedURL_ as module format, _format_.
1476+ > 1. Return _resolvedURL_.
14481477
14491478**PACKAGE_RESOLVE**(_packageSpecifier_, _parentURL_)
14501479
@@ -1472,7 +1501,7 @@ _defaultEnv_ is the conditional environment name priority array,
14721501> 1. If _selfUrl_ isn't empty, return _selfUrl_.
14731502> 1. If _packageSubpath_ is _undefined_ and _packageName_ is a Node.js builtin
14741503> module, then
1475- > 1. Return the string _"node :"_ concatenated with _packageSpecifier_.
1504+ > 1. Return the string _"nodejs :"_ concatenated with _packageSpecifier_.
14761505> 1. While _parentURL_ is not the file system root,
14771506> 1. Let _packageURL_ be the URL resolution of _"node_modules/"_
14781507> concatenated with _packageSpecifier_, relative to _parentURL_.
@@ -1481,6 +1510,8 @@ _defaultEnv_ is the conditional environment name priority array,
14811510> 1. Set _parentURL_ to the parent URL path of _parentURL_.
14821511> 1. Continue the next loop iteration.
14831512> 1. Let _pjson_ be the result of **READ_PACKAGE_JSON**(_packageURL_).
1513+ > 1. If _packageSubpath_ is equal to _"./"_, then
1514+ > 1. Return _packageURL_ + _"/"_.
14841515> 1. If _packageSubpath_ is _undefined__, then
14851516> 1. Return the result of **PACKAGE_MAIN_RESOLVE**(_packageURL_,
14861517> _pjson_).
@@ -1502,6 +1533,8 @@ _defaultEnv_ is the conditional environment name priority array,
15021533> 1. If _pjson_ does not include an _"exports"_ property, then
15031534> 1. Return **undefined**.
15041535> 1. If _pjson.name_ is equal to _packageName_, then
1536+ > 1. If _packageSubpath_ is equal to _"./"_, then
1537+ > 1. Return _packageURL_ + _"/"_.
15051538> 1. If _packageSubpath_ is _undefined_, then
15061539> 1. Return the result of **PACKAGE_MAIN_RESOLVE**(_packageURL_, _pjson_).
15071540> 1. Otherwise,
@@ -1680,3 +1713,4 @@ success!
16801713[the official standard format]: https://tc39.github.io/ecma262/#sec-modules
16811714[transpiler loader example]: #esm_transpiler_loader
16821715[6.1.7 Array Index]: https://tc39.es/ecma262/#integer-index
1716+ [Top-Level Await]: https://github.com/tc39/proposal-top-level-await
0 commit comments