@@ -279,13 +279,53 @@ import submodule from 'es-module-package/private-module.js';
279
279
// Throws ERR_PACKAGE_PATH_NOT_EXPORTED
280
280
```
281
281
282
- ### Subpath export patterns
282
+ ### Subpath imports
283
283
284
284
> Stability: 1 - Experimental
285
285
286
- For packages with a small number of exports, we recommend explicitly listing
287
- each exports subpath entry. But for packages that have large numbers of
288
- subpaths, this might cause ` package.json ` bloat and maintenance issues.
286
+ In addition to the [ ` "exports" ` ] [ ] field, it is possible to define internal
287
+ package import maps that only apply to import specifiers from within the package
288
+ itself.
289
+
290
+ Entries in the imports field must always start with ` # ` to ensure they are
291
+ disambiguated from package specifiers.
292
+
293
+ For example, the imports field can be used to gain the benefits of conditional
294
+ exports for internal modules:
295
+
296
+ ``` json
297
+ // package.json
298
+ {
299
+ "imports" : {
300
+ "#dep" : {
301
+ "node" : " dep-node-native" ,
302
+ "default" : " ./dep-polyfill.js"
303
+ }
304
+ },
305
+ "dependencies" : {
306
+ "dep-node-native" : " ^1.0.0"
307
+ }
308
+ }
309
+ ```
310
+
311
+ where ` import '#dep' ` does not get the resolution of the external package
312
+ ` dep-node-native ` (including its exports in turn), and instead gets the local
313
+ file ` ./dep-polyfill.js ` relative to the package in other environments.
314
+
315
+ Unlike the ` "exports" ` field, the ` "imports" ` field permits mapping to external
316
+ packages.
317
+
318
+ The resolution rules for the imports field are otherwise
319
+ analogous to the exports field.
320
+
321
+ ### Subpath patterns
322
+
323
+ > Stability: 1 - Experimental
324
+
325
+ For packages with a small number of exports or imports, we recommend
326
+ explicitly listing each exports subpath entry. But for packages that have
327
+ large numbers of subpaths, this might cause ` package.json ` bloat and
328
+ maintenance issues.
289
329
290
330
For these use cases, subpath export patterns can be used instead:
291
331
@@ -294,6 +334,9 @@ For these use cases, subpath export patterns can be used instead:
294
334
{
295
335
"exports" : {
296
336
"./features/*" : " ./src/features/*.js"
337
+ },
338
+ "imports" : {
339
+ "#internal/*" : " ./src/internal/*.js"
297
340
}
298
341
}
299
342
```
@@ -308,6 +351,9 @@ import featureX from 'es-module-package/features/x';
308
351
309
352
import featureY from ' es-module-package/features/y/y' ;
310
353
// Loads ./node_modules/es-module-package/src/features/y/y.js
354
+
355
+ import internalZ from ' #internal/z' ;
356
+ // Loads ./node_modules/es-module-package/src/internal/z.js
311
357
```
312
358
313
359
This is a direct static replacement without any special handling for file
@@ -947,16 +993,6 @@ added: v12.19.0
947
993
948
994
* Type: {Object }
949
995
950
- In addition to the [` "exports"` ][] field it is possible to define internal
951
- package import maps that only apply to import specifiers from within the package
952
- itself.
953
-
954
- Entries in the imports field must always start with ` #` to ensure they are
955
- clearly disambiguated from package specifiers.
956
-
957
- For example, the imports field can be used to gain the benefits of conditional
958
- exports for internal modules:
959
-
960
996
` ` ` json
961
997
// package.json
962
998
{
@@ -972,15 +1008,11 @@ exports for internal modules:
972
1008
}
973
1009
` ` `
974
1010
975
- where ` import '#dep'` would now get the resolution of the external package
976
- ` dep-node-native` (including its exports in turn), and instead get the local
977
- file ` ./dep-polyfill.js` relative to the package in other environments.
1011
+ Entries in the imports field must be strings starting with ` #` .
978
1012
979
- Unlike the ` "exports"` field, import maps permit mapping to external packages,
980
- providing an important use case for conditional loading scenarios.
1013
+ Import maps permit mapping to external packages.
981
1014
982
- Apart from the above, the resolution rules for the imports field are otherwise
983
- analogous to the exports field.
1015
+ This field defines [subpath imports][] for the current package .
984
1016
985
1017
[Babel]: https: // babeljs.io/
986
1018
[Conditional exports ]: #packages_conditional_exports
@@ -998,5 +1030,6 @@ analogous to the exports field.
998
1030
[` package.json` ]: #packages_node_js_package_json_field_definitions
999
1031
[self - reference]: #packages_self_referencing_a_package_using_its_name
1000
1032
[subpath exports ]: #packages_subpath_exports
1001
- [the full specifier path]: modules_esm .html #modules_esm_mandatory_file_extensions
1033
+ [subpath imports]: #packages_subpath_imports
1034
+ [the full specifier path]: esm .md #esm_mandatory_file_extensions
1002
1035
[the dual CommonJS/ ES module packages section]: #packages_dual_commonjs_es_module_packages
0 commit comments