Default unstable_renameRequire to false in base config (#1776)#1776
Closed
robhogan wants to merge 2 commits into
Closed
Default unstable_renameRequire to false in base config (#1776)#1776robhogan wants to merge 2 commits into
robhogan wants to merge 2 commits into
Conversation
Contributor
|
@robhogan has exported this pull request. If you are a Meta employee, you can view the originating Diff in D111627295. |
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
Summary: Flips the OSS default of the `unstable_renameRequire` transformer option from `true` to `false` in `metro-config`. With renaming disabled, the scoped `require` function is left as `require` instead of being renamed to `_$$_REQUIRE` when serializing to the IIFE module factory (the default). Tested with repeated, interleaved, RNTester dev builds with a cold transform cache: ``` metric OFF (false) ON (true) delta cold build, mean 10.65 s 11.21 s -0.56 s (~5%) cold build, median 11.09 s 11.73 s -0.64 s (~5.5%) _$$_REQUIRE renames 0 3,534 -3,534 ``` NB: Expo already sets this to `false`, so this is a no-op for Expo users. ``` - **[Performance]** Don't rename `require` in bundle output, improve cold build time ~5%` ``` Reviewed By: huntie Differential Revision: D111627295
b16c814 to
c5b9908
Compare
Contributor
|
This pull request has been merged in e1b6a6a. |
meta-codesync Bot
pushed a commit
that referenced
this pull request
Jul 13, 2026
Summary: Removes the `unstable_renameRequire` transformer option entirely and bakes in the no-rename behaviour that is now the default across base Metro (flipped in the parent #1776) and Expo. Require renaming was vestigial from some previous static analysis, and was implemented in a particularly expensive way, with full re-traversal. ``` - **[Experimental]** Remove unstable_renameRequire - require is never renamed ``` Reviewed By: huntie Differential Revision: D111709130 fbshipit-source-id: 42fb81ed57b668e5bf38b1f2a760aad26fc26fef
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:
Flips the OSS default of the
unstable_renameRequiretransformer optionfrom
truetofalseinmetro-config. With renaming disabled, the scopedrequirefunction is left asrequireinstead of being renamed to_$$_REQUIREwhen serializing to the IIFE module factory (the default).Tested with repeated, interleaved, RNTester dev builds with a cold transform cache:
NB: Expo already sets this to
false, so this is a no-op for Expo users.Reviewed By: huntie
Differential Revision: D111627295