mirror of
https://github.com/WeBankFinTech/fes.js.git
synced 2025-04-06 03:59:53 +08:00
feat(plugin-qiankun): 主应用支持路由上配置微应用
This commit is contained in:
parent
6dc8291f0b
commit
f179419757
@ -79,7 +79,6 @@ export default (api) => {
|
|||||||
api.addRuntimePlugin(() => `@@/${absRuntimeFilePath}`);
|
api.addRuntimePlugin(() => `@@/${absRuntimeFilePath}`);
|
||||||
|
|
||||||
// 把BaseLayout插入到路由配置中,作为根路由
|
// 把BaseLayout插入到路由配置中,作为根路由
|
||||||
// TODO: fes缺少修改路由API
|
|
||||||
api.modifyRoutes(routes => [
|
api.modifyRoutes(routes => [
|
||||||
{
|
{
|
||||||
path: '/',
|
path: '/',
|
||||||
|
@ -1,6 +1,7 @@
|
|||||||
import { readFileSync } from 'fs';
|
import { readFileSync } from 'fs';
|
||||||
import { join } from 'path';
|
import { join } from 'path';
|
||||||
import { defaultMainRootId, defaultHistoryType } from '../constants';
|
import { defaultMainRootId, defaultHistoryType } from '../constants';
|
||||||
|
import modifyRoutes from './modifyRoutes';
|
||||||
|
|
||||||
const namespace = 'plugin-qiankun/main';
|
const namespace = 'plugin-qiankun/main';
|
||||||
|
|
||||||
@ -21,9 +22,12 @@ export default function (api) {
|
|||||||
mountElementId: defaultMainRootId
|
mountElementId: defaultMainRootId
|
||||||
}));
|
}));
|
||||||
|
|
||||||
|
modifyRoutes({ api, namespace });
|
||||||
|
|
||||||
const absMicroAppPath = join(namespace, 'MicroApp.js');
|
const absMicroAppPath = join(namespace, 'MicroApp.js');
|
||||||
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.js');
|
||||||
|
|
||||||
api.onGenerateFiles(() => {
|
api.onGenerateFiles(() => {
|
||||||
api.writeTmpFile({
|
api.writeTmpFile({
|
||||||
@ -36,6 +40,10 @@ export default function (api) {
|
|||||||
content: readFileSync(join(__dirname, 'runtime/runtime.tpl'), 'utf-8')
|
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 { main: options } = api.config?.qiankun || {};
|
||||||
const masterHistoryType = api.config?.router?.mode || defaultHistoryType;
|
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 || {};
|
// const { registerRuntimeKeyInIndex = false } = options || {};
|
||||||
|
|
||||||
|
58
packages/fes-plugin-qiankun/src/main/modifyRoutes.js
Normal file
58
packages/fes-plugin-qiankun/src/main/modifyRoutes.js
Normal 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;
|
||||||
|
});
|
||||||
|
}
|
@ -0,0 +1,11 @@
|
|||||||
|
import { MicroApp } from './MicroApp';
|
||||||
|
|
||||||
|
export function getMicroAppRouteComponent({
|
||||||
|
appName,
|
||||||
|
base,
|
||||||
|
masterHistoryType,
|
||||||
|
routeProps
|
||||||
|
}) {
|
||||||
|
|
||||||
|
return <MicroApp base={base} masterHistoryType={masterHistoryType} name={appName} {...routeProps} />;
|
||||||
|
}
|
Loading…
x
Reference in New Issue
Block a user