Skip to content

Commit 1f7b577

Browse files
committed
doc: esm: revisions per feedback
1 parent 6526130 commit 1f7b577

File tree

1 file changed

+36
-4
lines changed

1 file changed

+36
-4
lines changed

doc/api/esm.md

+36-4
Original file line numberDiff line numberDiff line change
@@ -499,6 +499,11 @@ create an ES module wrapper file that defines the named exports. Using
499499
ES module wrapper is used for `import` and the CommonJS entry point for
500500
`require`.
501501

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+
502507
<!-- eslint-skip -->
503508
```js
504509
// ./node_modules/pkg/package.json
@@ -553,13 +558,13 @@ This approach is appropriate for any of the following use cases:
553558
* The package stores internal state, and the package author would prefer not to
554559
refactor the package to isolate its state management. See the next section.
555560

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
558564
'pkg/module'` by users who are certain that the CommonJS version will not be
559565
loaded anywhere in the application, such as by dependencies; or if the CommonJS
560566
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):
563568

564569
<!-- eslint-skip -->
565570
```js
@@ -574,6 +579,11 @@ because the package is stateless). This variant would not require
574579
}
575580
```
576581

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+
577587
##### Approach #2: Isolate State
578588

579589
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:
659669
Even with isolated state, there is still the cost of possible extra code
660670
execution between the CommonJS and ES module versions of a package.
661671

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+
662694
## <code>import</code> Specifiers
663695

664696
### Terminology

0 commit comments

Comments
 (0)