diff --git a/package.json b/package.json index cabd8cc..b524a5c 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "react-native-update", - "version": "10.5.1", + "version": "10.5.2", "description": "react-native hot update", "main": "src/index", "scripts": { diff --git a/src/client.js b/src/client.js deleted file mode 100644 index 1f123e3..0000000 --- a/src/client.js +++ /dev/null @@ -1,13 +0,0 @@ -const noop = () => {}; -export class Pushy { - constructor() { - console.warn( - 'react-native-update is not supported and will do nothing on web.', - ); - return new Proxy(this, { - get() { - return noop; - }, - }); - } -} diff --git a/src/client.native.ts b/src/client.ts similarity index 93% rename from src/client.native.ts rename to src/client.ts index cb494eb..c12b182 100644 --- a/src/client.native.ts +++ b/src/client.ts @@ -1,10 +1,7 @@ import { CheckResult, PushyOptions, ProgressData, EventType } from './type'; import { log, testUrls } from './utils'; -import { - EmitterSubscription, - PermissionsAndroid, - Platform, -} from 'react-native'; +import { EmitterSubscription, Platform } from 'react-native'; +import type { PermissionsAndroidStatic } from 'react-native'; import { PushyModule, buildTime, @@ -27,6 +24,10 @@ const defaultServer = { const empty = {}; const noop = () => {}; +if (Platform.OS === 'web') { + console.warn('react-native-update 不支持 web 端热更,不会执行操作'); +} + export class Pushy { options: PushyOptions = { appKey: '', @@ -149,10 +150,14 @@ export class Pushy { checkUpdate = async () => { if (__DEV__ && !this.options.debug) { console.info( - '您当前处于开发环境且未启用debug,不会进行热更检查。如需在开发环境中调试热更,请在client中设置debug为true', + '您当前处于开发环境且未启用 debug,不会进行热更检查。如需在开发环境中调试热更,请在 client 中设置 debug 为 true', ); return; } + if (Platform.OS === 'web') { + console.warn('web 端不支持热更新检查'); + return; + } const now = Date.now(); if ( this.lastRespJson && @@ -366,6 +371,8 @@ export class Pushy { this.report({ type: 'downloadingApk' }); if (Platform.Version <= 23) { try { + const PermissionsAndroid = + require('react-native/Libraries/PermissionsAndroid/PermissionsAndroid') as PermissionsAndroidStatic; const granted = await PermissionsAndroid.request( PermissionsAndroid.PERMISSIONS.WRITE_EXTERNAL_STORAGE, ); diff --git a/src/core.ts b/src/core.ts index 8b63a4e..5f903bc 100644 --- a/src/core.ts +++ b/src/core.ts @@ -8,9 +8,23 @@ const isTurboModuleEnabled = // @ts-expect-error global.__turboModuleProxy != null; -export const PushyModule = isTurboModuleEnabled - ? require('./NativePushy').default - : NativeModules.Pushy; +const noop = () => {}; +class EmptyModule { + constructor() { + return new Proxy(this, { + get() { + return noop; + }, + }); + } +} + +export const PushyModule = + Platform.OS === 'web' + ? new EmptyModule() + : isTurboModuleEnabled + ? require('./NativePushy').default + : NativeModules.Pushy; if (!PushyModule) { throw new Error('react-native-update模块无法加载,请对照安装文档检查配置。'); diff --git a/src/index.native.ts b/src/index.native.ts deleted file mode 100644 index 555b50b..0000000 --- a/src/index.native.ts +++ /dev/null @@ -1,3 +0,0 @@ -export { Pushy } from './client.native'; -export { PushyContext, usePushy } from './context'; -export { PushyProvider } from './provider.native'; diff --git a/src/index.js b/src/index.ts similarity index 100% rename from src/index.js rename to src/index.ts diff --git a/src/provider.jsx b/src/provider.jsx deleted file mode 100644 index 6169604..0000000 --- a/src/provider.jsx +++ /dev/null @@ -1,2 +0,0 @@ -import React from 'react'; -export const PushyProvider = ({ children }) => <>{children}; diff --git a/src/provider.native.tsx b/src/provider.tsx similarity index 99% rename from src/provider.native.tsx rename to src/provider.tsx index db11d43..518d997 100644 --- a/src/provider.native.tsx +++ b/src/provider.tsx @@ -12,7 +12,7 @@ import { Platform, Linking, } from 'react-native'; -import { Pushy } from './client.native'; +import { Pushy } from './client'; import { currentVersion, isFirstTime, diff --git a/src/utils.ts b/src/utils.ts index 3d1b23f..9cc5670 100644 --- a/src/utils.ts +++ b/src/utils.ts @@ -1,14 +1,18 @@ +import { Platform } from 'react-native'; export function log(...args: any[]) { console.log('pushy: ', ...args); } -const ping = async (url: string) => - Promise.race([ - fetch(url, { - method: 'HEAD', - }).then(({ status }) => status === 200), - new Promise(r => setTimeout(() => r(false), 2000)), - ]); +const ping = + Platform.OS === 'web' + ? () => Promise.resolve(true) + : async (url: string) => + Promise.race([ + fetch(url, { + method: 'HEAD', + }).then(({ status }) => status === 200), + new Promise(r => setTimeout(() => r(false), 2000)), + ]); const canUseGoogle = ping('https://www.google.com');