@@ -786,6 +786,32 @@ const __filename = fileURLToPath(import.meta.url);
786
786
const __dirname = dirname(__filename);
787
787
` ` `
788
788
789
+ ### No ` require.resolve`
790
+
791
+ Former use cases relying on ` require.resolve` to determine the resolved path
792
+ of a module can be supported via ` import.meta.resolve` , which is experimental
793
+ and supported via the ` --experimental-import-meta-resolve` flag:
794
+
795
+ ` ` ` js
796
+ (async () => {
797
+ const dependencyAsset = await import.meta.resolve('component-lib/asset.css');
798
+ })();
799
+ ` ` `
800
+
801
+ ` import.meta.resolve` also accepts a second argument which is the parent module
802
+ from which to resolve from:
803
+
804
+ ` ` ` js
805
+ (async () => {
806
+ // Equivalent to import.meta.resolve('./dep')
807
+ await import.meta.resolve('./dep', import.meta.url);
808
+ })();
809
+ ` ` `
810
+
811
+ This function is asynchronous since the ES module resolver in Node.js is
812
+ asynchronous. With the introduction of [Top-Level Await][], these use cases
813
+ will be easier as they won't require an async function wrapper.
814
+
789
815
### No `require.extensions`
790
816
791
817
`require.extensions` is not used by `import`. The expectation is that loader
@@ -1350,13 +1376,14 @@ The resolver has the following properties:
1350
1376
1351
1377
The algorithm to load an ES module specifier is given through the
1352
1378
**ESM_RESOLVE** method below. It returns the resolved URL for a
1353
- module specifier relative to a parentURL, in addition to the unique module
1354
- format for that resolved URL given by the **ESM_FORMAT** routine.
1379
+ module specifier relative to a parentURL.
1355
1380
1356
- The _"module"_ format is returned for an ECMAScript Module, while the
1357
- _"commonjs"_ format is used to indicate loading through the legacy
1358
- CommonJS loader. Additional formats such as _"addon"_ can be extended in future
1359
- updates.
1381
+ The algorithm to determine the module format of a resolved URL is
1382
+ provided by **ESM_FORMAT**, which returns the unique module
1383
+ format for any file. The _"module"_ format is returned for an ECMAScript
1384
+ Module, while the _"commonjs"_ format is used to indicate loading through the
1385
+ legacy CommonJS loader. Additional formats such as _"addon"_ can be extended in
1386
+ future updates.
1360
1387
1361
1388
In the following algorithms, all subroutine errors are propagated as errors
1362
1389
of these top-level routines unless stated otherwise.
@@ -1385,11 +1412,13 @@ _defaultEnv_ is the conditional environment name priority array,
1385
1412
> 1. If _resolvedURL_ contains any percent encodings of _"/"_ or _"\\ "_ (_"%2f"_
1386
1413
> and _"%5C"_ respectively), then
1387
1414
> 1. Throw an _Invalid Specifier_ error.
1388
- > 1. If the file at _resolvedURL_ does not exist, then
1415
+ > 1. If _resolvedURL_ does not end with a trailing _"/"_ and the file at
1416
+ > _resolvedURL_ does not exist, then
1389
1417
> 1. Throw a _Module Not Found_ error.
1390
1418
> 1. Set _resolvedURL_ to the real path of _resolvedURL_.
1391
1419
> 1. Let _format_ be the result of **ESM_FORMAT**(_resolvedURL_).
1392
1420
> 1. Load _resolvedURL_ as module format, _format_.
1421
+ > 1. Return _resolvedURL_.
1393
1422
1394
1423
**PACKAGE_RESOLVE**(_packageSpecifier_, _parentURL_)
1395
1424
@@ -1417,7 +1446,7 @@ _defaultEnv_ is the conditional environment name priority array,
1417
1446
> 1. If _selfUrl_ isn't empty, return _selfUrl_.
1418
1447
> 1. If _packageSubpath_ is _undefined_ and _packageName_ is a Node.js builtin
1419
1448
> module, then
1420
- > 1. Return the string _"node :"_ concatenated with _packageSpecifier_.
1449
+ > 1. Return the string _"nodejs :"_ concatenated with _packageSpecifier_.
1421
1450
> 1. While _parentURL_ is not the file system root,
1422
1451
> 1. Let _packageURL_ be the URL resolution of _"node_modules/"_
1423
1452
> concatenated with _packageSpecifier_, relative to _parentURL_.
@@ -1426,6 +1455,8 @@ _defaultEnv_ is the conditional environment name priority array,
1426
1455
> 1. Set _parentURL_ to the parent URL path of _parentURL_.
1427
1456
> 1. Continue the next loop iteration.
1428
1457
> 1. Let _pjson_ be the result of **READ_PACKAGE_JSON**(_packageURL_).
1458
+ > 1. If _packageSubpath_ is equal to _"./"_, then
1459
+ > 1. Return _packageURL_ + _"/"_.
1429
1460
> 1. If _packageSubpath_ is _undefined__, then
1430
1461
> 1. Return the result of **PACKAGE_MAIN_RESOLVE**(_packageURL_,
1431
1462
> _pjson_).
@@ -1447,6 +1478,8 @@ _defaultEnv_ is the conditional environment name priority array,
1447
1478
> 1. If _pjson_ does not include an _"exports"_ property, then
1448
1479
> 1. Return **undefined**.
1449
1480
> 1. If _pjson.name_ is equal to _packageName_, then
1481
+ > 1. If _packageSubpath_ is equal to _"./"_, then
1482
+ > 1. Return _packageURL_ + _"/"_.
1450
1483
> 1. If _packageSubpath_ is _undefined_, then
1451
1484
> 1. Return the result of **PACKAGE_MAIN_RESOLVE**(_packageURL_, _pjson_).
1452
1485
> 1. Otherwise,
@@ -1625,3 +1658,4 @@ success!
1625
1658
[the official standard format]: https://tc39.github.io/ecma262/#sec-modules
1626
1659
[transpiler loader example]: #esm_transpiler_loader
1627
1660
[6.1.7 Array Index]: https://tc39.es/ecma262/#integer-index
1661
+ [Top-Level Await]: https://github.com./tc39/proposal-top-level-await
0 commit comments