Skip to content

Commit

Permalink
Move REACT_NATIVE_VERSION to native-only code (#2666)
Browse files Browse the repository at this point in the history
## Description

Fixes #2660

Depending on this package in a web-only app is causing a compile error.
This is due to this package importing `react-native/package.json`, which
for web-only builds doesn't exist. This import is only done to obtain
the react-native version number, which isn't used for web anyway. So it
seems easy enough to move this version number to a native-only file.

I chose to export a **function** so that it's a _logic error_ to use
`getReactNativeVersion` on web, and an error is thrown (right now it's
only used in one place, behind a check that makes sure it runs on native
only). It might have been easier to just export a **const**, with fake
values on web, but that would hide the error for a potentially more
confusing error down the line.

## Test plan

Not tested, sorry. Would appreciate if someone could help out here.

---------

Co-authored-by: Chris Coomber <[email protected]>
  • Loading branch information
chriscoomber and Chris Coomber authored Nov 6, 2023
1 parent b0986c3 commit ff3b820
Show file tree
Hide file tree
Showing 4 changed files with 17 additions and 14 deletions.
11 changes: 11 additions & 0 deletions src/getReactNativeVersion.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
import pack from 'react-native/package.json';

const [majorStr, minorStr] = pack.version.split('.');
const REACT_NATIVE_VERSION = {
major: parseInt(majorStr, 10),
minor: parseInt(minorStr, 10),
};

export function getReactNativeVersion() {
return REACT_NATIVE_VERSION;
}
3 changes: 3 additions & 0 deletions src/getReactNativeVersion.web.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
export function getReactNativeVersion() {
throw new Error('getReactNativeVersion is not supported on web');
}
9 changes: 3 additions & 6 deletions src/handlers/gestures/GestureDetector.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -37,12 +37,8 @@ import { State } from '../../State';
import { TouchEventType } from '../../TouchEventType';
import { ComposedGesture } from './gestureComposition';
import { ActionType } from '../../ActionType';
import {
isFabric,
isJestEnv,
REACT_NATIVE_VERSION,
tagMessage,
} from '../../utils';
import { isFabric, isJestEnv, tagMessage } from '../../utils';
import { getReactNativeVersion } from '../../getReactNativeVersion';
import { getShadowNodeFromRef } from '../../getShadowNodeFromRef';
import { Platform } from 'react-native';
import type RNGestureHandlerModuleWeb from '../../RNGestureHandlerModule.web';
Expand Down Expand Up @@ -565,6 +561,7 @@ function validateDetectorChildren(ref: any) {
// / \
// NativeView NativeView
if (__DEV__ && Platform.OS !== 'web') {
const REACT_NATIVE_VERSION = getReactNativeVersion();
// eslint-disable-next-line @typescript-eslint/no-unsafe-assignment
const wrapType =
REACT_NATIVE_VERSION.minor > 63 || REACT_NATIVE_VERSION.major > 0
Expand Down
8 changes: 0 additions & 8 deletions src/utils.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,3 @@
import pack from 'react-native/package.json';

const [majorStr, minorStr] = pack.version.split('.');
export const REACT_NATIVE_VERSION = {
major: parseInt(majorStr, 10),
minor: parseInt(minorStr, 10),
};

export function toArray<T>(object: T | T[]): T[] {
if (!Array.isArray(object)) {
return [object];
Expand Down

0 comments on commit ff3b820

Please sign in to comment.