Enable allowOptionalDependencies by default (#1775)#1775
Closed
robhogan wants to merge 1 commit into
Closed
Conversation
Contributor
|
@robhogan has exported this pull request. If you are a Meta employee, you can view the originating Diff in D111577052. |
Summary:
Flip the default value of the `transformer.allowOptionalDependencies` Metro config option from `false` to `true` in `metro-config`'s own defaults.
This allows for patterns like:
```js
let dep = null;
try {
// If not installed, allowOptionalDependencies ===
// true => throw at runtime, handled by catch
// false => throw at build time
dep = require('maybe-installed-dependency');
} catch {
dep = require('./my-fallback');
}
```
Which comes up in popular packages, eg [`lru-cache`](https://github.com/isaacs/node-lru-cache/blob/16b3a916662ab449d496b7b4b4f04132565d1d28/src/diagnostics-channel-esm.mts#L24-L29).
Note that this is *already the default* in:
- `react-native/metro-config`: https://github.com/react/react-native/blob/a632f9efe24bac8b3a113c78469948f55bde0f5d/packages/metro-config/src/index.flow.js#L86
- And `expo/metro-config`: https://github.com/expo/expo/blob/ab042edb8228e3873bdbd8e04b7c61cd3e00372e/packages/%40expo/metro-config/src/ExpoMetroConfig.ts#L423
There were previously known problems with `allowOptionalDependencies` and the behaviour of subsequently declared dependencies, and they were also broken under HMR if their existence changed. These were both fixed a year ago in #1522.
The heuristic was also quite limited, for example a `try { require() }` was considered optional but an `import().catch()` was not. That was fixed in #1697.
I can't think of any good reason not to enable this and fix the misalignment with RN/Expo.
With this enabled, dependencies referenced inside `try/catch` blocks (and dynamic `import()`s with rejection handlers) are treated as optional by default: an unresolvable optional dependency no longer fails the build, matching the common expectation for guarded requires. Projects can still opt out by explicitly setting `allowOptionalDependencies: false`.
Since this change doesn't alter observable runtime behaviour (it allows bundles to build that would otherwise fail to build), I'm inclined to regard it as non-breaking.
```
- **[Feature]**: `allowOptionalDependencies` defaults to `true` to align with RN and Expo
```
Reviewed By: huntie
Differential Revision: D111577052
allowOptionalDependencies by defaultallowOptionalDependencies by default (#1775)
272ce4c to
ca92b8f
Compare
meta-codesync Bot
pushed a commit
that referenced
this pull request
Jul 13, 2026
Summary:
Flip the default value of the `transformer.allowOptionalDependencies` Metro config option from `false` to `true` in `metro-config`'s own defaults.
This allows for patterns like:
```js
let dep = null;
try {
// If not installed, allowOptionalDependencies ===
// true => throw at runtime, handled by catch
// false => throw at build time
dep = require('maybe-installed-dependency');
} catch {
dep = require('./my-fallback');
}
```
Which comes up in popular packages, eg [`lru-cache`](https://github.com/isaacs/node-lru-cache/blob/16b3a916662ab449d496b7b4b4f04132565d1d28/src/diagnostics-channel-esm.mts#L24-L29).
Note that this is *already the default* in:
- `react-native/metro-config`: https://github.com/react/react-native/blob/a632f9efe24bac8b3a113c78469948f55bde0f5d/packages/metro-config/src/index.flow.js#L86
- And `expo/metro-config`: https://github.com/expo/expo/blob/ab042edb8228e3873bdbd8e04b7c61cd3e00372e/packages/%40expo/metro-config/src/ExpoMetroConfig.ts#L423
There were previously known problems with `allowOptionalDependencies` and the behaviour of subsequently declared dependencies, and they were also broken under HMR if their existence changed. These were both fixed a year ago in #1522.
The heuristic was also quite limited, for example a `try { require() }` was considered optional but an `import().catch()` was not. That was fixed in #1697.
I can't think of any good reason not to enable this and fix the misalignment with RN/Expo.
With this enabled, dependencies referenced inside `try/catch` blocks (and dynamic `import()`s with rejection handlers) are treated as optional by default: an unresolvable optional dependency no longer fails the build, matching the common expectation for guarded requires. Projects can still opt out by explicitly setting `allowOptionalDependencies: false`.
Since this change doesn't alter observable runtime behaviour (it allows bundles to build that would otherwise fail to build), I'm inclined to regard it as non-breaking.
```
- **[Feature]**: `allowOptionalDependencies` defaults to `true` to align with RN and Expo
```
Reviewed By: huntie
Differential Revision: D111577052
Contributor
|
This pull request has been merged in 948f730. |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary:
Flip the default value of the
transformer.allowOptionalDependenciesMetro config option fromfalsetotrueinmetro-config's own defaults.This allows for patterns like:
Which comes up in popular packages, eg
lru-cache.Note that this is already the default in:
react-native/metro-config: https://github.com/react/react-native/blob/a632f9efe24bac8b3a113c78469948f55bde0f5d/packages/metro-config/src/index.flow.js#L86expo/metro-config: https://github.com/expo/expo/blob/ab042edb8228e3873bdbd8e04b7c61cd3e00372e/packages/%40expo/metro-config/src/ExpoMetroConfig.ts#L423There were previously known problems with
allowOptionalDependenciesand the behaviour of subsequently declared dependencies, and they were also broken under HMR if their existence changed. These were both fixed a year ago in #1522.The heuristic was also quite limited, for example a
try { require() }was considered optional but animport().catch()was not. That was fixed in #1697.I can't think of any good reason not to enable this and fix the misalignment with RN/Expo.
With this enabled, dependencies referenced inside
try/catchblocks (and dynamicimport()s with rejection handlers) are treated as optional by default: an unresolvable optional dependency no longer fails the build, matching the common expectation for guarded requires. Projects can still opt out by explicitly settingallowOptionalDependencies: false.Since this change doesn't alter observable runtime behaviour (it allows bundles to build that would otherwise fail to build), I'm inclined to regard it as non-breaking.
Reviewed By: huntie
Differential Revision: D111577052