feat(plugin-qiankun): 主应用支持路由上配置微应用

This commit is contained in:
万纯 2021-03-23 20:41:53 +08:00
parent 6dc8291f0b
commit f179419757
4 changed files with 84 additions and 1 deletions

View File

@ -79,7 +79,6 @@ export default (api) => {
api.addRuntimePlugin(() => `@@/${absRuntimeFilePath}`);
// 把BaseLayout插入到路由配置中作为根路由
// TODO: fes缺少修改路由API
api.modifyRoutes(routes => [
{
path: '/',

View File

@ -1,6 +1,7 @@
import { readFileSync } from 'fs';
import { join } from 'path';
import { defaultMainRootId, defaultHistoryType } from '../constants';
import modifyRoutes from './modifyRoutes';
const namespace = 'plugin-qiankun/main';
@ -21,9 +22,12 @@ export default function (api) {
mountElementId: defaultMainRootId
}));
modifyRoutes({ api, namespace });
const absMicroAppPath = join(namespace, 'MicroApp.js');
const absRuntimePath = join(namespace, 'runtime.js');
const absMasterOptionsPath = join(namespace, 'masterOptions.js');
const absGetMicroAppRouteCompPath = join(namespace, 'getMicroAppRouteComponent.js');
api.onGenerateFiles(() => {
api.writeTmpFile({
@ -36,6 +40,10 @@ export default function (api) {
content: readFileSync(join(__dirname, 'runtime/runtime.tpl'), 'utf-8')
});
api.writeTmpFile({
path: absGetMicroAppRouteCompPath,
content: readFileSync(join(__dirname, 'runtime/getMicroAppRouteComponent.tpl'), 'utf-8')
});
const { main: options } = api.config?.qiankun || {};
const masterHistoryType = api.config?.router?.mode || defaultHistoryType;
@ -61,6 +69,13 @@ export default function (api) {
}
]);
api.addPluginExports(() => [
{
specifiers: ['getMicroAppRouteComponent'],
source: absGetMicroAppRouteCompPath
}
]);
// const { registerRuntimeKeyInIndex = false } = options || {};

View File

@ -0,0 +1,58 @@
import { defaultHistoryType } from '../constants';
function getMicroApp(options) {
const {
microAppName, masterHistoryType, base, namespace, ...normalizedRouteProps
} = options;
return `(() => {
const { getMicroAppRouteComponent } = require('@@/${namespace}/getMicroAppRouteComponent');
return getMicroAppRouteComponent({ appName: '${microAppName}', base: '${base}', masterHistoryType: '${masterHistoryType}', routeProps: ${JSON.stringify(normalizedRouteProps)} })
})()`;
}
function modifyRoutesWithAttachMode({
routes, masterHistoryType, base, namespace
}) {
const patchRoutes = (_routes) => {
if (_routes.length) {
_routes.forEach((route) => {
if (route.meta && route.meta.microApp) {
route.component = getMicroApp({
microAppName: route.meta.microApp,
masterHistoryType,
base,
namespace
});
}
if (route.children?.length) {
modifyRoutesWithAttachMode({
routes: route.children,
masterHistoryType,
base,
namespace
});
}
});
}
};
patchRoutes(routes);
return routes;
}
export default function modifyRoutes({ api, namespace }) {
api.modifyRoutes((routes) => {
const { router, base } = api.config;
const masterHistoryType = (router && router?.mode) || defaultHistoryType;
modifyRoutesWithAttachMode({
routes,
masterHistoryType,
base: base || '/',
namespace
});
return routes;
});
}

View File

@ -0,0 +1,11 @@
import { MicroApp } from './MicroApp';
export function getMicroAppRouteComponent({
appName,
base,
masterHistoryType,
routeProps
}) {
return <MicroApp base={base} masterHistoryType={masterHistoryType} name={appName} {...routeProps} />;
}