forked from software-mansion/react-native-reanimated
-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Ensure global objects initialization for web (software-mansion#3418)
## Description The PR with tree shaking moved global objects initialization for web to another file and then imported it into the `index` file (`import './reanimated2/js-reanimated/global';`). However, the Webpack can lazy import modules, but we don't have any call to this module, so global objects were never initialized. Fixes software-mansion#3355
- Loading branch information
Showing
2 changed files
with
55 additions
and
35 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,5 +1,13 @@ | ||
// tree-shaken side effects | ||
import './reanimated2/js-reanimated/global'; | ||
import webGlobalIsInitialized from './reanimated2/js-reanimated/global'; | ||
if (!webGlobalIsInitialized) { | ||
/* | ||
`webGlobalIsInitialized` should always be `true`, | ||
but we need to use `webGlobalIsInitialized` somewhere to ensure function execution, | ||
in another way, the bundler can remove unused variables. | ||
*/ | ||
console.error('[Reanimated] Unable to initialize global objects for web.'); | ||
} | ||
|
||
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,38 +1,50 @@ | ||
// 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, | ||
const initializeGlobalsForWeb = () => { | ||
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._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" | ||
); | ||
}; | ||
} | ||
global._setGestureState = () => { | ||
console.warn( | ||
"[Reanimated] You can't use `setGestureState` with Chrome Debugger or with web version" | ||
); | ||
}; | ||
} | ||
return true; | ||
}; | ||
|
||
/* | ||
If a file doesn't export anything, tree shaking doesn't pack | ||
it into the JS bundle. In effect, the code inside of this file | ||
will never execute. That is why we wrapped initialization code | ||
into a function, and we call this one during creating | ||
the module export object. | ||
*/ | ||
|
||
export default initializeGlobalsForWeb(); |