@@ -546,6 +546,43 @@ and in a CommonJS one. For example, this code will also work:
546
546
const { something } = require (' a-package/foo' ); // Loads from ./foo.js.
547
547
```
548
548
549
+ ### Internal package imports
550
+
551
+ In addition to the ` "exports" ` field it is possible to define internal package
552
+ import maps that only apply to import specifiers from within the package itself.
553
+
554
+ Entries in the imports field must always start with ` # ` to ensure they are
555
+ clearly disambiguated from package specifiers.
556
+
557
+ For example, the imports field can be used to gain the benefits of conditional
558
+ exports for internal modules:
559
+
560
+ ``` json
561
+ // package.json
562
+ {
563
+ "imports" : {
564
+ "#dep" : {
565
+ "node" : " dep-node-native" ,
566
+ "default" : " ./dep-polyfill.js"
567
+ }
568
+ },
569
+ "dependencies" : {
570
+ "dep-node-native" : " ^1.0.0"
571
+ }
572
+ }
573
+ ```
574
+
575
+ where ` import '#dep' ` would now get the resolution of the external package
576
+ ` dep-node-native ` (including its exports in turn), and instead get the local
577
+ file ` ./dep-polyfill.js ` relative to the package in other environments.
578
+
579
+ Unlike the exports field, import maps permit mapping to external packages
580
+ because this provides an important use case for conditional loading and also can
581
+ be done without the risk of cycles, unlike for exports.
582
+
583
+ Apart from the above, the resolution rules for the imports field are otherwise
584
+ analogous to the exports field.
585
+
549
586
### Dual CommonJS/ES module packages
550
587
551
588
Prior to the introduction of support for ES modules in Node.js, it was a common
@@ -1577,10 +1614,11 @@ The resolver can throw the following errors:
1577
1614
or package subpath specifier .
1578
1615
* _Invalid Package Configuration_ : package .json configuration is invalid or
1579
1616
contains an invalid configuration .
1580
- * _Invalid Package Target_ : Package exports define a target module within the
1581
- package that is an invalid type or string target .
1617
+ * _Invalid Package Target_ : Package exports or imports define a target module
1618
+ for the package that is an invalid type or string target .
1582
1619
* _Package Path Not Exported_ : Package exports do not define or permit a target
1583
1620
subpath in the package for the given module .
1621
+ * _Package Import Not Defined_ : Package imports do not define the specifier .
1584
1622
* _Module Not Found_ : The package or module requested does not exist .
1585
1623
1586
1624
< details >
@@ -1592,11 +1630,14 @@ The resolver can throw the following errors:
1592
1630
> 1. If _specifier_ is a valid URL , then
1593
1631
> 1. Set _resolvedURL_ to the result of parsing and reserializing
1594
1632
> _specifier_ as a URL .
1595
- > 1. Otherwise , if _specifier_ starts with _ " /" _ , then
1596
- > 1. Throw an _Invalid Module Specifier_ error .
1597
- > 1. Otherwise , if _specifier_ starts with _ " ./" _ or _ " ../" _ , then
1633
+ > 1. Otherwise , if _specifier_ starts with _ " /" _ , _ " ./" _ or _ " ../" _ , then
1598
1634
> 1. Set _resolvedURL_ to the URL resolution of _specifier_ relative to
1599
1635
> _parentURL_ .
1636
+ > 1. Otherwise , if _specifier_ starts with _ " #" _ , then
1637
+ > 1. Set _resolvedURL_ to the result of
1638
+ > ** PACKAGE_INTERNAL_RESOLVE ** (_specifier_, _parentURL_).
1639
+ > 1. If _resolvedURL_ is ** null ** or ** undefined ** , throw a
1640
+ > _Package Import Not Defined_ error .
1600
1641
> 1. Otherwise ,
1601
1642
> 1. Note : _specifier_ is now a bare specifier .
1602
1643
> 1. Set _resolvedURL_ the result of
@@ -1634,7 +1675,7 @@ The resolver can throw the following errors:
1634
1675
> 1. If _packageSubpath_ contains any _ " ." _ or _ " .." _ segments or percent
1635
1676
> encoded strings for _ " /" _ or _ " \\ " _ , then
1636
1677
> 1. Throw an _Invalid Module Specifier_ error .
1637
- > 1. Set _selfUrl_ to the result of
1678
+ > 1. Let _selfUrl_ be the result of
1638
1679
> ** SELF_REFERENCE_RESOLVE ** (_packageName_, _packageSubpath_, _parentURL_).
1639
1680
> 1. If _selfUrl_ isn ' t empty, return _selfUrl_.
1640
1681
> 1. If _packageSubpath_ is _undefined_ and _packageName_ is a Node.js builtin
@@ -1657,8 +1698,11 @@ The resolver can throw the following errors:
1657
1698
> 1. If _pjson_ is not **null** and _pjson_ has an _"exports"_ key, then
1658
1699
> 1. Let _exports_ be _pjson.exports_.
1659
1700
> 1. If _exports_ is not **null** or **undefined**, then
1660
- > 1. Return **PACKAGE_EXPORTS_RESOLVE**(_packageURL_,
1661
- > _packageSubpath_, _pjson.exports_).
1701
+ > 1. Let _resolved_ be the result of **PACKAGE_EXPORTS_RESOLVE**(
1702
+ > _packageURL_, _packageSubpath_, _pjson.exports_).
1703
+ > 1. If _resolved_ is **null** or **undefined**, throw a
1704
+ > _Package Path Not Exported_ error.
1705
+ > 1. Return _resolved_.
1662
1706
> 1. Return the URL resolution of _packageSubpath_ in _packageURL_.
1663
1707
> 1. Throw a _Module Not Found_ error.
1664
1708
@@ -1679,8 +1723,11 @@ The resolver can throw the following errors:
1679
1723
> 1. If _pjson_ is not **null** and _pjson_ has an _"exports"_ key, then
1680
1724
> 1. Let _exports_ be _pjson.exports_.
1681
1725
> 1. If _exports_ is not **null** or **undefined**, then
1682
- > 1. Return **PACKAGE_EXPORTS_RESOLVE**(_packageURL_, _subpath_,
1683
- > _pjson.exports_).
1726
+ > 1. Let _resolved_ be the result of **PACKAGE_EXPORTS_RESOLVE**(
1727
+ > _packageURL_, _subpath_, _pjson.exports_).
1728
+ > 1. If _resolved_ is **null** or **undefined**, throw a
1729
+ > _Package Path Not Exported_ error.
1730
+ > 1. Return _resolved_.
1684
1731
> 1. Return the URL resolution of _subpath_ in _packageURL_.
1685
1732
> 1. Otherwise, return **undefined**.
1686
1733
@@ -1693,12 +1740,18 @@ The resolver can throw the following errors:
1693
1740
> not starting with _"."_, throw an _Invalid Package Configuration_ error.
1694
1741
> 1. If _pjson.exports_ is a String or Array, or an Object containing no
1695
1742
> keys starting with _"."_, then
1696
- > 1. Return **PACKAGE_EXPORTS_TARGET_RESOLVE**(_packageURL_,
1697
- > _pjson.exports_, _""_).
1743
+ > 1. Let _resolved_ be the result of **PACKAGE_TARGET_RESOLVE**(
1744
+ > _packageURL_, _pjson.exports_, _""_, **false**, _defaultEnv_).
1745
+ > 1. If _resolved_ is **null** or **undefined**, throw a
1746
+ > _Package Path Not Exported_ error.
1747
+ > 1. Return _resolved_.
1698
1748
> 1. If _pjson.exports_ is an Object containing a _"."_ property, then
1699
1749
> 1. Let _mainExport_ be the _"."_ property in _pjson.exports_.
1700
- > 1. Return **PACKAGE_EXPORTS_TARGET_RESOLVE**(_packageURL_,
1701
- > _mainExport_, _""_).
1750
+ > 1. Let _resolved_ be the result of **PACKAGE_TARGET_RESOLVE**(
1751
+ > _packageURL_, _mainExport_, _""_, **false**, _defaultEnv_).
1752
+ > 1. If _resolved_ is **null** or **undefined**, throw a
1753
+ > _Package Path Not Exported_ error.
1754
+ > 1. Return _resolved_.
1702
1755
> 1. Throw a _Package Path Not Exported_ error.
1703
1756
> 1. Let _legacyMainURL_ be the result applying the legacy
1704
1757
> **LOAD_AS_DIRECTORY** CommonJS resolver to _packageURL_, throwing a
@@ -1712,31 +1765,37 @@ The resolver can throw the following errors:
1712
1765
> 1. Set _packagePath_ to _"./"_ concatenated with _packagePath_.
1713
1766
> 1. If _packagePath_ is a key of _exports_, then
1714
1767
> 1. Let _target_ be the value of _exports\[ packagePath\] _.
1715
- > 1. Return **PACKAGE_EXPORTS_TARGET_RESOLVE **(_packageURL_, _target_,
1716
- > _""_, _defaultEnv_).
1768
+ > 1. Return **PACKAGE_TARGET_RESOLVE **(_packageURL_, _target_,
1769
+ > _""_, **false**, _defaultEnv_).
1717
1770
> 1. Let _directoryKeys_ be the list of keys of _exports_ ending in
1718
1771
> _"/"_, sorted by length descending.
1719
1772
> 1. For each key _directory_ in _directoryKeys_, do
1720
1773
> 1. If _packagePath_ starts with _directory_, then
1721
1774
> 1. Let _target_ be the value of _exports\[ directory\] _.
1722
1775
> 1. Let _subpath_ be the substring of _target_ starting at the index
1723
1776
> of the length of _directory_.
1724
- > 1. Return **PACKAGE_EXPORTS_TARGET_RESOLVE **(_packageURL_, _target_,
1725
- > _subpath_, _defaultEnv_).
1726
- > 1. Throw a _Package Path Not Exported_ error .
1777
+ > 1. Return **PACKAGE_TARGET_RESOLVE **(_packageURL_, _target_,
1778
+ > _subpath_, **false**, _defaultEnv_).
1779
+ > 1. Return **null** .
1727
1780
1728
- **PACKAGE_EXPORTS_TARGET_RESOLVE **(_packageURL_, _target_, _subpath_, _env_)
1781
+ **PACKAGE_TARGET_RESOLVE **(_packageURL_, _target_, _subpath_, _internal_ , _env_)
1729
1782
1730
1783
> 1. If _target_ is a String, then
1731
- > 1. If _target_ does not start with _"./"_ or contains any _"node_modules"_
1732
- > segments including _"node_modules"_ percent-encoding, throw an
1733
- > _Invalid Package Target_ error.
1784
+ > 1. If _target_ contains any _"node_modules"_ segments including
1785
+ > _"node_modules"_ percent-encoding, throw an _Invalid Package Target_
1786
+ > error.
1787
+ > 1. If _subpath_ has non-zero length and _target_ does not end with _"/"_,
1788
+ > throw an _Invalid Module Specifier_ error.
1789
+ > 1. If _target_ does not start with _"./"_, then
1790
+ > 1. If _target_ does not start with _"../"_ or _"/"_ and is not a valid
1791
+ > URL, then
1792
+ > 1. If _internal_ is **true**, return **PACKAGE_RESOLVE**(
1793
+ > _target_ + _subpath_, _packageURL_ + _"/"_)_.
1794
+ > 1. Otherwise throw an _Invalid Package Target_ error.
1734
1795
> 1. Let _resolvedTarget_ be the URL resolution of the concatenation of
1735
1796
> _packageURL_ and _target_.
1736
1797
> 1. If _resolvedTarget_ is not contained in _packageURL_, throw an
1737
1798
> _Invalid Package Target_ error.
1738
- > 1. If _subpath_ has non-zero length and _target_ does not end with _"/"_,
1739
- > throw an _Invalid Module Specifier_ error.
1740
1799
> 1. Let _resolved_ be the URL resolution of the concatenation of
1741
1800
> _subpath_ and _resolvedTarget_.
1742
1801
> 1. If _resolved_ is not contained in _resolvedTarget_, throw an
@@ -1748,22 +1807,48 @@ The resolver can throw the following errors:
1748
1807
> 1. For each property _p_ of _target_, in object insertion order as,
1749
1808
> 1. If _p_ equals _"default"_ or _env_ contains an entry for _p_, then
1750
1809
> 1. Let _targetValue_ be the value of the _p_ property in _target_.
1751
- > 1. Return the result of **PACKAGE_EXPORTS_TARGET_RESOLVE**(
1752
- > _packageURL_, _targetValue_, _subpath_, _env_), continuing the
1753
- > loop on any _Package Path Not Exported_ error.
1754
- > 1. Throw a _Package Path Not Exported_ error.
1810
+ > 1. Let _resolved_ be the result of **PACKAGE_TARGET_RESOLVE**(
1811
+ > _packageURL_, _targetValue_, _subpath_, _internal_, _env_)
1812
+ > 1. If _resolved_ is equal to **undefined**, continue the loop.
1813
+ > 1. Return _resolved_.
1814
+ > 1. Return **undefined**.
1755
1815
> 1. Otherwise, if _target_ is an Array, then
1756
- > 1. If _target.length is zero, throw a _Package Path Not Exported_ error .
1816
+ > 1. If _target.length is zero, return **null** .
1757
1817
> 1. For each item _targetValue_ in _target_, do
1758
- > 1. If _targetValue_ is an Array, continue the loop.
1759
- > 1. Return the result of **PACKAGE_EXPORTS_TARGET_RESOLVE**(_packageURL_ ,
1760
- > _targetValue_, _subpath_, _env_), continuing the loop on any
1761
- > _Package Path Not Exported_ or _Invalid Package Target_ error .
1762
- > 1. Throw the last fallback resolution error .
1763
- > 1. Otherwise, if _target_ is _null_, throw a _Package Path Not Exported_
1764
- > error .
1818
+ > 1. Let _resolved_ be the result of **PACKAGE_TARGET_RESOLVE**(
1819
+ > _packageURL_, _targetValue_, _subpath_, _internal_, _env_) ,
1820
+ > continuing the loop on any _Invalid Package Target_ error.
1821
+ > 1. If _resolved_ is **undefined**, continue the loop .
1822
+ > 1. Return _resolved_ .
1823
+ > 1. Return or throw the last fallback resolution **null** return or error.
1824
+ > 1. Otherwise, if _target_ is _null_, return **null** .
1765
1825
> 1. Otherwise throw an _Invalid Package Target_ error.
1766
1826
1827
+ **PACKAGE_INTERNAL_RESOLVE**(_specifier_, _parentURL_)
1828
+
1829
+ > 1. Assert: _specifier_ begins with _"#"_.
1830
+ > 1. If _specifier_ is exactly equal to _"#"_ or starts with _"#/"_, then
1831
+ > 1. Throw an _Invalid Module Specifier_ error.
1832
+ > 1. Let _packageURL_ be the result of **READ_PACKAGE_SCOPE**(_parentURL_).
1833
+ > 1. If _packageURL_ is not **null**, then
1834
+ > 1. Let _pjson_ be the result of **READ_PACKAGE_JSON**(_packageURL_).
1835
+ > 1. If _pjson.imports is a non-null Object, then
1836
+ > 1. Let _imports_ be _pjson.imports_.
1837
+ > 1. If _specifier_ is a key of _imports_, then
1838
+ > 1. Let _target_ be the value of _imports\[ specifier\] _.
1839
+ > 1. Return **PACKAGE_TARGET_RESOLVE**(_packageURL_, _target_,
1840
+ > _""_, **true**, _defaultEnv_).
1841
+ > 1. Let _directoryKeys_ be the list of keys of _imports_ ending in
1842
+ > _"/"_, sorted by length descending.
1843
+ > 1. For each key _directory_ in _directoryKeys_, do
1844
+ > 1. If _specifier_ starts with _directory_, then
1845
+ > 1. Let _target_ be the value of _imports\[ directory\] _.
1846
+ > 1. Let _subpath_ be the substring of _target_ starting at the
1847
+ > index of the length of _directory_.
1848
+ > 1. Return **PACKAGE_TARGET_RESOLVE**(_packageURL_, _target_,
1849
+ > _subpath_, **true**, _defaultEnv_).
1850
+ > 1. Return **null**.
1851
+
1767
1852
**ESM_FORMAT**(_url_)
1768
1853
1769
1854
> 1. Assert: _url_ corresponds to an existing file.
0 commit comments