Skip to content

Commit 32c3d2f

Browse files
robhoganmeta-codesync[bot]
authored andcommitted
Pass unstable_transformProfile through Babel caller API and fall back to it in @react-native/babel-preset (#54967)
Summary: Pull Request resolved: #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
1 parent 8f10b33 commit 32c3d2f

File tree

3 files changed

+23
-7
lines changed

3 files changed

+23
-7
lines changed

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

Lines changed: 14 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -39,12 +39,21 @@ 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 based on `options`. This must be pure,
45+
// and should be cheap.
46+
function getTransformProfile(caller) {
47+
return caller?.unstable_transformProfile ?? 'default';
48+
}
49+
4250
// use `this.foo = bar` instead of `this.defineProperty('foo', ...)`
4351
const loose = true;
4452

45-
const getPreset = (src, options) => {
53+
const getPreset = (src, options, babel) => {
4654
const transformProfile =
47-
(options && options.unstable_transformProfile) || 'default';
55+
options?.unstable_transformProfile ?? babel?.caller(getTransformProfile);
56+
4857
const isHermesStable = transformProfile === 'hermes-stable';
4958
const isHermesCanary = transformProfile === 'hermes-canary';
5059
const isHermes = isHermesStable || isHermesCanary;
@@ -252,14 +261,14 @@ const getPreset = (src, options) => {
252261
};
253262
};
254263

255-
module.exports = options => {
264+
module.exports = (options, babel) => {
256265
if (options.withDevTools == null) {
257266
const env = process.env.BABEL_ENV || process.env.NODE_ENV;
258267
if (!env || env === 'development') {
259-
return getPreset(null, {...options, dev: true});
268+
return getPreset(null, {...options, dev: true}, babel);
260269
}
261270
}
262-
return getPreset(null, options);
271+
return getPreset(null, options, babel);
263272
};
264273

265274
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)