import type { ComponentType } from "react"; import type { PluginBridgeHostContext, PluginPageContract } from "../contracts/pluginBridge"; export interface PluginPageBundleModule { pluginPageBundle: { key: string; version: string; integritySha256: string }; renderPluginPage: (react: { createElement: typeof import("react").createElement }, input: { page: PluginPageContract; context: PluginBridgeHostContext; workspace?: unknown; availability: { available: boolean; reason?: string } }) => ReturnType; } const pageBundles = import.meta.glob("../../plugins/examples/*/page-bundle/index.ts"); export async function loadPluginPageBundle(page: PluginPageContract): Promise> { if (!page.bundleKey || !page.bundleVersion || !page.bundleIntegritySha256) throw new Error("插件没有声明受验证的页面 bundle。"); const match = Object.entries(pageBundles).find(([path]) => path.endsWith(`/${page.bundleKey}/page-bundle/index.ts`)); if (!match) throw new Error("已声明的插件页面 bundle 未安装。"); const module = await match[1](); if (module.pluginPageBundle.key !== page.bundleKey || module.pluginPageBundle.version !== page.bundleVersion || module.pluginPageBundle.integritySha256 !== page.bundleIntegritySha256) throw new Error("插件页面 bundle 完整性或版本校验失败。"); return ({ context, workspace, availability }) => module.renderPluginPage({ createElement: (awaitReact()).createElement }, { page, context, workspace, availability }); } function awaitReact(): typeof import("react") { // React is supplied by the authenticated platform shell; bundles never own a second runtime. return requireReact(); } function requireReact(): typeof import("react") { return globalThis.__PLUGIN_PAGE_REACT__; } declare global { var __PLUGIN_PAGE_REACT__: typeof import("react"); }