Skip to content

Commit 7d1cdd4

Browse files
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 <[email protected]> Co-authored-by: Antoine du Hamel <[email protected]> Co-authored-by: Rich Trott <[email protected]> Signed-off-by: Myles Borins <[email protected]> PR-URL: #35535 Reviewed-By: Guy Bedford <[email protected]> Reviewed-By: Matteo Collina <[email protected]> Reviewed-By: Rich Trott <[email protected]> Reviewed-By: Ujjwal Sharma <[email protected]> Reviewed-By: Benjamin Gruenbaum <[email protected]>
1 parent 62755b6 commit 7d1cdd4

File tree

1 file changed

+54
-21
lines changed

1 file changed

+54
-21
lines changed

doc/api/packages.md

+54-21
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
@@ -956,16 +1002,6 @@ added: v14.6.0
9561002

9571003
* Type: {Object}
9581004

959-
In addition to the [`"exports"`][] field it is possible to define internal
960-
package import maps that only apply to import specifiers from within the package
961-
itself.
962-
963-
Entries in the imports field must always start with `#` to ensure they are
964-
clearly disambiguated from package specifiers.
965-
966-
For example, the imports field can be used to gain the benefits of conditional
967-
exports for internal modules:
968-
9691005
```json
9701006
// package.json
9711007
{
@@ -981,15 +1017,11 @@ exports for internal modules:
9811017
}
9821018
```
9831019

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

988-
Unlike the `"exports"` field, import maps permit mapping to external packages,
989-
providing an important use case for conditional loading scenarios.
1022+
Import maps permit mapping to external packages.
9901023

991-
Apart from the above, the resolution rules for the imports field are otherwise
992-
analogous to the exports field.
1024+
This field defines [subpath imports][] for the current package.
9931025

9941026
[Babel]: https://babeljs.io/
9951027
[Conditional exports]: #packages_conditional_exports
@@ -1007,5 +1039,6 @@ analogous to the exports field.
10071039
[entry points]: #packages_package_entry_points
10081040
[self-reference]: #packages_self_referencing_a_package_using_its_name
10091041
[subpath exports]: #packages_subpath_exports
1042+
[subpath imports]: #packages_subpath_imports
10101043
[the full specifier path]: esm.md#esm_mandatory_file_extensions
10111044
[the dual CommonJS/ES module packages section]: #packages_dual_commonjs_es_module_packages

0 commit comments

Comments
 (0)