@@ -538,6 +538,43 @@ and in a CommonJS one. For example, this code will also work:
538
538
const { something } = require (' a-package/foo' ); // Loads from ./foo.js.
539
539
```
540
540
541
+ ### Internal package imports
542
+
543
+ In addition to the ` "exports" ` field it is possible to define internal package
544
+ import maps that only apply to import specifiers from within the package itself.
545
+
546
+ Entries in the imports field must always start with ` # ` to ensure they are
547
+ clearly disambiguated from package specifiers.
548
+
549
+ For example, the imports field can be used to gain the benefits of conditional
550
+ exports for internal modules:
551
+
552
+ ``` json
553
+ // package.json
554
+ {
555
+ "imports" : {
556
+ "#dep" : {
557
+ "node" : " dep-node-native" ,
558
+ "default" : " ./dep-polyfill.js"
559
+ }
560
+ },
561
+ "dependencies" : {
562
+ "dep-node-native" : " ^1.0.0"
563
+ }
564
+ }
565
+ ```
566
+
567
+ where ` import '#dep' ` would now get the resolution of the external package
568
+ ` dep-node-native ` (including its exports in turn), and instead get the local
569
+ file ` ./dep-polyfill.js ` relative to the package in other environments.
570
+
571
+ Unlike the exports field, import maps permit mapping to external packages
572
+ because this provides an important use case for conditional loading and also can
573
+ be done without the risk of cycles, unlike for exports.
574
+
575
+ Apart from the above, the resolution rules for the imports field are otherwise
576
+ analogous to the exports field.
577
+
541
578
### Dual CommonJS/ES module packages
542
579
543
580
Prior to the introduction of support for ES modules in Node.js, it was a common
@@ -1552,10 +1589,11 @@ The resolver can throw the following errors:
1552
1589
or package subpath specifier .
1553
1590
* _Invalid Package Configuration_ : package .json configuration is invalid or
1554
1591
contains an invalid configuration .
1555
- * _Invalid Package Target_ : Package exports define a target module within the
1556
- package that is an invalid type or string target .
1592
+ * _Invalid Package Target_ : Package exports or imports define a target module
1593
+ for the package that is an invalid type or string target .
1557
1594
* _Package Path Not Exported_ : Package exports do not define or permit a target
1558
1595
subpath in the package for the given module .
1596
+ * _Package Import Not Defined_ : Package imports do not define the specifier .
1559
1597
* _Module Not Found_ : The package or module requested does not exist .
1560
1598
1561
1599
< details >
@@ -1567,11 +1605,14 @@ The resolver can throw the following errors:
1567
1605
> 1. If _specifier_ is a valid URL , then
1568
1606
> 1. Set _resolvedURL_ to the result of parsing and reserializing
1569
1607
> _specifier_ as a URL .
1570
- > 1. Otherwise , if _specifier_ starts with _ " /" _ , then
1571
- > 1. Throw an _Invalid Module Specifier_ error .
1572
- > 1. Otherwise , if _specifier_ starts with _ " ./" _ or _ " ../" _ , then
1608
+ > 1. Otherwise , if _specifier_ starts with _ " /" _ , _ " ./" _ or _ " ../" _ , then
1573
1609
> 1. Set _resolvedURL_ to the URL resolution of _specifier_ relative to
1574
1610
> _parentURL_ .
1611
+ > 1. Otherwise , if _specifier_ starts with _ " #" _ , then
1612
+ > 1. Set _resolvedURL_ to the result of
1613
+ > ** PACKAGE_INTERNAL_RESOLVE ** (_specifier_, _parentURL_).
1614
+ > 1. If _resolvedURL_ is ** null ** or ** undefined ** , throw a
1615
+ > _Package Import Not Defined_ error .
1575
1616
> 1. Otherwise ,
1576
1617
> 1. Note : _specifier_ is now a bare specifier .
1577
1618
> 1. Set _resolvedURL_ the result of
@@ -1609,7 +1650,7 @@ The resolver can throw the following errors:
1609
1650
> 1. If _packageSubpath_ contains any _ " ." _ or _ " .." _ segments or percent
1610
1651
> encoded strings for _ " /" _ or _ " \\ " _ , then
1611
1652
> 1. Throw an _Invalid Module Specifier_ error .
1612
- > 1. Set _selfUrl_ to the result of
1653
+ > 1. Let _selfUrl_ be the result of
1613
1654
> ** SELF_REFERENCE_RESOLVE ** (_packageName_, _packageSubpath_, _parentURL_).
1614
1655
> 1. If _selfUrl_ isn ' t empty, return _selfUrl_.
1615
1656
> 1. If _packageSubpath_ is _undefined_ and _packageName_ is a Node.js builtin
@@ -1632,8 +1673,11 @@ The resolver can throw the following errors:
1632
1673
> 1. If _pjson_ is not **null** and _pjson_ has an _"exports"_ key, then
1633
1674
> 1. Let _exports_ be _pjson.exports_.
1634
1675
> 1. If _exports_ is not **null** or **undefined**, then
1635
- > 1. Return **PACKAGE_EXPORTS_RESOLVE**(_packageURL_,
1636
- > _packageSubpath_, _pjson.exports_).
1676
+ > 1. Let _resolved_ be the result of **PACKAGE_EXPORTS_RESOLVE**(
1677
+ > _packageURL_, _packageSubpath_, _pjson.exports_).
1678
+ > 1. If _resolved_ is **null** or **undefined**, throw a
1679
+ > _Package Path Not Exported_ error.
1680
+ > 1. Return _resolved_.
1637
1681
> 1. Return the URL resolution of _packageSubpath_ in _packageURL_.
1638
1682
> 1. Throw a _Module Not Found_ error.
1639
1683
@@ -1654,8 +1698,11 @@ The resolver can throw the following errors:
1654
1698
> 1. If _pjson_ is not **null** and _pjson_ has an _"exports"_ key, then
1655
1699
> 1. Let _exports_ be _pjson.exports_.
1656
1700
> 1. If _exports_ is not **null** or **undefined**, then
1657
- > 1. Return **PACKAGE_EXPORTS_RESOLVE**(_packageURL_, _subpath_,
1658
- > _pjson.exports_).
1701
+ > 1. Let _resolved_ be the result of **PACKAGE_EXPORTS_RESOLVE**(
1702
+ > _packageURL_, _subpath_, _pjson.exports_).
1703
+ > 1. If _resolved_ is **null** or **undefined**, throw a
1704
+ > _Package Path Not Exported_ error.
1705
+ > 1. Return _resolved_.
1659
1706
> 1. Return the URL resolution of _subpath_ in _packageURL_.
1660
1707
> 1. Otherwise, return **undefined**.
1661
1708
@@ -1668,12 +1715,18 @@ The resolver can throw the following errors:
1668
1715
> not starting with _"."_, throw an _Invalid Package Configuration_ error.
1669
1716
> 1. If _pjson.exports_ is a String or Array, or an Object containing no
1670
1717
> keys starting with _"."_, then
1671
- > 1. Return **PACKAGE_EXPORTS_TARGET_RESOLVE**(_packageURL_,
1672
- > _pjson.exports_, _""_).
1718
+ > 1. Let _resolved_ be the result of **PACKAGE_TARGET_RESOLVE**(
1719
+ > _packageURL_, _pjson.exports_, _""_, **false**, _defaultEnv_).
1720
+ > 1. If _resolved_ is **null** or **undefined**, throw a
1721
+ > _Package Path Not Exported_ error.
1722
+ > 1. Return _resolved_.
1673
1723
> 1. If _pjson.exports_ is an Object containing a _"."_ property, then
1674
1724
> 1. Let _mainExport_ be the _"."_ property in _pjson.exports_.
1675
- > 1. Return **PACKAGE_EXPORTS_TARGET_RESOLVE**(_packageURL_,
1676
- > _mainExport_, _""_).
1725
+ > 1. Let _resolved_ be the result of **PACKAGE_TARGET_RESOLVE**(
1726
+ > _packageURL_, _mainExport_, _""_, **false**, _defaultEnv_).
1727
+ > 1. If _resolved_ is **null** or **undefined**, throw a
1728
+ > _Package Path Not Exported_ error.
1729
+ > 1. Return _resolved_.
1677
1730
> 1. Throw a _Package Path Not Exported_ error.
1678
1731
> 1. Let _legacyMainURL_ be the result applying the legacy
1679
1732
> **LOAD_AS_DIRECTORY** CommonJS resolver to _packageURL_, throwing a
@@ -1687,31 +1740,37 @@ The resolver can throw the following errors:
1687
1740
> 1. Set _packagePath_ to _"./"_ concatenated with _packagePath_.
1688
1741
> 1. If _packagePath_ is a key of _exports_, then
1689
1742
> 1. Let _target_ be the value of _exports\[ packagePath\] _.
1690
- > 1. Return **PACKAGE_EXPORTS_TARGET_RESOLVE **(_packageURL_, _target_,
1691
- > _""_, _defaultEnv_).
1743
+ > 1. Return **PACKAGE_TARGET_RESOLVE **(_packageURL_, _target_,
1744
+ > _""_, **false**, _defaultEnv_).
1692
1745
> 1. Let _directoryKeys_ be the list of keys of _exports_ ending in
1693
1746
> _"/"_, sorted by length descending.
1694
1747
> 1. For each key _directory_ in _directoryKeys_, do
1695
1748
> 1. If _packagePath_ starts with _directory_, then
1696
1749
> 1. Let _target_ be the value of _exports\[ directory\] _.
1697
1750
> 1. Let _subpath_ be the substring of _target_ starting at the index
1698
1751
> of the length of _directory_.
1699
- > 1. Return **PACKAGE_EXPORTS_TARGET_RESOLVE **(_packageURL_, _target_,
1700
- > _subpath_, _defaultEnv_).
1701
- > 1. Throw a _Package Path Not Exported_ error .
1752
+ > 1. Return **PACKAGE_TARGET_RESOLVE **(_packageURL_, _target_,
1753
+ > _subpath_, **false**, _defaultEnv_).
1754
+ > 1. Return **null** .
1702
1755
1703
- **PACKAGE_EXPORTS_TARGET_RESOLVE **(_packageURL_, _target_, _subpath_, _env_)
1756
+ **PACKAGE_TARGET_RESOLVE **(_packageURL_, _target_, _subpath_, _internal_ , _env_)
1704
1757
1705
1758
> 1. If _target_ is a String, then
1706
- > 1. If _target_ does not start with _"./"_ or contains any _"node_modules"_
1707
- > segments including _"node_modules"_ percent-encoding, throw an
1708
- > _Invalid Package Target_ error.
1759
+ > 1. If _target_ contains any _"node_modules"_ segments including
1760
+ > _"node_modules"_ percent-encoding, throw an _Invalid Package Target_
1761
+ > error.
1762
+ > 1. If _subpath_ has non-zero length and _target_ does not end with _"/"_,
1763
+ > throw an _Invalid Module Specifier_ error.
1764
+ > 1. If _target_ does not start with _"./"_, then
1765
+ > 1. If _target_ does not start with _"../"_ or _"/"_ and is not a valid
1766
+ > URL, then
1767
+ > 1. If _internal_ is **true**, return **PACKAGE_RESOLVE**(
1768
+ > _target_ + _subpath_, _packageURL_ + _"/"_)_.
1769
+ > 1. Otherwise throw an _Invalid Package Target_ error.
1709
1770
> 1. Let _resolvedTarget_ be the URL resolution of the concatenation of
1710
1771
> _packageURL_ and _target_.
1711
1772
> 1. If _resolvedTarget_ is not contained in _packageURL_, throw an
1712
1773
> _Invalid Package Target_ error.
1713
- > 1. If _subpath_ has non-zero length and _target_ does not end with _"/"_,
1714
- > throw an _Invalid Module Specifier_ error.
1715
1774
> 1. Let _resolved_ be the URL resolution of the concatenation of
1716
1775
> _subpath_ and _resolvedTarget_.
1717
1776
> 1. If _resolved_ is not contained in _resolvedTarget_, throw an
@@ -1723,22 +1782,48 @@ The resolver can throw the following errors:
1723
1782
> 1. For each property _p_ of _target_, in object insertion order as,
1724
1783
> 1. If _p_ equals _"default"_ or _env_ contains an entry for _p_, then
1725
1784
> 1. Let _targetValue_ be the value of the _p_ property in _target_.
1726
- > 1. Return the result of **PACKAGE_EXPORTS_TARGET_RESOLVE**(
1727
- > _packageURL_, _targetValue_, _subpath_, _env_), continuing the
1728
- > loop on any _Package Path Not Exported_ error.
1729
- > 1. Throw a _Package Path Not Exported_ error.
1785
+ > 1. Let _resolved_ be the result of **PACKAGE_TARGET_RESOLVE**(
1786
+ > _packageURL_, _targetValue_, _subpath_, _internal_, _env_)
1787
+ > 1. If _resolved_ is equal to **undefined**, continue the loop.
1788
+ > 1. Return _resolved_.
1789
+ > 1. Return **undefined**.
1730
1790
> 1. Otherwise, if _target_ is an Array, then
1731
- > 1. If _target.length is zero, throw a _Package Path Not Exported_ error .
1791
+ > 1. If _target.length is zero, return **null** .
1732
1792
> 1. For each item _targetValue_ in _target_, do
1733
- > 1. If _targetValue_ is an Array, continue the loop.
1734
- > 1. Return the result of **PACKAGE_EXPORTS_TARGET_RESOLVE**(_packageURL_ ,
1735
- > _targetValue_, _subpath_, _env_), continuing the loop on any
1736
- > _Package Path Not Exported_ or _Invalid Package Target_ error .
1737
- > 1. Throw the last fallback resolution error .
1738
- > 1. Otherwise, if _target_ is _null_, throw a _Package Path Not Exported_
1739
- > error .
1793
+ > 1. Let _resolved_ be the result of **PACKAGE_TARGET_RESOLVE**(
1794
+ > _packageURL_, _targetValue_, _subpath_, _internal_, _env_) ,
1795
+ > continuing the loop on any _Invalid Package Target_ error.
1796
+ > 1. If _resolved_ is **undefined**, continue the loop .
1797
+ > 1. Return _resolved_ .
1798
+ > 1. Return or throw the last fallback resolution **null** return or error.
1799
+ > 1. Otherwise, if _target_ is _null_, return **null** .
1740
1800
> 1. Otherwise throw an _Invalid Package Target_ error.
1741
1801
1802
+ **PACKAGE_INTERNAL_RESOLVE**(_specifier_, _parentURL_)
1803
+
1804
+ > 1. Assert: _specifier_ begins with _"#"_.
1805
+ > 1. If _specifier_ is exactly equal to _"#"_ or starts with _"#/"_, then
1806
+ > 1. Throw an _Invalid Module Specifier_ error.
1807
+ > 1. Let _packageURL_ be the result of **READ_PACKAGE_SCOPE**(_parentURL_).
1808
+ > 1. If _packageURL_ is not **null**, then
1809
+ > 1. Let _pjson_ be the result of **READ_PACKAGE_JSON**(_packageURL_).
1810
+ > 1. If _pjson.imports is a non-null Object, then
1811
+ > 1. Let _imports_ be _pjson.imports_.
1812
+ > 1. If _specifier_ is a key of _imports_, then
1813
+ > 1. Let _target_ be the value of _imports\[ specifier\] _.
1814
+ > 1. Return **PACKAGE_TARGET_RESOLVE**(_packageURL_, _target_,
1815
+ > _""_, **true**, _defaultEnv_).
1816
+ > 1. Let _directoryKeys_ be the list of keys of _imports_ ending in
1817
+ > _"/"_, sorted by length descending.
1818
+ > 1. For each key _directory_ in _directoryKeys_, do
1819
+ > 1. If _specifier_ starts with _directory_, then
1820
+ > 1. Let _target_ be the value of _imports\[ directory\] _.
1821
+ > 1. Let _subpath_ be the substring of _target_ starting at the
1822
+ > index of the length of _directory_.
1823
+ > 1. Return **PACKAGE_TARGET_RESOLVE**(_packageURL_, _target_,
1824
+ > _subpath_, **true**, _defaultEnv_).
1825
+ > 1. Return **null**.
1826
+
1742
1827
**ESM_FORMAT**(_url_)
1743
1828
1744
1829
> 1. Assert: _url_ corresponds to an existing file.
0 commit comments