@@ -782,6 +782,40 @@ a nested condition does not have any mapping it will continue checking |
782 | 782 | the remaining conditions of the parent condition. In this way nested |
783 | 783 | conditions behave analogously to nested JavaScript `if` statements. |
784 | 784 |
|
| 785 | +### Target fallback arrays |
| 786 | + |
| 787 | +Anywhere a target is accepted in the [`"exports"`][] or [`"imports"`][] fields, |
| 788 | +an array of targets can be used instead. Node.js tries the items in order and |
| 789 | +uses the first one that is a valid target. An item is skipped, and the next one |
| 790 | +is tried, only when: |
| 791 | + |
| 792 | +* it is not a valid target, for example a string that does not follow the |
| 793 | + [path rules for export targets][], or |
| 794 | +* it is a conditions object in which no condition matches. |
| 795 | + |
| 796 | +Fallback arrays exist for compatibility: a package can list a target that only |
| 797 | +some environments or future versions of Node.js treat as valid, followed by one |
| 798 | +that all of them support. |
| 799 | + |
| 800 | +Node.js does not check whether the file a target points to exists. Once an |
| 801 | +item is a valid target, it is used and the remaining items are ignored, even if |
| 802 | +loading the file then fails because it is missing: |
| 803 | + |
| 804 | +```json |
| 805 | +{ |
| 806 | + "exports": { |
| 807 | + "./feature": ["./src/feature.js", "./dist/feature.js"] |
| 808 | + } |
| 809 | +} |
| 810 | +``` |
| 811 | + |
| 812 | +Here `./feature` always resolves to `./src/feature.js`. An array cannot be used |
| 813 | +to fall back from a missing file to another file. |
| 814 | + |
| 815 | +If the array is empty, the subpath is not exported. If every item is an |
| 816 | +invalid target, the error for the last item is thrown. The exact behavior is |
| 817 | +defined by `PACKAGE_TARGET_RESOLVE` in the [package resolution algorithm][]. |
| 818 | + |
785 | 819 | ### Resolving user conditions |
786 | 820 |
|
787 | 821 | <!-- YAML |
@@ -1417,7 +1451,9 @@ Node.js allows type stripping for: |
1417 | 1451 | [import maps]: https://github.com/WICG/import-maps |
1418 | 1452 | [load ECMAScript modules from CommonJS modules]: modules.md#loading-ecmascript-modules-using-require |
1419 | 1453 | [merve]: https://github.com/anonrig/merve |
| 1454 | +[package resolution algorithm]: esm.md#resolution-algorithm-specification |
1420 | 1455 | [packages folder mapping]: https://github.com/WICG/import-maps#packages-via-trailing-slashes |
| 1456 | +[path rules for export targets]: #path-rules-and-validation-for-export-targets |
1421 | 1457 | [resolution algorithm pseudo-code]: modules.md#all-together |
1422 | 1458 | [self-reference]: #self-referencing-a-package-using-its-name |
1423 | 1459 | [subpath exports]: #subpath-exports |
|