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
Original file line number Diff line number Diff line change
Expand Up @@ -17,9 +17,7 @@ const PROJECT_ROOT = path.sep === '/' ? '/my/project' : 'C:\\my\\project';
const RN_ROOT = path.join(PROJECT_ROOT, 'node_modules', 'react-native');

// The transformer memoizes its resolved Babel config in a module-level
// closure, keyed on nothing - so a fresh module instance is required for every
// distinct `options` shape, or later variants silently reuse the first
// variant's config.
// closure, so a fresh module instance keeps each test independent.
beforeEach(() => {
jest.resetModules();
});
Expand Down Expand Up @@ -171,9 +169,8 @@ describe.each([false, true])(
);

test('the two import-support modes really do produce different output', () => {
// Guards the test setup itself: without a module reset between variants the
// memoized config leaks and the parameterized suite above would silently run
// the same configuration twice.
// Guards the test setup itself: the parameterized suite above must really
// run two different configurations.
const src = "import {Platform} from 'react-native';\nconst x = Other.thing;";

const lowered = transformToCode(src, {experimentalImportSupport: false});
Expand All @@ -185,6 +182,17 @@ test('the two import-support modes really do produce different output', () => {
expect(preserved).not.toContain('require');
});

test('inlines each platform from one module instance', () => {
const src = "import {Platform} from 'react-native';\nconst os = Platform.OS;";

const ios = transformToCode(src, {platform: 'ios'});
const android = transformToCode(src, {platform: 'android'});

expect(ios).toContain('"ios"');
expect(android).toContain('"android"');
expect(android).not.toContain('"ios"');
});

test('inlines before the preset lowers ESM to CommonJS interop', () => {
// Guards the ordering contract: if the plugin ran after the RN preset's
// import transform, it would see `_reactNative.Platform.OS` and the
Expand Down
8 changes: 4 additions & 4 deletions packages/react-native-babel-transformer/src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ function isTSXSource(fileName /*: string */) {
* default RN babelrc file and uses that.
*/
const getBabelRC = (function () {
let babelRC /*: ?BabelCoreOptions */ = null;
const babelRCs /*: Map<string, BabelCoreOptions> */ = new Map();

/* $FlowFixMe[missing-local-annot] The type annotation(s) required by Flow's
* LTI update could not be added via codemod */
Expand All @@ -57,6 +57,8 @@ const getBabelRC = (function () {
extendsBabelConfigPath,
...options
}) {
const cacheKey = JSON.stringify(options);
let babelRC = babelRCs.get(cacheKey);
if (babelRC != null) {
return babelRC;
}
Expand All @@ -65,6 +67,7 @@ const getBabelRC = (function () {
plugins: [],
extends: extendsBabelConfigPath,
};
babelRCs.set(cacheKey, babelRC);

if (extendsBabelConfigPath) {
return babelRC;
Expand Down Expand Up @@ -92,14 +95,12 @@ const getBabelRC = (function () {
// If we found a babel config file, extend our config off of it
// otherwise the default config will be used
if (fs.existsSync(projectBabelRCPath)) {
// $FlowFixMe[incompatible-use] `extends` is missing in null or undefined.
babelRC.extends = projectBabelRCPath;
}
}

// If a babel config file doesn't exist in the project then
// the default preset for react-native will be used instead.
// $FlowFixMe[incompatible-use] `extends` is missing in null or undefined.
if (!babelRC.extends) {
const {
babelRuntimeModuleName,
Expand All @@ -108,7 +109,6 @@ const getBabelRC = (function () {
...presetOptions
} = options;

// $FlowFixMe[incompatible-use] `presets` is missing in null or undefined.
babelRC.presets = [
[
require('@react-native/babel-preset'),
Expand Down
Loading