Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
🌲 Reduce bundle size on Web by 80% (#3278)
## Description <!-- Description and motivation for this PR. Inlude Fixes #<number> if this is fixing some issue. Fixes # . --> Fixes #2843. Reduces Reanimated's bundle size on Web from `85kb` to `17kb`. ## Changes Zero user-facing changes are in this PR. It is strictly file organization changes to optimize Reanimated for tree shaking. After extensive testing, this PR reduced Reanimated's bundle size on Web by almost 80%. In `package.json`, you'll notice that there is a new `sideEffects` array. This is a way to tell Webpack which files need to run global code rather than as standalone modules. I looked through every file that runs on Web which edits `global` at the top-level and found 2 files. Ideally, we could move all side effects into a single file rather than colocating them with other code that may go unused. But for now, this works. (Update: I moved some into their own file) More on Webpack tree shaking and the `sideEffects` field can be found here: https://webpack.js.org/guides/tree-shaking/ ## Screenshots / GIFs I created a fresh [repo](https://github.com/nandorojo/reanimated-tree-shaking) to fix tree shaking for Reanimated. The playground tests it by building in a Next.js app (the most common framework for Web). Prior to this PR, this imported _all_ code from Reanimated: ```ts import Animated from 'react-native-reanimated' ``` This resulted in an absolutely massive impact on the Web bundle size, approximately 85kb, including tons of unused code (such as layout animations). This PR reduces the Reanimated overhead down to about `17kb`. In the future, I may be able to investigate further improvements by seeing where that size is actually coming from. But this PR is a massive step forward, since it provides no changes to the user. I wrote about my findings [here](nandorojo/reanimated-tree-shaking#1). There are plenty of detailed screenshots there. ### Before Massive bundle size on Web, nearly unusable. ### After It's now very optimized for tree shaking. ## Checklist - [x] Included code example that can be used to test this change - [x] Updated TS types - [ ] Added TS types tests - [ ] Added unit / integration tests - [ ] Updated documentation - [x] Ensured that CI passes
- Loading branch information
Showing
26 changed files
with
161 additions
and
96 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
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
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
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
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
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
This file was deleted.
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
export { default as createAnimatedComponent } from './createAnimatedComponent'; | ||
export { | ||
addWhitelistedNativeProps, | ||
addWhitelistedUIProps, | ||
} from './ConfigHelper'; | ||
|
||
export { default as Text } from './reanimated2/component/Text'; | ||
export { default as View } from './reanimated2/component/View'; | ||
export { default as ScrollView } from './reanimated2/component/ScrollView'; | ||
export { default as Image } from './reanimated2/component/Image'; | ||
export { default as FlatList } from './reanimated2/component/FlatList'; |
This file was deleted.
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
// tree-shaken side effects | ||
import './reanimated2/js-reanimated/global'; | ||
|
||
export * from './reanimated2'; | ||
export * as default from './Animated'; |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
import { Image } from 'react-native'; | ||
import createAnimatedComponent from '../../createAnimatedComponent'; | ||
|
||
const AnimatedImage = createAnimatedComponent(Image as any); | ||
|
||
export default AnimatedImage; |
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,6 @@ | ||
import { ScrollView } from 'react-native'; | ||
import createAnimatedComponent from '../../createAnimatedComponent'; | ||
|
||
const AnimatedScrollView = createAnimatedComponent(ScrollView); | ||
|
||
export default AnimatedScrollView; |
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,6 @@ | ||
import { Text } from 'react-native'; | ||
import createAnimatedComponent from '../../createAnimatedComponent'; | ||
|
||
const AnimatedText = createAnimatedComponent(Text as any); | ||
|
||
export default AnimatedText; |
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,6 @@ | ||
import { View } from 'react-native'; | ||
import createAnimatedComponent from '../../createAnimatedComponent'; | ||
|
||
const AnimatedView = createAnimatedComponent(View); | ||
|
||
export default AnimatedView; |
This file was deleted.
Oops, something went wrong.
This file was deleted.
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
// In order to keep bundle size down, we treat this file as a polyfill for Web. | ||
|
||
import { shouldBeUseWeb } from '../PlatformChecker'; | ||
|
||
if (shouldBeUseWeb()) { | ||
global._frameTimestamp = null; | ||
global._setGlobalConsole = (_val) => { | ||
// noop | ||
}; | ||
global._measure = () => { | ||
console.warn( | ||
"[Reanimated] You can't use `measure` with Chrome Debugger or with web version" | ||
); | ||
return { | ||
x: 0, | ||
y: 0, | ||
width: 0, | ||
height: 0, | ||
pageX: 0, | ||
pageY: 0, | ||
}; | ||
}; | ||
global._scrollTo = () => { | ||
console.warn( | ||
"[Reanimated] You can't use `scrollTo` with Chrome Debugger or with web version" | ||
); | ||
}; | ||
global._dispatchCommand = () => { | ||
console.warn( | ||
"[Reanimated] You can't use `scrollTo` or `dispatchCommand` methods with Chrome Debugger or with web version" | ||
); | ||
}; | ||
global._setGestureState = () => { | ||
console.warn( | ||
"[Reanimated] You can't use `setGestureState` with Chrome Debugger or with web version" | ||
); | ||
}; | ||
} |
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.