Prevent Crashes by Guarding babel.env Access in RN Babel Preset (#55918)

Summary:
Pull Request resolved: https://github.com/facebook/react-native/pull/55918

To prevent crashes when a Babel plugin or preset is invoked without a Babel API object, ensure that any access to Babel-specific properties (such as babel.env) is guarded. Some build tools or environments may call Babel plugins without providing the full Babel API instance, leading to runtime errors if the code assumes its presence. By using optional chaining and providing safe fallbacks, you can maintain compatibility across different build systems (such as Metro and others) without introducing breaking changes.

Changelog:
[General][Fixed] Prevent errorrs by handling contexts where the Babel API object is not provided

Reviewed By: robhogan, rubennorte

Differential Revision: D94952379

fbshipit-source-id: 65e69c0510bc66930ee3b5f28e20df32d732f990
This commit is contained in:
On Saint Tiong authored and meta-codesync[bot] committed 2026-03-04 11:07:31 -08:00
1 parent 6b32971f46
commit 63796a9160
1 file changed
+1 -1
+1 -1
View File
@@ -51,7 +51,7 @@ const getPreset = (src, options, babel) => {
const transformProfile =
options?.unstable_transformProfile ?? babel?.caller(getTransformProfile);
const dev = options?.dev ?? babel.env('development');
const dev = options?.dev ?? babel?.env('development') ?? false;
// Hermes V1 (aka Static Hermes) uses more optimised profiles.
// There is currently no difference between stable and canary, but canary