From 32c3d2f35cff400c5d957be76c88627ae776bfaf Mon Sep 17 00:00:00 2001 From: Rob Hogan Date: Mon, 5 Jan 2026 06:28:15 -0800 Subject: [PATCH] Pass `unstable_transformProfile` through Babel `caller` API and fall back to it in `@react-native/babel-preset` (#54967) Summary: Pull Request resolved: https://github.com/facebook/react-native/pull/54967 `unstable_transformProfile` is used by Metro to hint at the appropriate set of Babel transforms for the target engine. To be effective in `react-native/babel-preset`, it must currently be passed in the preset config. When the user does not use a custom Babel config, `react-native/metro-babel-transformer` detects that and auto-configures the preset with options from Metro - that's fine. But if the user has their own Babel config, building on top of `react-native/babel-preset`, the preset for the config is fixed - so `unstable_transformProfile` has no effect. Ideally, `unstable_transformProfile` should be a function of the target engine - it's totally reasonable for one instance of Metro to build for multiple engines - so this should instead be passed programmatically to the engine by Metro for each transform job. Babel provides a mechanism to do this efficiently, without reinitialising the preset unnecessarily, via the `caller` API. This passes `unstable_transformProfile` through `caller`. Changelog: [Internal] (This is part of using a more optimised transform profiles for Hermes V1, which is still an experimental opt-in. When we stabilise Hermes V1 in RN, we should stabilise this API) Reviewed By: vzaidman, huntie Differential Revision: D82625477 fbshipit-source-id: 76a4d73d192c2deb03bed2064856e526de25f2ec --- .../src/configs/main.js | 19 ++++++++++++++----- .../react-native-babel-preset/src/index.js | 2 +- .../src/index.js | 9 ++++++++- 3 files changed, 23 insertions(+), 7 deletions(-) diff --git a/packages/react-native-babel-preset/src/configs/main.js b/packages/react-native-babel-preset/src/configs/main.js index d4fc7e10378..06e3d14e857 100644 --- a/packages/react-native-babel-preset/src/configs/main.js +++ b/packages/react-native-babel-preset/src/configs/main.js @@ -39,12 +39,21 @@ function isFirstParty(fileName) { ); } +// Called by Babel whenever caller information changes between transform calls +// for a given config. If the return value changes, Babel re-evaluates +// getPreset, which is otherwise cached based on `options`. This must be pure, +// and should be cheap. +function getTransformProfile(caller) { + return caller?.unstable_transformProfile ?? 'default'; +} + // use `this.foo = bar` instead of `this.defineProperty('foo', ...)` const loose = true; -const getPreset = (src, options) => { +const getPreset = (src, options, babel) => { const transformProfile = - (options && options.unstable_transformProfile) || 'default'; + options?.unstable_transformProfile ?? babel?.caller(getTransformProfile); + const isHermesStable = transformProfile === 'hermes-stable'; const isHermesCanary = transformProfile === 'hermes-canary'; const isHermes = isHermesStable || isHermesCanary; @@ -252,14 +261,14 @@ const getPreset = (src, options) => { }; }; -module.exports = options => { +module.exports = (options, babel) => { if (options.withDevTools == null) { const env = process.env.BABEL_ENV || process.env.NODE_ENV; if (!env || env === 'development') { - return getPreset(null, {...options, dev: true}); + return getPreset(null, {...options, dev: true}, babel); } } - return getPreset(null, options); + return getPreset(null, options, babel); }; module.exports.getPreset = getPreset; diff --git a/packages/react-native-babel-preset/src/index.js b/packages/react-native-babel-preset/src/index.js index d2b5fc98721..e785c975938 100644 --- a/packages/react-native-babel-preset/src/index.js +++ b/packages/react-native-babel-preset/src/index.js @@ -14,7 +14,7 @@ const {version: packageVersion} = require('../package.json'); const main = require('./configs/main'); module.exports = function (babel, options) { - return main(options); + return main(options, babel); }; let cacheKey; diff --git a/packages/react-native-babel-transformer/src/index.js b/packages/react-native-babel-transformer/src/index.js index d5e541c370a..358d62f175b 100644 --- a/packages/react-native-babel-transformer/src/index.js +++ b/packages/react-native-babel-transformer/src/index.js @@ -193,7 +193,14 @@ const transform /*: BabelTransformer['transform'] */ = ({ // ES modules require sourceType='module' but OSS may not always want that sourceType: 'unambiguous', ...buildBabelConfig(filename, options, plugins), - caller: {name: 'metro', bundler: 'metro', platform: options.platform}, + caller: { + // Varies Babel's config cache - presets will be re-initialized + // if they use caller information. + name: 'metro', + bundler: 'metro', + platform: options.platform, + unstable_transformProfile: options.unstable_transformProfile, + }, ast: true, // NOTE(EvanBacon): We split the parse/transform steps up to accommodate