From 06134f03bbd12cc90cca9607b4e1f326e30d6282 Mon Sep 17 00:00:00 2001 From: wanchun <445436867@qq.com> Date: Mon, 28 Nov 2022 16:04:32 +0800 Subject: [PATCH] =?UTF-8?q?fix:=20=E4=BC=98=E5=8C=96=E6=A0=B9=E6=8D=AEmeta?= =?UTF-8?q?=E5=88=9B=E5=BB=BAqiankun=E5=AD=90=E8=B7=AF=E7=94=B1=E9=80=BB?= =?UTF-8?q?=E8=BE=91?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- packages/fes-plugin-qiankun/src/main/index.js | 8 --- .../src/main/modifyRoutes.js | 67 +++++++++---------- .../main/runtime/MicroAppRouteComponent.tpl | 24 +++++++ .../runtime/getMicroAppRouteComponent.jsx | 6 -- 4 files changed, 54 insertions(+), 51 deletions(-) create mode 100644 packages/fes-plugin-qiankun/src/main/runtime/MicroAppRouteComponent.tpl delete mode 100644 packages/fes-plugin-qiankun/src/main/runtime/getMicroAppRouteComponent.jsx diff --git a/packages/fes-plugin-qiankun/src/main/index.js b/packages/fes-plugin-qiankun/src/main/index.js index b741733e..74b4b6be 100644 --- a/packages/fes-plugin-qiankun/src/main/index.js +++ b/packages/fes-plugin-qiankun/src/main/index.js @@ -30,7 +30,6 @@ export default function (api) { const absMicroAppWithMemoHistoryPath = join(namespace, 'MicroAppWithMemoHistory.jsx'); const absRuntimePath = join(namespace, 'runtime.js'); const absMasterOptionsPath = join(namespace, 'masterOptions.js'); - const absGetMicroAppRouteCompPath = join(namespace, 'getMicroAppRouteComponent.jsx'); api.onGenerateFiles(() => { const HAS_PLUGIN_MODEL = api.hasPlugins(['@fesjs/plugin-model']); @@ -85,11 +84,4 @@ export default function (api) { source: absMicroAppWithMemoHistoryPath, }, ]); - - api.addPluginExports(() => [ - { - specifiers: ['getMicroAppRouteComponent'], - source: absGetMicroAppRouteCompPath, - }, - ]); } diff --git a/packages/fes-plugin-qiankun/src/main/modifyRoutes.js b/packages/fes-plugin-qiankun/src/main/modifyRoutes.js index 9435fde2..6aee94fc 100644 --- a/packages/fes-plugin-qiankun/src/main/modifyRoutes.js +++ b/packages/fes-plugin-qiankun/src/main/modifyRoutes.js @@ -1,44 +1,36 @@ +import { join } from 'path'; +import { readFileSync } from 'fs'; import { defaultHistoryType } from '../constants'; -function getMicroApp(options) { - const { key, microAppName, cacheName, masterHistoryType, base, namespace, ...normalizedRouteProps } = options; - return `(async () => { -const { getMicroAppRouteComponent } = await import('@@/${namespace}/getMicroAppRouteComponent'); -return getMicroAppRouteComponent({key: '${key}', appName: '${microAppName}', cacheName: '${cacheName}', base: '${base}', masterHistoryType: '${masterHistoryType}', routeProps: ${JSON.stringify( - normalizedRouteProps, - )} }) -})()`; -} +let index = 0; -function modifyRoutesWithAttachMode({ routes, masterHistoryType, base, namespace }) { - const patchRoutes = (_routes) => { - if (_routes.length) { - _routes.forEach((route) => { - if (route.meta && route.meta.microApp) { - route.component = getMicroApp({ - key: route.path, - cacheName: route.meta.cacheName ?? route.path, - microAppName: route.meta.microApp, - masterHistoryType, - base, - namespace, - }); - } - if (route.children?.length) { - modifyRoutesWithAttachMode({ - routes: route.children, - masterHistoryType, - base, - namespace, - }); - } +function modifyRoutesWithAttachMode({ routes, api, namespace, masterHistoryType, base }) { + if (!routes.length) return; + routes.forEach((route) => { + if (route.meta && route.meta.microApp) { + const fileName = `MicroAppRouteComponent${index++}.vue`; + route.component = `@@/${namespace}/${fileName}`; + api.writeTmpFile({ + path: join(namespace, fileName), + content: api.utils.Mustache.render(readFileSync(join(__dirname, 'runtime/MicroAppRouteComponent.tpl'), 'utf-8'), { + cacheName: route.meta.cacheName ?? route.path, + microAppName: route.meta.microApp, + masterHistoryType, + base, + namespace, + }), }); } - }; - - patchRoutes(routes); - - return routes; + if (route.children?.length) { + modifyRoutesWithAttachMode({ + routes: route.children, + api, + namespace, + masterHistoryType, + base, + }); + } + }); } export default function modifyRoutes({ api, namespace }) { @@ -47,10 +39,11 @@ export default function modifyRoutes({ api, namespace }) { const masterHistoryType = (router && router.mode) || defaultHistoryType; modifyRoutesWithAttachMode({ + api, + namespace, routes, masterHistoryType, base: base || '/', - namespace, }); return routes; diff --git a/packages/fes-plugin-qiankun/src/main/runtime/MicroAppRouteComponent.tpl b/packages/fes-plugin-qiankun/src/main/runtime/MicroAppRouteComponent.tpl new file mode 100644 index 00000000..b2b55166 --- /dev/null +++ b/packages/fes-plugin-qiankun/src/main/runtime/MicroAppRouteComponent.tpl @@ -0,0 +1,24 @@ + + diff --git a/packages/fes-plugin-qiankun/src/main/runtime/getMicroAppRouteComponent.jsx b/packages/fes-plugin-qiankun/src/main/runtime/getMicroAppRouteComponent.jsx deleted file mode 100644 index bbdc78cc..00000000 --- a/packages/fes-plugin-qiankun/src/main/runtime/getMicroAppRouteComponent.jsx +++ /dev/null @@ -1,6 +0,0 @@ -// eslint-disable-next-line import/extensions -import { MicroApp } from './MicroApp'; - -export function getMicroAppRouteComponent({ key, appName, base, masterHistoryType, routeProps, cacheName }) { - return ; -}