@@ -837,6 +837,32 @@ const __filename = fileURLToPath(import.meta.url);
837
837
const __dirname = dirname(__filename);
838
838
` ` `
839
839
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
+
840
866
### No `require.extensions`
841
867
842
868
`require.extensions` is not used by `import`. The expectation is that loader
@@ -1405,13 +1431,14 @@ The resolver has the following properties:
1405
1431
1406
1432
The algorithm to load an ES module specifier is given through the
1407
1433
**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.
1410
1435
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.
1415
1442
1416
1443
In the following algorithms, all subroutine errors are propagated as errors
1417
1444
of these top-level routines unless stated otherwise.
@@ -1440,11 +1467,13 @@ _defaultEnv_ is the conditional environment name priority array,
1440
1467
> 1. If _resolvedURL_ contains any percent encodings of _"/"_ or _"\\ "_ (_"%2f"_
1441
1468
> and _"%5C"_ respectively), then
1442
1469
> 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
1444
1472
> 1. Throw a _Module Not Found_ error.
1445
1473
> 1. Set _resolvedURL_ to the real path of _resolvedURL_.
1446
1474
> 1. Let _format_ be the result of **ESM_FORMAT**(_resolvedURL_).
1447
1475
> 1. Load _resolvedURL_ as module format, _format_.
1476
+ > 1. Return _resolvedURL_.
1448
1477
1449
1478
**PACKAGE_RESOLVE**(_packageSpecifier_, _parentURL_)
1450
1479
@@ -1472,7 +1501,7 @@ _defaultEnv_ is the conditional environment name priority array,
1472
1501
> 1. If _selfUrl_ isn't empty, return _selfUrl_.
1473
1502
> 1. If _packageSubpath_ is _undefined_ and _packageName_ is a Node.js builtin
1474
1503
> module, then
1475
- > 1. Return the string _"node :"_ concatenated with _packageSpecifier_.
1504
+ > 1. Return the string _"nodejs :"_ concatenated with _packageSpecifier_.
1476
1505
> 1. While _parentURL_ is not the file system root,
1477
1506
> 1. Let _packageURL_ be the URL resolution of _"node_modules/"_
1478
1507
> concatenated with _packageSpecifier_, relative to _parentURL_.
@@ -1481,6 +1510,8 @@ _defaultEnv_ is the conditional environment name priority array,
1481
1510
> 1. Set _parentURL_ to the parent URL path of _parentURL_.
1482
1511
> 1. Continue the next loop iteration.
1483
1512
> 1. Let _pjson_ be the result of **READ_PACKAGE_JSON**(_packageURL_).
1513
+ > 1. If _packageSubpath_ is equal to _"./"_, then
1514
+ > 1. Return _packageURL_ + _"/"_.
1484
1515
> 1. If _packageSubpath_ is _undefined__, then
1485
1516
> 1. Return the result of **PACKAGE_MAIN_RESOLVE**(_packageURL_,
1486
1517
> _pjson_).
@@ -1502,6 +1533,8 @@ _defaultEnv_ is the conditional environment name priority array,
1502
1533
> 1. If _pjson_ does not include an _"exports"_ property, then
1503
1534
> 1. Return **undefined**.
1504
1535
> 1. If _pjson.name_ is equal to _packageName_, then
1536
+ > 1. If _packageSubpath_ is equal to _"./"_, then
1537
+ > 1. Return _packageURL_ + _"/"_.
1505
1538
> 1. If _packageSubpath_ is _undefined_, then
1506
1539
> 1. Return the result of **PACKAGE_MAIN_RESOLVE**(_packageURL_, _pjson_).
1507
1540
> 1. Otherwise,
@@ -1680,3 +1713,4 @@ success!
1680
1713
[the official standard format]: https://tc39.github.io/ecma262/#sec-modules
1681
1714
[transpiler loader example]: #esm_transpiler_loader
1682
1715
[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