Skip to content

Commit

Permalink
Ensure global objects initialization for web (#3418)
Browse files Browse the repository at this point in the history
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 #3355
  • Loading branch information
piaskowyk committed Aug 17, 2022
1 parent 1f94d36 commit 31b306e
Show file tree
Hide file tree
Showing 2 changed files with 50 additions and 30 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.');
}

// @ts-ignore backward compatibility with treeshaking
export * from './reanimated1';
Expand Down
70 changes: 41 additions & 29 deletions src/reanimated2/js-reanimated/global.ts
Original file line number Diff line number Diff line change
@@ -1,33 +1,45 @@
// 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._scrollTo = () => {
console.warn(
"[Reanimated] You can't use `scrollTo` 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 31b306e

Please sign in to comment.