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
This commit is contained in:
Rob Hogan authored and meta-codesync[bot] committed 2026-01-05 06:28:15 -08:00
1 parent 8f10b339d4
commit 32c3d2f35c
3 files changed
+23 -7

No files matched your search

+14 -5
View File
@@ -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;
+1 -1
View File
@@ -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;
+8 -1
View File
@@ -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