@@ -499,6 +499,11 @@ create an ES module wrapper file that defines the named exports. Using
499
499
ES module wrapper is used for ` import ` and the CommonJS entry point for
500
500
` require ` .
501
501
502
+ > Note: While ` --experimental-conditional-exports ` is flagged, a package
503
+ > using this pattern will throw when loaded via ` require() ` in modern
504
+ > Node.js, unless package consumers use the ` --experimental-conditional-exports `
505
+ > flag.
506
+
502
507
<!-- eslint-skip -->
503
508
``` js
504
509
// ./node_modules/pkg/package.json
@@ -553,13 +558,13 @@ This approach is appropriate for any of the following use cases:
553
558
* The package stores internal state, and the package author would prefer not to
554
559
refactor the package to isolate its state management. See the next section.
555
560
556
- A variant of this approach would add an export, e.g. ` "./module" ` , to point to
557
- an all-ES module-syntax version the package. This could be used via `import
561
+ A variant of this approach not requiring ` --experimental-conditional-exports `
562
+ for consumers could be to add an export, e.g. ` "./module" ` , to point to an
563
+ all-ES module-syntax version the package. This could be used via `import
558
564
'pkg/module'` by users who are certain that the CommonJS version will not be
559
565
loaded anywhere in the application, such as by dependencies; or if the CommonJS
560
566
version can be loaded but doesn’t affect the ES module version (for example,
561
- because the package is stateless). This variant would not require
562
- ` --experimental-conditional-exports ` :
567
+ because the package is stateless):
563
568
564
569
<!-- eslint-skip -->
565
570
``` js
@@ -574,6 +579,11 @@ because the package is stateless). This variant would not require
574
579
}
575
580
```
576
581
582
+ If the ` --experimental-conditional-exports ` flag is dropped and therefore
583
+ [ Conditional Exports] [ ] become available without a flag, this variant could be
584
+ easily updated to use conditional exports by adding conditions to the ` "." `
585
+ path; while keeping ` "./module" ` for backward compatibility.
586
+
577
587
##### Approach #2 : Isolate State
578
588
579
589
The most straightforward ` package.json ` would be one that defines the separate
@@ -659,6 +669,28 @@ This approach is appropriate for any of the following use cases:
659
669
Even with isolated state, there is still the cost of possible extra code
660
670
execution between the CommonJS and ES module versions of a package.
661
671
672
+ As with the previous approach, a variant of this approach not requiring
673
+ `--experimental-conditional-exports` for consumers could be to add an export,
674
+ e.g. `"./module"`, to point to an all-ES module-syntax version the package:
675
+
676
+ <!-- eslint-skip -->
677
+ ```js
678
+ // ./node_modules/pkg/package.json
679
+ {
680
+ " type" : " module" ,
681
+ " main" : " ./index.cjs" ,
682
+ " exports" : {
683
+ " ." : " ./index.cjs" ,
684
+ " ./module" : " ./index.mjs"
685
+ }
686
+ }
687
+ ```
688
+
689
+ If the ` --experimental-conditional-exports ` flag is dropped and therefore
690
+ [ Conditional Exports] [ ] become available without a flag, this variant could be
691
+ easily updated to use conditional exports by adding conditions to the ` "." `
692
+ path; while keeping ` "./module" ` for backward compatibility.
693
+
662
694
## <code >import</code > Specifiers
663
695
664
696
### Terminology
0 commit comments