Skip to content

Commit f1c4999

Browse files
MylesBorinsguybedfordaduh95Trott
committed
doc: move package.import content higher
This is currently at the end of the doc, it likely should be found right after the documentation about `package.exports`. Refactored the docs while duplicating content as little as possible. Co-authored-by: Guy Bedford <guybedford@gmail.com> Co-authored-by: Antoine du Hamel <duhamelantoine1995@gmail.com> Co-authored-by: Rich Trott <rtrott@gmail.com> Signed-off-by: Myles Borins <mylesborins@github.com>
1 parent bc0c094 commit f1c4999

File tree

1 file changed

+54
-21
lines changed

1 file changed

+54
-21
lines changed

doc/api/packages.md

Lines changed: 54 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -282,13 +282,53 @@ import submodule from 'es-module-package/private-module.js';
282282
// Throws ERR_PACKAGE_PATH_NOT_EXPORTED
283283
```
284284

285-
### Subpath export patterns
285+
### Subpath imports
286286

287287
> Stability: 1 - Experimental
288288
289-
For packages with a small number of exports, we recommend explicitly listing
290-
each exports subpath entry. But for packages that have large numbers of
291-
subpaths, this might cause `package.json` bloat and maintenance issues.
289+
In addition to the [`"exports"`][] field, it is possible to define internal
290+
package import maps that only apply to import specifiers from within the package
291+
itself.
292+
293+
Entries in the imports field must always start with `#` to ensure they are
294+
disambiguated from package specifiers.
295+
296+
For example, the imports field can be used to gain the benefits of conditional
297+
exports for internal modules:
298+
299+
```json
300+
// package.json
301+
{
302+
"imports": {
303+
"#dep": {
304+
"node": "dep-node-native",
305+
"default": "./dep-polyfill.js"
306+
}
307+
},
308+
"dependencies": {
309+
"dep-node-native": "^1.0.0"
310+
}
311+
}
312+
```
313+
314+
where `import '#dep'` does not get the resolution of the external package
315+
`dep-node-native` (including its exports in turn), and instead gets the local
316+
file `./dep-polyfill.js` relative to the package in other environments.
317+
318+
Unlike the `"exports"` field, the `"imports"` field permits mapping to external
319+
packages.
320+
321+
The resolution rules for the imports field are otherwise
322+
analogous to the exports field.
323+
324+
### Subpath patterns
325+
326+
> Stability: 1 - Experimental
327+
328+
For packages with a small number of exports or imports, we recommend
329+
explicitly listing each exports subpath entry. But for packages that have
330+
large numbers of subpaths, this might cause `package.json` bloat and
331+
maintenance issues.
292332

293333
For these use cases, subpath export patterns can be used instead:
294334

@@ -297,6 +337,9 @@ For these use cases, subpath export patterns can be used instead:
297337
{
298338
"exports": {
299339
"./features/*": "./src/features/*.js"
340+
},
341+
"imports": {
342+
"#internal/*": "./src/internal/*.js"
300343
}
301344
}
302345
```
@@ -311,6 +354,9 @@ import featureX from 'es-module-package/features/x';
311354

312355
import featureY from 'es-module-package/features/y/y';
313356
// Loads ./node_modules/es-module-package/src/features/y/y.js
357+
358+
import internalZ from '#internal/z';
359+
// Loads ./node_modules/es-module-package/src/internal/z.js
314360
```
315361

316362
This is a direct static replacement without any special handling for file
@@ -952,16 +998,6 @@ added: v14.6.0
952998

953999
* Type: {Object}
9541000

955-
In addition to the [`"exports"`][] field it is possible to define internal
956-
package import maps that only apply to import specifiers from within the package
957-
itself.
958-
959-
Entries in the imports field must always start with `#` to ensure they are
960-
clearly disambiguated from package specifiers.
961-
962-
For example, the imports field can be used to gain the benefits of conditional
963-
exports for internal modules:
964-
9651001
```json
9661002
// package.json
9671003
{
@@ -977,15 +1013,11 @@ exports for internal modules:
9771013
}
9781014
```
9791015

980-
where `import '#dep'` would now get the resolution of the external package
981-
`dep-node-native` (including its exports in turn), and instead get the local
982-
file `./dep-polyfill.js` relative to the package in other environments.
1016+
Entries in the imports field must be strings starting with `#`.
9831017

984-
Unlike the `"exports"` field, import maps permit mapping to external packages,
985-
providing an important use case for conditional loading scenarios.
1018+
Import maps permit mapping to external packages.
9861019

987-
Apart from the above, the resolution rules for the imports field are otherwise
988-
analogous to the exports field.
1020+
This field defines [subpath imports][] for the current package.
9891021

9901022
[Babel]: https://babeljs.io/
9911023
[Conditional exports]: #packages_conditional_exports
@@ -1003,5 +1035,6 @@ analogous to the exports field.
10031035
[entry points]: #packages_package_entry_points
10041036
[self-reference]: #packages_self_referencing_a_package_using_its_name
10051037
[subpath exports]: #packages_subpath_exports
1038+
[subpath imports]: #packages_subpath_imports
10061039
[the full specifier path]: esm.md#esm_mandatory_file_extensions
10071040
[the dual CommonJS/ES module packages section]: #packages_dual_commonjs_es_module_packages

0 commit comments

Comments
 (0)