@@ -546,6 +546,43 @@ and in a CommonJS one. For example, this code will also work:
546546const { something } = require (' a-package/foo' ); // Loads from ./foo.js.
547547```
548548
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+
549586### Dual CommonJS/ES module packages
550587
551588Prior 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:
15771614 or package subpath specifier .
15781615* _Invalid Package Configuration_ : package .json configuration is invalid or
15791616 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 .
15821619* _Package Path Not Exported_ : Package exports do not define or permit a target
15831620 subpath in the package for the given module .
1621+ * _Package Import Not Defined_ : Package imports do not define the specifier .
15841622* _Module Not Found_ : The package or module requested does not exist .
15851623
15861624< details >
@@ -1592,11 +1630,14 @@ The resolver can throw the following errors:
15921630> 1. If _specifier_ is a valid URL , then
15931631> 1. Set _resolvedURL_ to the result of parsing and reserializing
15941632> _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
15981634> 1. Set _resolvedURL_ to the URL resolution of _specifier_ relative to
15991635> _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 .
16001641> 1. Otherwise ,
16011642> 1. Note : _specifier_ is now a bare specifier .
16021643> 1. Set _resolvedURL_ the result of
@@ -1634,7 +1675,7 @@ The resolver can throw the following errors:
16341675> 1. If _packageSubpath_ contains any _ " ." _ or _ " .." _ segments or percent
16351676> encoded strings for _ " /" _ or _ " \\ " _ , then
16361677> 1. Throw an _Invalid Module Specifier_ error .
1637- > 1. Set _selfUrl_ to the result of
1678+ > 1. Let _selfUrl_ be the result of
16381679> ** SELF_REFERENCE_RESOLVE ** (_packageName_, _packageSubpath_, _parentURL_).
16391680> 1. If _selfUrl_ isn ' t empty, return _selfUrl_.
16401681> 1. If _packageSubpath_ is _undefined_ and _packageName_ is a Node.js builtin
@@ -1657,8 +1698,11 @@ The resolver can throw the following errors:
16571698> 1. If _pjson_ is not **null** and _pjson_ has an _"exports"_ key, then
16581699> 1. Let _exports_ be _pjson.exports_.
16591700> 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_.
16621706> 1. Return the URL resolution of _packageSubpath_ in _packageURL_.
16631707> 1. Throw a _Module Not Found_ error.
16641708
@@ -1679,8 +1723,11 @@ The resolver can throw the following errors:
16791723> 1. If _pjson_ is not **null** and _pjson_ has an _"exports"_ key, then
16801724> 1. Let _exports_ be _pjson.exports_.
16811725> 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_.
16841731> 1. Return the URL resolution of _subpath_ in _packageURL_.
16851732> 1. Otherwise, return **undefined**.
16861733
@@ -1693,12 +1740,18 @@ The resolver can throw the following errors:
16931740> not starting with _"."_, throw an _Invalid Package Configuration_ error.
16941741> 1. If _pjson.exports_ is a String or Array, or an Object containing no
16951742> 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_.
16981748> 1. If _pjson.exports_ is an Object containing a _"."_ property, then
16991749> 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_.
17021755> 1. Throw a _Package Path Not Exported_ error.
17031756> 1. Let _legacyMainURL_ be the result applying the legacy
17041757> **LOAD_AS_DIRECTORY** CommonJS resolver to _packageURL_, throwing a
@@ -1712,31 +1765,37 @@ The resolver can throw the following errors:
17121765> 1. Set _packagePath_ to _"./"_ concatenated with _packagePath_.
17131766> 1. If _packagePath_ is a key of _exports_, then
17141767> 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_).
17171770> 1. Let _directoryKeys_ be the list of keys of _exports_ ending in
17181771> _"/"_, sorted by length descending.
17191772> 1. For each key _directory_ in _directoryKeys_, do
17201773> 1. If _packagePath_ starts with _directory_, then
17211774> 1. Let _target_ be the value of _exports\[ directory\] _.
17221775> 1. Let _subpath_ be the substring of _target_ starting at the index
17231776> 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** .
17271780
1728- **PACKAGE_EXPORTS_TARGET_RESOLVE **(_packageURL_, _target_, _subpath_, _env_)
1781+ **PACKAGE_TARGET_RESOLVE **(_packageURL_, _target_, _subpath_, _internal_ , _env_)
17291782
17301783> 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.
17341795> 1. Let _resolvedTarget_ be the URL resolution of the concatenation of
17351796> _packageURL_ and _target_.
17361797> 1. If _resolvedTarget_ is not contained in _packageURL_, throw an
17371798> _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.
17401799> 1. Let _resolved_ be the URL resolution of the concatenation of
17411800> _subpath_ and _resolvedTarget_.
17421801> 1. If _resolved_ is not contained in _resolvedTarget_, throw an
@@ -1748,22 +1807,48 @@ The resolver can throw the following errors:
17481807> 1. For each property _p_ of _target_, in object insertion order as,
17491808> 1. If _p_ equals _"default"_ or _env_ contains an entry for _p_, then
17501809> 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**.
17551815> 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** .
17571817> 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** .
17651825> 1. Otherwise throw an _Invalid Package Target_ error.
17661826
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+
17671852**ESM_FORMAT**(_url_)
17681853
17691854> 1. Assert: _url_ corresponds to an existing file.
0 commit comments