From 58e05d233a2705b27fa754876e1e035b37b90180 Mon Sep 17 00:00:00 2001 From: "zexuan.dzx" Date: Thu, 13 Jan 2022 18:20:32 +0800 Subject: [PATCH 1/4] @dev fix insert --- .../plugin-qiankun/src/master/masterRuntimePlugin.ts.tpl | 5 ++--- 1 file changed, 2 insertions(+), 3 deletions(-) diff --git a/packages/plugin-qiankun/src/master/masterRuntimePlugin.ts.tpl b/packages/plugin-qiankun/src/master/masterRuntimePlugin.ts.tpl index c79399612..53e557b86 100644 --- a/packages/plugin-qiankun/src/master/masterRuntimePlugin.ts.tpl +++ b/packages/plugin-qiankun/src/master/masterRuntimePlugin.ts.tpl @@ -52,8 +52,7 @@ function patchMicroAppRouteComponent(routes: IRouteProps[]) { const rootRoutes = getRootRoutes(routes); if (rootRoutes) { const { routeBindingAlias, base, masterHistoryType } = getMasterOptions() as MasterOptions; - const microAppAttachedRoutes = microAppRuntimeRoutes.filter(r => !r.insert); - microAppAttachedRoutes.reverse().forEach(microAppRoute => { + microAppRuntimeRoutes.reverse().forEach(microAppRoute => { const patchRoute = (route: IRouteProps) => { patchMicroAppRoute(route, getMicroAppRouteComponent, { base, masterHistoryType, routeBindingAlias }); if (route.routes?.length) { @@ -62,7 +61,7 @@ function patchMicroAppRouteComponent(routes: IRouteProps[]) { }; patchRoute(microAppRoute); - rootRoutes.unshift(microAppRoute); + !microAppRoute.insert && rootRoutes.unshift(microAppRoute); }); } } From 0f2f891bfc88860f0ff05bc9802b194811f8db4b Mon Sep 17 00:00:00 2001 From: "zexuan.dzx" Date: Tue, 12 Apr 2022 21:19:47 +0800 Subject: [PATCH 2/4] feat: sitemodeV2-support-menus --- .../src/master/masterRuntimePlugin.ts.tpl | 39 ++++++++++++++++--- packages/plugin-qiankun/src/types.ts | 8 ++++ 2 files changed, 42 insertions(+), 5 deletions(-) diff --git a/packages/plugin-qiankun/src/master/masterRuntimePlugin.ts.tpl b/packages/plugin-qiankun/src/master/masterRuntimePlugin.ts.tpl index 635fc1ad9..e4694b71a 100644 --- a/packages/plugin-qiankun/src/master/masterRuntimePlugin.ts.tpl +++ b/packages/plugin-qiankun/src/master/masterRuntimePlugin.ts.tpl @@ -12,9 +12,10 @@ import { defaultHistoryType } from './constants'; import { getMasterOptions, setMasterOptions } from './masterOptions'; // @ts-ignore import { deferred } from './qiankunDefer.js'; -import { App, HistoryType, MasterOptions, MicroAppRoute } from './types'; +import { App, HistoryType, MasterOptions, MicroAppRoute, Menu } from './types'; let microAppRuntimeRoutes: MicroAppRoute[]; +let microAppRuntimeMenus: Menu[]; async function getMasterRuntime() { const config = await plugin.applyPlugins({ @@ -61,13 +62,9 @@ function patchMicroAppRouteComponent(routes: IRouteProps[]) { }; patchRoute(microAppRoute); -<<<<<<< HEAD - !microAppRoute.insert && rootRoutes.unshift(microAppRoute); -======= if (!microAppRoute.insert && !microAppRoute.insertBefore && !microAppRoute.appendChildTo) { rootRoutes.unshift(microAppRoute); } ->>>>>>> dae9300797d298a934d97e31cea4ab191dae788a }); } } @@ -112,6 +109,7 @@ export async function render(oldRender: typeof noop) { const { apps = [], routes, ...options } = getMasterOptions(); microAppRuntimeRoutes = routes; + microAppRuntimeMenus = menus; // 主应用相关的配置注册完毕后即可开启渲染 oldRender(); @@ -142,6 +140,37 @@ export function patchRoutes({ routes }: { routes: IRouteProps[] }) { } } +export function layout(initConfig) { + const loopMenusAndDoSomeThing = (menus, callback: (targetNode, key?: string) => void, key: string = '0'): void => { + for (let i = 0; i < menus.length; i++) { + if (Array.isArray(menus[i].children) && (menus[i].children).length > 0) { + loopMenusAndDoSomeThing((menus[i].children), callback, `${key}-${i}`); + } + callback(menus[i], `${key}-${i}`); + } + } + return { + ...initConfig, + patchMenus: function (menus, initialInfo) { + if (_.isArray(microAppRuntimeMenus) && microAppRuntimeMenus.length > 0) { // + loopMenusAndDoSomeThing(microAppRuntimeMenus, (menuItem, key) => { + menuItem.key = key; // 给菜单项唯一key + if (menuItem.target) { // 处理外链 + menuItem.path = menuItem.target; + menuItem.isUrl = true; + } + }) + menus.length = 0; + menus.push(...microAppRuntimeMenus); + } + if (_.isFunction(initConfig.patchMenus)) { + initConfig.patchMenus(menus, initialInfo); + } + return menus; + }, + } +}; + function mergeExtraQiankunConfig(masterOptions: MasterOptions, extraQiankunConfig: BaseIConfig): BaseIConfig { function removeDuplicateApps(apps = new Array(), extraAppsNameSet?: Set): App[] { let _extraAppsNameSet = extraAppsNameSet || new Set( diff --git a/packages/plugin-qiankun/src/types.ts b/packages/plugin-qiankun/src/types.ts index ffec346fc..392f498b7 100644 --- a/packages/plugin-qiankun/src/types.ts +++ b/packages/plugin-qiankun/src/types.ts @@ -22,6 +22,14 @@ export type MicroAppRoute = { microApp: string; } & Record; +export type Menu = { + children?: Menu[], + name?: string, + icon?: string, + path?: string, + target?: string, +} & Record; + export type MasterOptions = { enable?: boolean; apps?: App[]; From cd5d96c01b60bbcd016d7489a84119581d875c38 Mon Sep 17 00:00:00 2001 From: "zexuan.dzx" Date: Tue, 12 Apr 2022 21:21:26 +0800 Subject: [PATCH 3/4] feat: sitemodeV2-support-menus --- packages/plugin-qiankun/src/master/masterRuntimePlugin.ts.tpl | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/packages/plugin-qiankun/src/master/masterRuntimePlugin.ts.tpl b/packages/plugin-qiankun/src/master/masterRuntimePlugin.ts.tpl index e4694b71a..ff92d1118 100644 --- a/packages/plugin-qiankun/src/master/masterRuntimePlugin.ts.tpl +++ b/packages/plugin-qiankun/src/master/masterRuntimePlugin.ts.tpl @@ -107,7 +107,7 @@ export async function render(oldRender: typeof noop) { // 更新 master options setMasterOptions(masterOptions); - const { apps = [], routes, ...options } = getMasterOptions(); + const { apps = [], routes, menus, ...options } = getMasterOptions(); microAppRuntimeRoutes = routes; microAppRuntimeMenus = menus; From 0a19e17d150feb97d0ecdada09749e418a2f738f Mon Sep 17 00:00:00 2001 From: "zexuan.dzx" Date: Tue, 12 Apr 2022 21:26:39 +0800 Subject: [PATCH 4/4] feat: sitemodeV2-support-menus --- packages/plugin-qiankun/src/master/masterRuntimePlugin.ts.tpl | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/packages/plugin-qiankun/src/master/masterRuntimePlugin.ts.tpl b/packages/plugin-qiankun/src/master/masterRuntimePlugin.ts.tpl index ff92d1118..19b7da968 100644 --- a/packages/plugin-qiankun/src/master/masterRuntimePlugin.ts.tpl +++ b/packages/plugin-qiankun/src/master/masterRuntimePlugin.ts.tpl @@ -152,7 +152,8 @@ export function layout(initConfig) { return { ...initConfig, patchMenus: function (menus, initialInfo) { - if (_.isArray(microAppRuntimeMenus) && microAppRuntimeMenus.length > 0) { // + // 尝试使用站点菜单配置 + if (_.isArray(microAppRuntimeMenus) && microAppRuntimeMenus.length > 0) { loopMenusAndDoSomeThing(microAppRuntimeMenus, (menuItem, key) => { menuItem.key = key; // 给菜单项唯一key if (menuItem.target) { // 处理外链