diff --git a/packages/metro-transform-plugins/src/__tests__/inline-plugin-test.js b/packages/metro-transform-plugins/src/__tests__/inline-plugin-test.js index fe2fb8d154..816692c5a2 100644 --- a/packages/metro-transform-plugins/src/__tests__/inline-plugin-test.js +++ b/packages/metro-transform-plugins/src/__tests__/inline-plugin-test.js @@ -410,6 +410,20 @@ describe('inline constants', () => { }); }); + test('does not discard impure Platform.select initializers', () => { + const code = ` + var value = Platform.select({ + ios: selected(), + android: discarded(), + }); + `; + + compare([inlinePlugin], code, code, { + inlinePlatform: true, + platform: 'ios', + }); + }); + test('inlines Platform.select in the code when using an ObjectMethod', () => { const code = ` function a() { diff --git a/packages/metro-transform-plugins/src/inline-plugin.js b/packages/metro-transform-plugins/src/inline-plugin.js index 4fc5dfb49d..73b163422f 100644 --- a/packages/metro-transform-plugins/src/inline-plugin.js +++ b/packages/metro-transform-plugins/src/inline-plugin.js @@ -200,8 +200,17 @@ export default function inlinePlugin( findProperty(arg, 'native', () => findProperty(arg, 'default', () => t.identifier('undefined')), ); - - path.replaceWith(findProperty(arg, opts.platform, fallback)); + const selected = findProperty(arg, opts.platform, fallback); + + if ( + arg.properties.every( + property => + (isObjectProperty(property) && property.value === selected) || + scope.isPure(property), + ) + ) { + path.replaceWith(selected); + } } } },