From 07c5b7b0ebd4248ab69d996fced10e4dcbcf95e6 Mon Sep 17 00:00:00 2001 From: jack zhang <1098626505@qq.com> Date: Thu, 19 Oct 2023 19:17:58 +0800 Subject: [PATCH] fix: remote plugin name (#2872) --- .../client/src/application/PluginManager.ts | 4 +- .../src/application/utils/remotePlugins.ts | 39 +++++++++++-------- .../src/nocobase-buildin-plugin/index.tsx | 6 +-- 3 files changed, 28 insertions(+), 21 deletions(-) diff --git a/packages/core/client/src/application/PluginManager.ts b/packages/core/client/src/application/PluginManager.ts index de600347c..b75328148 100644 --- a/packages/core/client/src/application/PluginManager.ts +++ b/packages/core/client/src/application/PluginManager.ts @@ -47,8 +47,8 @@ export class PluginManager { pluginData: pluginList, devDynamicImport: this.app.devDynamicImport, }); - for await (const plugin of plugins) { - await this.add(plugin); + for await (const [name, pluginClass] of plugins) { + await this.add(pluginClass, { name }); } } catch (error) { if (401 === error?.response?.status) { diff --git a/packages/core/client/src/application/utils/remotePlugins.ts b/packages/core/client/src/application/utils/remotePlugins.ts index 702e11cd3..b45011b1a 100644 --- a/packages/core/client/src/application/utils/remotePlugins.ts +++ b/packages/core/client/src/application/utils/remotePlugins.ts @@ -27,7 +27,10 @@ export function definePluginClient(packageName: string) { }); } -export function getRemotePlugins(requirejs: any, pluginData: PluginData[] = []): Promise> { +export function getRemotePlugins( + requirejs: any, + pluginData: PluginData[] = [], +): Promise> { requirejs.requirejs.config({ waitSeconds: 120, paths: pluginData.reduce>((acc, cur) => { @@ -43,10 +46,13 @@ export function getRemotePlugins(requirejs: any, pluginData: PluginData[] = []): return new Promise((resolve, reject) => { requirejs.requirejs( packageNames, - (...plugins: (typeof Plugin & { default?: typeof Plugin })[]) => { - const res = plugins.filter((item) => item).map((item) => item.default || item); + (...pluginModules: (typeof Plugin & { default?: typeof Plugin })[]) => { + const res = pluginModules + .map<[string, typeof Plugin]>((item, index) => [pluginData[index].name, item.default || item]) + .filter((item) => item[1]); resolve(res); - const emptyPlugins = plugins + + const emptyPlugins = pluginModules .map((item, index) => (!item ? index : null)) .filter((i) => i !== null) .map((i) => pluginData[i].packageName); @@ -69,36 +75,37 @@ interface GetPluginsOption { devDynamicImport?: DevDynamicImport; } -export async function getPlugins(options: GetPluginsOption): Promise> { +export async function getPlugins(options: GetPluginsOption): Promise> { const { requirejs, pluginData, devDynamicImport } = options; if (pluginData.length === 0) return []; if (process.env.NODE_ENV === 'development' && !process.env.USE_REMOTE_PLUGIN) { - const plugins: Array = []; + const res: Array<[string, typeof Plugin]> = []; const resolveDevPlugins: Record = {}; - const pluginPackageNames = pluginData.map((item) => item.packageName); if (devDynamicImport) { - for await (const packageName of pluginPackageNames) { - const plugin = await devDynamicImport(packageName); - if (plugin) { - plugins.push(plugin.default); - resolveDevPlugins[packageName] = plugin.default; + for await (const plugin of pluginData) { + const pluginModule = await devDynamicImport(plugin.packageName); + if (pluginModule) { + res.push([plugin.name, pluginModule.default]); + resolveDevPlugins[plugin.name] = pluginModule.default; + } else { + console.error(`[nocobase]: plugin ${plugin.packageName} load error`); } } defineDevPlugins(resolveDevPlugins); } - const remotePlugins = pluginData.filter((item) => !resolveDevPlugins[item.packageName]); + const remotePlugins = pluginData.filter((item) => !resolveDevPlugins[item.name]); if (remotePlugins.length === 0) { - return plugins; + return res; } const remotePluginList = await getRemotePlugins(requirejs, remotePlugins); - plugins.push(...remotePluginList); - return plugins; + res.push(...remotePluginList); + return res; } return getRemotePlugins(requirejs, pluginData); diff --git a/packages/core/client/src/nocobase-buildin-plugin/index.tsx b/packages/core/client/src/nocobase-buildin-plugin/index.tsx index c0f448a7f..d1b6cd682 100644 --- a/packages/core/client/src/nocobase-buildin-plugin/index.tsx +++ b/packages/core/client/src/nocobase-buildin-plugin/index.tsx @@ -250,7 +250,7 @@ export class NocoBaseBuildInPlugin extends Plugin { }); } async addPlugins() { - await this.app.pm.add(LocalePlugin, { name: 'locale' }); + await this.app.pm.add(LocalePlugin, { name: 'builtin-locale' }); await this.app.pm.add(AdminLayoutPlugin, { name: 'admin-layout' }); await this.app.pm.add(SystemSettingsPlugin, { name: 'system-setting' }); await this.app.pm.add(PinnedListPlugin, { @@ -275,8 +275,8 @@ export class NocoBaseBuildInPlugin extends Plugin { await this.app.pm.add(BlockSchemaComponentPlugin, { name: 'block-schema-component' }); await this.app.pm.add(AntdSchemaComponentPlugin, { name: 'antd-schema-component' }); await this.app.pm.add(SigninPageExtensionPlugin, { name: 'signin-page-extension' }); - await this.app.pm.add(ACLPlugin, { name: 'acl' }); + await this.app.pm.add(ACLPlugin, { name: 'builtin-acl' }); await this.app.pm.add(RemoteDocumentTitlePlugin, { name: 'remote-document-title' }); - await this.app.pm.add(PMPlugin, { name: 'pm' }); + await this.app.pm.add(PMPlugin, { name: 'builtin-pm' }); } }