新增 withXuqmConfig(metroConfig) Metro 插件: - 扫描 src/assets/config/ 下的 *.xuqmconfig 文件 - 通过 resolveRequest 拦截 @xuqm/autoinit-config 模块 - 宿主只需放入下载的 .xuqmconfig 文件,无需重命名 对齐 Android SDK 的 ConfigFileReader 逻辑。 Co-Authored-By: Claude <noreply@anthropic.com>
51 行
1.6 KiB
TypeScript
51 行
1.6 KiB
TypeScript
/**
|
||
* SDK 自动初始化模块。
|
||
*
|
||
* 对齐 Android SDK 的 ContentProvider 模式。
|
||
*
|
||
* 推荐方式(withXuqmConfig Metro 插件):
|
||
* 1. 宿主将 .xuqmconfig 文件放入 src/assets/config/
|
||
* 2. metro.config.js 使用 withXuqmConfig(metroConfig) 包装
|
||
* 3. 本模块 tryRequire('@xuqm/autoinit-config') 自动读取并初始化
|
||
*
|
||
* 兼容方式(手动 alias):
|
||
* 1. 宿主把加密配置放到 src/assets/xuqm/config.xuqmconfig.ts
|
||
* 2. 宿主 babel/metro config 添加 alias: '@xuqm/autoinit-config' → 该文件
|
||
*
|
||
* 如果配置未找到(require 失败),静默跳过,不崩溃。
|
||
*/
|
||
|
||
import {isInitialized} from './config'
|
||
import {XuqmSDK} from './sdk'
|
||
|
||
let _autoInitAttempted = false
|
||
|
||
function tryRequireConfig(): string | null {
|
||
try {
|
||
// Metro moduleNameMapper 将此路径映射到宿主的配置文件
|
||
// eslint-disable-next-line @typescript-eslint/no-require-imports
|
||
const mod = require('@xuqm/autoinit-config') as {ENCRYPTED_CONFIG?: string; default?: string}
|
||
return mod?.ENCRYPTED_CONFIG ?? mod?.default ?? null
|
||
} catch {
|
||
return null
|
||
}
|
||
}
|
||
|
||
/**
|
||
* 尝试自动初始化。幂等,只执行一次。
|
||
* 失败时静默跳过(对齐 Android SDK 的 runCatching + Log.w 行为)。
|
||
*/
|
||
export function tryAutoInit(): void {
|
||
if (_autoInitAttempted) return
|
||
_autoInitAttempted = true
|
||
if (isInitialized()) return
|
||
|
||
const encrypted = tryRequireConfig()
|
||
if (!encrypted) return
|
||
|
||
XuqmSDK.initWithConfigFile(encrypted, {debug: __DEV__}).catch((e: unknown) => {
|
||
console.error('[XuqmSDK] Auto-init failed:', e)
|
||
if (__DEV__) throw e
|
||
})
|
||
}
|