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 absMicroAppWithMemoHistoryPath = join(namespace, 'MicroAppWithMemoHistory.jsx');
const absRuntimePath = join(namespace, 'runtime.js'); const absRuntimePath = join(namespace, 'runtime.js');
const absMasterOptionsPath = join(namespace, 'masterOptions.js'); const absMasterOptionsPath = join(namespace, 'masterOptions.js');
const absGetMicroAppRouteCompPath = join(namespace, 'getMicroAppRouteComponent.jsx');
api.onGenerateFiles(() => { api.onGenerateFiles(() => {
const HAS_PLUGIN_MODEL = api.hasPlugins(['@fesjs/plugin-model']); const HAS_PLUGIN_MODEL = api.hasPlugins(['@fesjs/plugin-model']);
@ -85,11 +84,4 @@ export default function (api) {
source: absMicroAppWithMemoHistoryPath, source: absMicroAppWithMemoHistoryPath,
}, },
]); ]);
api.addPluginExports(() => [
{
specifiers: ['getMicroAppRouteComponent'],
source: absGetMicroAppRouteCompPath,
},
]);
} }

View File

@ -1,44 +1,36 @@
import { join } from 'path';
import { readFileSync } from 'fs';
import { defaultHistoryType } from '../constants'; import { defaultHistoryType } from '../constants';
function getMicroApp(options) { let index = 0;
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,
)} })
})()`;
}
function modifyRoutesWithAttachMode({ routes, masterHistoryType, base, namespace }) { function modifyRoutesWithAttachMode({ routes, api, namespace, masterHistoryType, base }) {
const patchRoutes = (_routes) => { if (!routes.length) return;
if (_routes.length) { routes.forEach((route) => {
_routes.forEach((route) => { if (route.meta && route.meta.microApp) {
if (route.meta && route.meta.microApp) { const fileName = `MicroAppRouteComponent${index++}.vue`;
route.component = getMicroApp({ route.component = `@@/${namespace}/${fileName}`;
key: route.path, api.writeTmpFile({
cacheName: route.meta.cacheName ?? route.path, path: join(namespace, fileName),
microAppName: route.meta.microApp, content: api.utils.Mustache.render(readFileSync(join(__dirname, 'runtime/MicroAppRouteComponent.tpl'), 'utf-8'), {
masterHistoryType, cacheName: route.meta.cacheName ?? route.path,
base, microAppName: route.meta.microApp,
namespace, masterHistoryType,
}); base,
} namespace,
if (route.children?.length) { }),
modifyRoutesWithAttachMode({
routes: route.children,
masterHistoryType,
base,
namespace,
});
}
}); });
} }
}; if (route.children?.length) {
modifyRoutesWithAttachMode({
patchRoutes(routes); routes: route.children,
api,
return routes; namespace,
masterHistoryType,
base,
});
}
});
} }
export default function modifyRoutes({ api, namespace }) { export default function modifyRoutes({ api, namespace }) {
@ -47,10 +39,11 @@ export default function modifyRoutes({ api, namespace }) {
const masterHistoryType = (router && router.mode) || defaultHistoryType; const masterHistoryType = (router && router.mode) || defaultHistoryType;
modifyRoutesWithAttachMode({ modifyRoutesWithAttachMode({
api,
namespace,
routes, routes,
masterHistoryType, masterHistoryType,
base: base || '/', base: base || '/',
namespace,
}); });
return routes; 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} />;
}