Skip to content

Commit 6ad661d

Browse files
robhoganfacebook-github-bot
authored andcommitted
Pass unstable_transformProfile through Babel caller API and fall back to it in @react-native/babel-preset (#54967)
Summary: `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) Differential Revision: D82625477
1 parent c98494b commit 6ad661d

3 files changed

Lines changed: 27 additions & 7 deletions

File tree

packages/react-native-babel-preset/src/configs/main.js

Lines changed: 18 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -39,12 +39,25 @@ function isFirstParty(fileName) {
3939
);
4040
}
4141

42+
// Called by Babel whenever caller information changes between transform calls
43+
// for a given config. If the return value changes, Babel re-evaluates
44+
// getPreset, which is otherwise cached on `options`. Should be pure and cheap.
45+
function getTransformProfile(caller) {
46+
if (caller?.unstable_transformProfile != null) {
47+
return caller.unstable_transformProfile;
48+
}
49+
return 'default';
50+
}
51+
4252
// use `this.foo = bar` instead of `this.defineProperty('foo', ...)`
4353
const loose = true;
4454

45-
const getPreset = (src, options) => {
55+
const getPreset = (src, options, babel) => {
4656
const transformProfile =
47-
(options && options.unstable_transformProfile) || 'default';
57+
options?.unstable_transformProfile ??
58+
babel?.caller(getTransformProfile) ??
59+
'default';
60+
4861
const isHermesStable = transformProfile === 'hermes-stable';
4962
const isHermesCanary = transformProfile === 'hermes-canary';
5063
const isHermes = isHermesStable || isHermesCanary;
@@ -252,14 +265,14 @@ const getPreset = (src, options) => {
252265
};
253266
};
254267

255-
module.exports = options => {
268+
module.exports = (options, babel) => {
256269
if (options.withDevTools == null) {
257270
const env = process.env.BABEL_ENV || process.env.NODE_ENV;
258271
if (!env || env === 'development') {
259-
return getPreset(null, {...options, dev: true});
272+
return getPreset(null, {...options, dev: true}, babel);
260273
}
261274
}
262-
return getPreset(null, options);
275+
return getPreset(null, options, babel);
263276
};
264277

265278
module.exports.getPreset = getPreset;

packages/react-native-babel-preset/src/index.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ const {version: packageVersion} = require('../package.json');
1414
const main = require('./configs/main');
1515

1616
module.exports = function (babel, options) {
17-
return main(options);
17+
return main(options, babel);
1818
};
1919

2020
let cacheKey;

packages/react-native-babel-transformer/src/index.js

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -193,7 +193,14 @@ const transform /*: BabelTransformer['transform'] */ = ({
193193
// ES modules require sourceType='module' but OSS may not always want that
194194
sourceType: 'unambiguous',
195195
...buildBabelConfig(filename, options, plugins),
196-
caller: {name: 'metro', bundler: 'metro', platform: options.platform},
196+
caller: {
197+
// Varies Babel's config cache - presets will be re-initialized
198+
// if they use caller information.
199+
name: 'metro',
200+
bundler: 'metro',
201+
platform: options.platform,
202+
unstable_transformProfile: options.unstable_transformProfile,
203+
},
197204
ast: true,
198205

199206
// NOTE(EvanBacon): We split the parse/transform steps up to accommodate

0 commit comments

Comments
 (0)