Skip to content

Commit

Permalink
chore: Add additional tests for undefined platform minification behav…
Browse files Browse the repository at this point in the history
…ior. (#27515)

# Why

- Metro's platform minification plugin has undefined behavior where the
Platform module doesn't need to come from the `react-native` import,
this inadvertently resolves the need to add support for
`expo-modules-core`.
- I'll add a follow up PR to fork the Metro plugin and remove the many
additional passes used to support haste. Will also fix the extra case I
found where `.native` is left as-is even when bundling for web.

# Test Plan

- Added tests only

---------

Co-authored-by: Expo Bot <34669131+expo-bot@users.noreply.github.com>
  • Loading branch information
EvanBacon and expo-bot committed Mar 8, 2024
1 parent 5bd5d16 commit 4d9c14e
Show file tree
Hide file tree
Showing 2 changed files with 57 additions and 0 deletions.
1 change: 1 addition & 0 deletions packages/babel-preset-expo/CHANGELOG.md
Expand Up @@ -13,6 +13,7 @@
### 💡 Others

- Disable color in snapshot tests in CI. ([#27301](https://github.com/expo/expo/pull/27301) by [@EvanBacon](https://github.com/EvanBacon))
- Add additional tests for undefined platform minification behavior. ([#27515](https://github.com/expo/expo/pull/27515) by [@EvanBacon](https://github.com/EvanBacon))
- Upgrade `babel-plugin-react-native-web` for latest `react-native-web` aliases. ([#27214](https://github.com/expo/expo/pull/27214) by [@EvanBacon](https://github.com/EvanBacon))
- Directly resolve plugins. ([#27041](https://github.com/expo/expo/pull/27041) by [@EvanBacon](https://github.com/EvanBacon))

Expand Down
56 changes: 56 additions & 0 deletions packages/babel-preset-expo/src/__tests__/platform-shaking.test.ts
Expand Up @@ -30,6 +30,62 @@ function stripReactNativeImport(code: string) {
.replace('var _reactNative=require("react-native");', '');
}

it(`removes Platform module without import (undefined behavior)`, () => {
const options = {
...DEFAULT_OPTS,
caller: getCaller({ name: 'metro', engine: 'hermes', platform: 'web', isDev: false }),
};

const sourceCode = `
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(`supports Platform module default fallback on web`, () => {
const options = {
...DEFAULT_OPTS,
caller: getCaller({ name: 'metro', engine: 'hermes', platform: 'web', isDev: false }),
};

const sourceCode = `
Platform.select({
ios: () => console.log('ios'),
default: () => console.log('default'),
})`;

expect(stripReactNativeImport(babel.transform(sourceCode, options)!.code!)).toEqual(
`(function(){return console.log('default');});`
);
});

xit(`removes Platform module and native fallback on web`, () => {
const options = {
...DEFAULT_OPTS,
caller: getCaller({ name: 'metro', engine: 'hermes', platform: 'web', isDev: false }),
};

const sourceCode = `
Platform.select({
native: () => console.log('native'),
default: () => console.log('default'),
})`;

expect(stripReactNativeImport(babel.transform(sourceCode, options)!.code!)).toEqual(
`(function(){return console.log('web');});`
);
});

it(`removes Platform module usage on web`, () => {
const options = {
...DEFAULT_OPTS,
Expand Down

0 comments on commit 4d9c14e

Please sign in to comment.