Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 19 additions & 5 deletions packages/react-native-babel-preset/src/configs/main.js
Original file line number Diff line number Diff line change
Expand Up @@ -39,12 +39,26 @@ 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 on `options`. Should be pure and cheap.
function getTransformProfile(caller) {
if (caller.unstable_transformProfile != null) {
return caller.unstable_transformProfile;
} else {
return '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) ??
'default';

const isHermesStable = transformProfile === 'hermes-stable';
const isHermesCanary = transformProfile === 'hermes-canary';
const isHermes = isHermesStable || isHermesCanary;
Expand Down Expand Up @@ -252,14 +266,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;
2 changes: 1 addition & 1 deletion packages/react-native-babel-preset/src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
9 changes: 8 additions & 1 deletion packages/react-native-babel-transformer/src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
Loading