/** * XuqmBundleModule JS 封装。 * * 提供 bundle 文件的原生管理能力: * - 读取/写入/删除 bundle 文件 * - manifest 管理 * - bundle 路径查询 * * 注意:bundle 注入到运行中的 RN host(loadBundle) * 由宿主的 BundleRuntime 负责(RN 0.84 不暴露公开 API)。 */ import {NativeModules, Platform} from 'react-native' interface XuqmBundleModuleInterface { bundleExists(moduleId: string): Promise readBundle(moduleId: string): Promise writeBundle(moduleId: string, source: string, md5: string): Promise deleteBundle(moduleId: string): Promise getBundlePath(moduleId: string): Promise getManifest(): Promise writeManifest(manifestJson: string): Promise } function getModule(): XuqmBundleModuleInterface { const mod = NativeModules.XuqmBundleModule if (!mod) { throw new Error('[XuqmBundleModule] Native module not available') } return mod as XuqmBundleModuleInterface } export const NativeBundle = { /** * 检查指定 bundle 文件是否存在。 */ async exists(moduleId: string): Promise { return getModule().bundleExists(moduleId) }, /** * 读取 bundle 文件内容(JS 源码文本)。 */ async read(moduleId: string): Promise { return getModule().readBundle(moduleId) }, /** * 将 bundle 源码写入本地文件。 * * @param moduleId 插件 ID * @param source JS 源码文本 * @param md5 文件 MD5 校验值 */ async write(moduleId: string, source: string, md5: string): Promise { await getModule().writeBundle(moduleId, source, md5) }, /** * 删除指定 bundle 文件。 */ async remove(moduleId: string): Promise { await getModule().deleteBundle(moduleId) }, /** * 获取 bundle 文件的绝对路径。 */ async getPath(moduleId: string): Promise { return getModule().getBundlePath(moduleId) }, /** * 读取 manifest.json 内容。 */ async getManifest(): Promise { return getModule().getManifest() }, /** * 写入 manifest.json。 */ async writeManifest(manifestJson: string): Promise { await getModule().writeManifest(manifestJson) }, /** * 获取 bundle 文件名(平台相关)。 */ getFileName(moduleId: string): string { return `${moduleId}.${Platform.OS}.bundle` }, }