Skip to content

Commit 17174e6

Browse files
guybedfordMylesBorins
authored andcommitted
doc: clarify conditional exports guidance
PR-URL: #34306 Reviewed-By: Rich Trott <[email protected]> Reviewed-By: James M Snell <[email protected]> Reviewed-By: Michaël Zasso <[email protected]> Reviewed-By: Jan Krems <[email protected]>
1 parent 1dd2653 commit 17174e6

File tree

1 file changed

+13
-5
lines changed

1 file changed

+13
-5
lines changed

doc/api/esm.md

+13-5
Original file line numberDiff line numberDiff line change
@@ -456,16 +456,24 @@ Conditional exports can also be extended to exports subpaths, for example:
456456
"exports": {
457457
".": "./main.js",
458458
"./feature": {
459-
"browser": "./feature-browser.js",
459+
"node": "./feature-node.js",
460460
"default": "./feature.js"
461461
}
462462
}
463463
}
464464
```
465465

466466
Defines a package where `require('pkg/feature')` and `import 'pkg/feature'`
467-
could provide different implementations between the browser and Node.js,
468-
given third-party tool support for a `"browser"` condition.
467+
could provide different implementations between Node.js and other JS
468+
environments.
469+
470+
When using environment branches, always include a `"default"` condition where
471+
possible. Providing a `"default"` condition ensures that any unknown JS
472+
environments are able to use this universal implementation, which helps avoid
473+
these JS environments from having to pretend to be existing environments in
474+
order to support packages with conditional exports. For this reason, using
475+
`"node"` and `"default"` condition branches is usually preferable to using
476+
`"node"` and `"browser"` condition branches.
469477

470478
#### Nested conditions
471479

@@ -479,11 +487,11 @@ use in Node.js but not the browser:
479487
{
480488
"main": "./main.js",
481489
"exports": {
482-
"browser": "./feature-browser.mjs",
483490
"node": {
484491
"import": "./feature-node.mjs",
485492
"require": "./feature-node.cjs"
486-
}
493+
},
494+
"default": "./feature.mjs",
487495
}
488496
}
489497
```

0 commit comments

Comments
 (0)