@@ -282,13 +282,53 @@ import submodule from 'es-module-package/private-module.js';
282
282
// Throws ERR_PACKAGE_PATH_NOT_EXPORTED
283
283
```
284
284
285
- ### Subpath export patterns
285
+ ### Subpath imports
286
286
287
287
> Stability: 1 - Experimental
288
288
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.
292
332
293
333
For these use cases, subpath export patterns can be used instead:
294
334
@@ -297,6 +337,9 @@ For these use cases, subpath export patterns can be used instead:
297
337
{
298
338
"exports" : {
299
339
"./features/*" : " ./src/features/*.js"
340
+ },
341
+ "imports" : {
342
+ "#internal/*" : " ./src/internal/*.js"
300
343
}
301
344
}
302
345
```
@@ -311,6 +354,9 @@ import featureX from 'es-module-package/features/x';
311
354
312
355
import featureY from ' es-module-package/features/y/y' ;
313
356
// 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
314
360
```
315
361
316
362
This is a direct static replacement without any special handling for file
@@ -956,16 +1002,6 @@ added: v14.6.0
956
1002
957
1003
* Type: {Object }
958
1004
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
-
969
1005
` ` ` json
970
1006
// package.json
971
1007
{
@@ -981,15 +1017,11 @@ exports for internal modules:
981
1017
}
982
1018
` ` `
983
1019
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 ` #` .
987
1021
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.
990
1023
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 .
993
1025
994
1026
[Babel]: https: // babeljs.io/
995
1027
[Conditional exports ]: #packages_conditional_exports
@@ -1007,5 +1039,6 @@ analogous to the exports field.
1007
1039
[entry points]: #packages_package_entry_points
1008
1040
[self - reference]: #packages_self_referencing_a_package_using_its_name
1009
1041
[subpath exports ]: #packages_subpath_exports
1042
+ [subpath imports]: #packages_subpath_imports
1010
1043
[the full specifier path]: esm .md #esm_mandatory_file_extensions
1011
1044
[the dual CommonJS/ ES module packages section]: #packages_dual_commonjs_es_module_packages
0 commit comments