Skip to content

Commit 171838a

Browse files
robhoganmeta-codesync[bot]
authored andcommitted
Pass unstable_transformProfile through Babel caller API and respect it @react-native/babel-preset
Differential Revision: D82625477
1 parent f2948fb commit 171838a

3 files changed

Lines changed: 23 additions & 7 deletions

File tree

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)