@advenue/react-native 0.6.0 → 0.6.2

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,23 @@
1
+ // CommonJS ON PURPOSE — do not convert to ESM/TS. esbuild rewrites `require`
2
+ // references in ESM sources into a `__require` runtime shim that Metro's
3
+ // static dependency collection cannot see, which broke every native bridge
4
+ // access in the shipped app ("Requiring unknown module"). In a genuine CJS
5
+ // module, esbuild preserves these literal requires in the CJS build that the
6
+ // package.json "react-native" field routes Metro to. Each host module gets its
7
+ // own function so the module names stay string literals (Metro requirement).
8
+
9
+ module.exports.requireExpoModulesCore = function requireExpoModulesCore() {
10
+ try {
11
+ return require('expo-modules-core');
12
+ } catch {
13
+ return null; // not an Expo runtime (web, node tests)
14
+ }
15
+ };
16
+
17
+ module.exports.requireReactNative = function requireReactNative() {
18
+ try {
19
+ return require('react-native');
20
+ } catch {
21
+ return null; // not a React Native runtime
22
+ }
23
+ };
@@ -0,0 +1,4 @@
1
+ // Type surface for native-require.cjs (which must stay CommonJS — see the
2
+ // comment there). Callers narrow the `unknown` results themselves.
3
+ export function requireExpoModulesCore(): unknown | null;
4
+ export function requireReactNative(): unknown | null;
package/src/native.ts CHANGED
@@ -1,22 +1,21 @@
1
+ import { requireExpoModulesCore, requireReactNative } from './native-require.cjs';
1
2
  import type { AdvenueAndroidNativeModule, AdvenueIosNativeModule } from './native-types';
2
3
 
3
- declare function require(id: string): unknown;
4
-
5
4
  type ExpoModulesCore = {
6
5
  requireNativeModule<T>(name: string): T;
7
6
  };
8
7
 
9
8
  /**
10
9
  * Lazy access to the native modules that ship inside this package (ios/ and
11
- * android/, registered via expo-module.config.json). `requireNativeModule`
12
- * throws when the native side isn't built (Expo Go, web, missing pods), so
13
- * resolution happens inside try/catch — never at module scope.
14
- * expo-modules-core is resolved from the consuming app's Expo install.
10
+ * android/, registered via expo-module.config.json). Host-module resolution
11
+ * goes through native-require.cjs so Metro sees literal requires (see the
12
+ * comment there); `requireNativeModule` throws when the native side isn't
13
+ * built (Expo Go, web, missing pods), so calls stay inside try/catch.
15
14
  */
16
15
  export function getIosNative(): AdvenueIosNativeModule | null {
17
16
  try {
18
- const core = require('expo-modules-core') as ExpoModulesCore;
19
- return core.requireNativeModule<AdvenueIosNativeModule>('AdvenueIos');
17
+ const core = requireExpoModulesCore() as ExpoModulesCore | null;
18
+ return core ? core.requireNativeModule<AdvenueIosNativeModule>('AdvenueIos') : null;
20
19
  } catch {
21
20
  return null;
22
21
  }
@@ -24,8 +23,8 @@ export function getIosNative(): AdvenueIosNativeModule | null {
24
23
 
25
24
  export function getAndroidNative(): AdvenueAndroidNativeModule | null {
26
25
  try {
27
- const core = require('expo-modules-core') as ExpoModulesCore;
28
- return core.requireNativeModule<AdvenueAndroidNativeModule>('AdvenueAndroid');
26
+ const core = requireExpoModulesCore() as ExpoModulesCore | null;
27
+ return core ? core.requireNativeModule<AdvenueAndroidNativeModule>('AdvenueAndroid') : null;
29
28
  } catch {
30
29
  return null;
31
30
  }
@@ -38,7 +37,8 @@ export function getAndroidNative(): AdvenueAndroidNativeModule | null {
38
37
  */
39
38
  export function getPlatformOS(): string | null {
40
39
  try {
41
- return (require('react-native') as { Platform: { OS: string } }).Platform.OS ?? null;
40
+ const rn = requireReactNative() as { Platform: { OS: string } } | null;
41
+ return rn?.Platform.OS ?? null;
42
42
  } catch {
43
43
  return null;
44
44
  }
@@ -60,7 +60,8 @@ export interface AppStateLike {
60
60
  */
61
61
  export function getAppState(): AppStateLike | null {
62
62
  try {
63
- return (require('react-native') as { AppState: AppStateLike }).AppState;
63
+ const rn = requireReactNative() as { AppState: AppStateLike } | null;
64
+ return rn?.AppState ?? null;
64
65
  } catch {
65
66
  return null;
66
67
  }
@@ -88,7 +89,8 @@ export interface LinkingLike {
88
89
  */
89
90
  export function getLinking(): LinkingLike | null {
90
91
  try {
91
- return (require('react-native') as { Linking: LinkingLike }).Linking;
92
+ const rn = requireReactNative() as { Linking: LinkingLike } | null;
93
+ return rn?.Linking ?? null;
92
94
  } catch {
93
95
  return null;
94
96
  }