Skip to content

Commit

Permalink
Ensure global objects initialization for web (software-mansion#3418)
Browse files Browse the repository at this point in the history
## 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
piaskowyk authored and fluiddot committed Jun 5, 2023
1 parent 2aed9a4 commit cfb5591
Show file tree
Hide file tree
Showing 2 changed files with 55 additions and 35 deletions.
10 changes: 9 additions & 1 deletion src/index.ts
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';
80 changes: 46 additions & 34 deletions src/reanimated2/js-reanimated/global.ts
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();

0 comments on commit cfb5591

Please sign in to comment.