-
Notifications
You must be signed in to change notification settings - Fork 4.8k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
fix(babel): fix unused platform code removal #25171
Merged
Merged
Changes from all commits
Commits
Show all changes
8 commits
Select commit
Hold shift + click to select a range
8359891
Fix platform shaking by default
EvanBacon f17e9d3
Merge branch 'main' into @evanbacon/babel/fix-platform-shaking
EvanBacon 9a2bfdc
Update index.js
EvanBacon 056ad06
Update packages/babel-preset-expo/CHANGELOG.md
EvanBacon 91ac0d6
Merge branch 'main' into @evanbacon/babel/fix-platform-shaking
EvanBacon 90d45ec
Merge branch 'main' into @evanbacon/babel/fix-platform-shaking
EvanBacon 622fa6e
update tests
EvanBacon dbd133c
Update inline-env-vars.test.ts
EvanBacon File filter
Filter by extension
Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
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
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Why should this not run in development, too?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
We don't do any minification in development to keep things fast, might change in the future but right now it doesn't provide much benefit.