fix: remote plugin name (#2872)

This commit is contained in:
jack zhang 2023-10-19 19:17:58 +08:00 committed by GitHub
parent cc676c3dda
commit 07c5b7b0eb
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 28 additions and 21 deletions

View File

@ -47,8 +47,8 @@ export class PluginManager {
pluginData: pluginList, pluginData: pluginList,
devDynamicImport: this.app.devDynamicImport, devDynamicImport: this.app.devDynamicImport,
}); });
for await (const plugin of plugins) { for await (const [name, pluginClass] of plugins) {
await this.add(plugin); await this.add(pluginClass, { name });
} }
} catch (error) { } catch (error) {
if (401 === error?.response?.status) { if (401 === error?.response?.status) {

View File

@ -27,7 +27,10 @@ export function definePluginClient(packageName: string) {
}); });
} }
export function getRemotePlugins(requirejs: any, pluginData: PluginData[] = []): Promise<Array<typeof Plugin>> { export function getRemotePlugins(
requirejs: any,
pluginData: PluginData[] = [],
): Promise<Array<[string, typeof Plugin]>> {
requirejs.requirejs.config({ requirejs.requirejs.config({
waitSeconds: 120, waitSeconds: 120,
paths: pluginData.reduce<Record<string, string>>((acc, cur) => { paths: pluginData.reduce<Record<string, string>>((acc, cur) => {
@ -43,10 +46,13 @@ export function getRemotePlugins(requirejs: any, pluginData: PluginData[] = []):
return new Promise((resolve, reject) => { return new Promise((resolve, reject) => {
requirejs.requirejs( requirejs.requirejs(
packageNames, packageNames,
(...plugins: (typeof Plugin & { default?: typeof Plugin })[]) => { (...pluginModules: (typeof Plugin & { default?: typeof Plugin })[]) => {
const res = plugins.filter((item) => item).map((item) => item.default || item); const res = pluginModules
.map<[string, typeof Plugin]>((item, index) => [pluginData[index].name, item.default || item])
.filter((item) => item[1]);
resolve(res); resolve(res);
const emptyPlugins = plugins
const emptyPlugins = pluginModules
.map((item, index) => (!item ? index : null)) .map((item, index) => (!item ? index : null))
.filter((i) => i !== null) .filter((i) => i !== null)
.map((i) => pluginData[i].packageName); .map((i) => pluginData[i].packageName);
@ -69,36 +75,37 @@ interface GetPluginsOption {
devDynamicImport?: DevDynamicImport; devDynamicImport?: DevDynamicImport;
} }
export async function getPlugins(options: GetPluginsOption): Promise<Array<typeof Plugin>> { export async function getPlugins(options: GetPluginsOption): Promise<Array<[string, typeof Plugin]>> {
const { requirejs, pluginData, devDynamicImport } = options; const { requirejs, pluginData, devDynamicImport } = options;
if (pluginData.length === 0) return []; if (pluginData.length === 0) return [];
if (process.env.NODE_ENV === 'development' && !process.env.USE_REMOTE_PLUGIN) { if (process.env.NODE_ENV === 'development' && !process.env.USE_REMOTE_PLUGIN) {
const plugins: Array<typeof Plugin> = []; const res: Array<[string, typeof Plugin]> = [];
const resolveDevPlugins: Record<string, typeof Plugin> = {}; const resolveDevPlugins: Record<string, typeof Plugin> = {};
const pluginPackageNames = pluginData.map((item) => item.packageName);
if (devDynamicImport) { if (devDynamicImport) {
for await (const packageName of pluginPackageNames) { for await (const plugin of pluginData) {
const plugin = await devDynamicImport(packageName); const pluginModule = await devDynamicImport(plugin.packageName);
if (plugin) { if (pluginModule) {
plugins.push(plugin.default); res.push([plugin.name, pluginModule.default]);
resolveDevPlugins[packageName] = plugin.default; resolveDevPlugins[plugin.name] = pluginModule.default;
} else {
console.error(`[nocobase]: plugin ${plugin.packageName} load error`);
} }
} }
defineDevPlugins(resolveDevPlugins); defineDevPlugins(resolveDevPlugins);
} }
const remotePlugins = pluginData.filter((item) => !resolveDevPlugins[item.packageName]); const remotePlugins = pluginData.filter((item) => !resolveDevPlugins[item.name]);
if (remotePlugins.length === 0) { if (remotePlugins.length === 0) {
return plugins; return res;
} }
const remotePluginList = await getRemotePlugins(requirejs, remotePlugins); const remotePluginList = await getRemotePlugins(requirejs, remotePlugins);
plugins.push(...remotePluginList); res.push(...remotePluginList);
return plugins; return res;
} }
return getRemotePlugins(requirejs, pluginData); return getRemotePlugins(requirejs, pluginData);

View File

@ -250,7 +250,7 @@ export class NocoBaseBuildInPlugin extends Plugin {
}); });
} }
async addPlugins() { 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(AdminLayoutPlugin, { name: 'admin-layout' });
await this.app.pm.add(SystemSettingsPlugin, { name: 'system-setting' }); await this.app.pm.add(SystemSettingsPlugin, { name: 'system-setting' });
await this.app.pm.add(PinnedListPlugin, { 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(BlockSchemaComponentPlugin, { name: 'block-schema-component' });
await this.app.pm.add(AntdSchemaComponentPlugin, { name: 'antd-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(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(RemoteDocumentTitlePlugin, { name: 'remote-document-title' });
await this.app.pm.add(PMPlugin, { name: 'pm' }); await this.app.pm.add(PMPlugin, { name: 'builtin-pm' });
} }
} }