Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: metro commonjs issues with optional libs #1625

Open
wants to merge 3 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions packages/react-native-sdk/.gitignore
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

unrelated: added for easy local testing with yarn pack

Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,7 @@ buck-out/

# Bundle artifact
*.jsbundle
*.tgz

# Ruby / CocoaPods
/ios/Pods/
Expand Down
6 changes: 4 additions & 2 deletions packages/react-native-sdk/src/utils/push/ios.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,13 @@ import { pushNonRingingCallData$ } from './internal/rxSubjects';
import {
ExpoNotification,
getExpoNotificationsLib,
getNotifeeLibThrowIfNotInstalledForPush,
getPushNotificationIosLib,
PushNotificationiOSType,
} from './libs';
import { StreamVideoClient, getLogger } from '@stream-io/video-client';
import { setPushLogoutCallback } from '../internal/pushLogoutCallback';
import { EventType, Event } from '@notifee/react-native';
import type { Event } from '@notifee/react-native';
import { StreamVideoRN } from '../StreamVideoRN';
import { StreamPushPayload } from './utils';

Expand Down Expand Up @@ -51,7 +52,8 @@ export const oniOSNotifeeEvent = ({
if (Platform.OS !== 'ios') return;
const pushConfig = StreamVideoRN.getConfig().push;
const { type, detail } = event;
if (pushConfig && type === EventType.PRESS) {
const notifeeLib = getNotifeeLibThrowIfNotInstalledForPush();
if (pushConfig && type === notifeeLib.EventType.PRESS) {
const streamPayload = detail.notification?.data?.stream as
| StreamPushPayload
| undefined;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,29 +1,24 @@
import { getLogger } from '@stream-io/video-client';
import { Type, lib } from './lib';

export type { FirebaseMessagingTypes } from '@react-native-firebase/messaging';
export type FirebaseMessagingType =
typeof import('@react-native-firebase/messaging').default;

let messaging: FirebaseMessagingType | undefined;

try {
messaging = require('@react-native-firebase/messaging').default;
} catch (_e) {}
export type FirebaseMessagingType = Type;

const INSTALLATION_INSTRUCTION =
'Please see https://rnfirebase.io/messaging/usage#installation for installation instructions';

export function getFirebaseMessagingLib() {
if (!messaging) {
if (!lib) {
throw Error(
'@react-native-firebase/messaging is not installed. ' +
INSTALLATION_INSTRUCTION
);
}
return messaging;
return lib;
}

export function getFirebaseMessagingLibNoThrow(isExpo: boolean) {
if (!messaging) {
if (!lib) {
const logger = getLogger(['getFirebaseMessagingLibNoThrow']);
logger(
'warn',
Expand All @@ -34,5 +29,5 @@ export function getFirebaseMessagingLibNoThrow(isExpo: boolean) {
}${INSTALLATION_INSTRUCTION}`
);
}
return messaging;
return lib;
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
export type Type = typeof import('@react-native-firebase/messaging').default;

let lib: Type | undefined;

try {
lib = require('@react-native-firebase/messaging').default;
} catch (_e) {}

export { lib };

/*
IMPORTANT: must keep a failing import in a different file
Else on commonjs, metro doesnt resolve any other modules properly in a file, if one of the module is not installed
*/
4 changes: 2 additions & 2 deletions packages/react-native-sdk/src/utils/push/libs/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,6 @@ export * from './callkeep';
export * from './notifee';

/*
NOTE: must keep each libs in different files
Else on commonjs, metro doesnt resolve modules properly if one of the module is not installed
NOTE: must keep each libs in different files
Else on commonjs, metro doesnt resolve modules properly if one of the module is not installed
*/
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
import { getLogger } from '@stream-io/video-client';
import { PermissionsAndroid } from 'react-native';
import { Type, lib } from './lib';

export type NotifeeLib = typeof import('@notifee/react-native');
export type NotifeeLib = Type;

enum AndroidForegroundServiceType {
FOREGROUND_SERVICE_TYPE_CAMERA = 64,
Expand All @@ -21,34 +22,28 @@ enum AndroidForegroundServiceType {
FOREGROUND_SERVICE_TYPE_MANIFEST = -1,
}

let notifeeLib: NotifeeLib | undefined;

try {
notifeeLib = require('@notifee/react-native');
} catch (_e) {}

const INSTALLATION_INSTRUCTION =
'Please see https://notifee.app/react-native/docs/installation for installation instructions';

export function getNotifeeLibThrowIfNotInstalledForPush() {
if (!notifeeLib) {
if (!lib) {
throw Error(
'@notifee/react-native is not installed. It is required for implementing push notifications. ' +
INSTALLATION_INSTRUCTION
);
}
return notifeeLib;
return lib;
}

export function getNotifeeLibNoThrowForKeepCallAlive() {
if (!notifeeLib) {
if (!lib) {
const logger = getLogger(['getNotifeeLibNoThrow']);
logger(
'info',
`${'@notifee/react-native library not installed. It is required to keep call alive in the background for Android. '}${INSTALLATION_INSTRUCTION}`
);
}
return notifeeLib;
return lib;
}

export async function getKeepCallAliveForegroundServiceTypes() {
Expand Down
14 changes: 14 additions & 0 deletions packages/react-native-sdk/src/utils/push/libs/notifee/lib.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
export type Type = typeof import('@notifee/react-native');

let lib: Type | undefined;

try {
lib = require('@notifee/react-native');
} catch (_e) {}

export { lib };

/*
IMPORTANT: must keep a failing import in a different file
Else on commonjs, metro doesnt resolve any other modules properly in a file, if one of the module is not installed
*/
2 changes: 1 addition & 1 deletion packages/react-native-sdk/src/utils/push/utils.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { Event } from '@notifee/react-native';
import type { Event } from '@notifee/react-native';
import { FirebaseMessagingTypes } from './libs/firebaseMessaging';
import { ExpoNotification } from './libs/expoNotifications';
import { NonRingingPushEvent } from '../StreamVideoRN/types';
Expand Down
Loading