fix: 优化根据meta创建qiankun子路由逻辑

This commit is contained in:
wanchun 2022-11-28 16:04:32 +08:00
parent 7f94eccc3a
commit 06134f03bb
4 changed files with 54 additions and 51 deletions

View File

@ -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,
},
]);
}

View File

@ -1,45 +1,37 @@
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) => {
function modifyRoutesWithAttachMode({ routes, api, namespace, masterHistoryType, base }) {
if (!routes.length) return;
routes.forEach((route) => {
if (route.meta && route.meta.microApp) {
route.component = getMicroApp({
key: route.path,
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,
}),
});
}
if (route.children?.length) {
modifyRoutesWithAttachMode({
routes: route.children,
api,
namespace,
masterHistoryType,
base,
namespace,
});
}
});
}
};
patchRoutes(routes);
return routes;
}
export default function modifyRoutes({ api, namespace }) {
api.modifyRoutes((routes) => {
@ -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;

View File

@ -0,0 +1,24 @@
<template>
<MicroApp :name="name" :base="base" :masterHistoryType="masterHistoryType" :cacheName="cacheName"> </MicroApp>
</template>
<script>
// eslint-disable-next-line import/extensions
import { MicroApp } from './MicroApp';
export default {
components: { MicroApp },
setup() {
const name = '{{{microAppName}}}';
const base = '{{{base}}}';
const masterHistoryType = '{{{masterHistoryType}}}';
const cacheName = '{{{cacheName}}}';
return {
name,
base,
masterHistoryType,
cacheName,
};
},
};
</script>

View File

@ -1,6 +0,0 @@
// eslint-disable-next-line import/extensions
import { MicroApp } from './MicroApp';
export function getMicroAppRouteComponent({ key, appName, base, masterHistoryType, routeProps, cacheName }) {
return <MicroApp key={key} base={base} masterHistoryType={masterHistoryType} name={appName} cacheName={cacheName} {...routeProps} />;
}