-
Notifications
You must be signed in to change notification settings - Fork 4.8k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix(babel): fix unused platform code removal (#25171)
# Why Metro applies this plugin too but it does it after the imports have been transformed which breaks the plugin. Here, we'll apply it before the commonjs transform, in production, to ensure `Platform.OS` is replaced with a string literal and `__DEV__` is converted to a boolean. Applying early also means that web can be transformed before the `react-native-web` transform mutates the import. # Test Plan - Added new tests for the plugin. # Checklist <!-- Please check the appropriate items below if they apply to your diff. This is required for changes to Expo modules. --> - [ ] Documentation is up to date to reflect these changes (eg: https://docs.expo.dev and README.md). - [ ] Conforms with the [Documentation Writing Style Guide](https://github.com/expo/expo/blob/main/guides/Expo%20Documentation%20Writing%20Style%20Guide.md) - [ ] This diff will work correctly for `npx expo prebuild` & EAS Build (eg: updated a module plugin). --------- Co-authored-by: Expo Bot <34669131+expo-bot@users.noreply.github.com>
- Loading branch information
Showing
6 changed files
with
135 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
103 changes: 103 additions & 0 deletions
103
packages/babel-preset-expo/src/__tests__/platform-shaking.test.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,103 @@ | ||
import * as babel from '@babel/core'; | ||
|
||
import preset from '..'; | ||
|
||
function getCaller(props: Record<string, string>): babel.TransformCaller { | ||
return props as unknown as babel.TransformCaller; | ||
} | ||
|
||
const DEFAULT_OPTS = { | ||
babelrc: false, | ||
presets: [[preset]], | ||
plugins: [ | ||
// Fold constants to emulate Metro | ||
require('metro-transform-plugins/src/constant-folding-plugin.js'), | ||
], | ||
sourceMaps: true, | ||
filename: 'unknown', | ||
configFile: false, | ||
compact: true, | ||
comments: false, | ||
retainLines: false, | ||
}; | ||
|
||
function stripReactNativeImport(code: string) { | ||
return code | ||
.replace( | ||
'var _interopRequireDefault=require("@babel/runtime/helpers/interopRequireDefault");var _Platform=_interopRequireDefault(require("react-native-web/dist/exports/Platform"));', | ||
'' | ||
) | ||
.replace('var _reactNative=require("react-native");', ''); | ||
} | ||
|
||
it(`removes Platform module usage on web`, () => { | ||
const options = { | ||
...DEFAULT_OPTS, | ||
caller: getCaller({ name: 'metro', engine: 'hermes', platform: 'web' }), | ||
}; | ||
|
||
const sourceCode = ` | ||
import { Platform } from 'react-native'; | ||
if (Platform.OS === 'ios') { | ||
console.log('ios') | ||
} | ||
Platform.select({ | ||
ios: () => console.log('ios'), | ||
web: () => console.log('web'), | ||
android: () => console.log('android'), | ||
}) | ||
`; | ||
|
||
expect(stripReactNativeImport(babel.transform(sourceCode, options)!.code!)).toEqual( | ||
`(function(){return console.log('web');});` | ||
); | ||
}); | ||
|
||
it(`removes Platform module usage on native`, () => { | ||
const options = { | ||
...DEFAULT_OPTS, | ||
caller: getCaller({ name: 'metro', engine: 'hermes', platform: 'android' }), | ||
}; | ||
|
||
expect( | ||
babel.transform( | ||
`Platform.select({ ios: () => console.log('ios'), web: () => console.log('web'), android: () => console.log('android'), })`, | ||
options | ||
)!.code | ||
).toEqual(`(function(){return console.log('android');});`); | ||
|
||
const sourceCode = ` | ||
import { Platform } from 'react-native'; | ||
if (Platform.OS === 'ios') { | ||
console.log('ios') | ||
} | ||
Platform.select({ | ||
ios: () => console.log('ios'), | ||
web: () => console.log('web'), | ||
android: () => console.log('android'), | ||
}) | ||
`; | ||
|
||
expect(stripReactNativeImport(babel.transform(sourceCode, options)!.code!)).toEqual( | ||
`(function(){return console.log('android');});` | ||
); | ||
}); | ||
|
||
it(`removes __DEV__ usage`, () => { | ||
const options = { | ||
...DEFAULT_OPTS, | ||
caller: getCaller({ name: 'metro', engine: 'hermes', platform: 'android' }), | ||
}; | ||
|
||
const sourceCode = ` | ||
if (__DEV__) { | ||
require('./foobar') | ||
} | ||
`; | ||
|
||
expect(babel.transform(sourceCode, options)!.code!).toEqual(``); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters