mirror of
https://github.com/WeBankFinTech/fes.js.git
synced 2025-06-29 10:04:44 +08:00
fix: 优化根据meta创建qiankun子路由逻辑
This commit is contained in:
parent
7f94eccc3a
commit
06134f03bb
@ -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,
|
||||
},
|
||||
]);
|
||||
}
|
||||
|
@ -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;
|
||||
|
@ -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>
|
@ -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} />;
|
||||
}
|
Loading…
x
Reference in New Issue
Block a user