2024-05-08 13:43:24 +08:00
|
|
|
import { routeFilenameHelper } from '@/utils/file/routeFileUtil';
|
|
|
|
import { RouteRecordRaw } from 'vue-router';
|
|
|
|
|
|
|
|
// * 最终路由
|
|
|
|
const routeMap: Record<string, RouteRecordRaw> = {};
|
|
|
|
|
|
|
|
// * 所有处理的路由
|
|
|
|
const contexts = [
|
2024-05-08 14:18:50 +08:00
|
|
|
{ context: import.meta.glob('@/views/**/index.vue', { eager: true, import: 'default' }), isIndex: true },
|
|
|
|
{ context: import.meta.glob('@/views/**/page.ts', { eager: true, import: 'default' }), isIndex: false },
|
2024-05-08 13:43:24 +08:00
|
|
|
];
|
|
|
|
|
|
|
|
/**
|
|
|
|
* 遍历路由信息
|
|
|
|
* @param context 路由上下文
|
|
|
|
* @param isIndex 是否为索引遍历
|
|
|
|
*/
|
|
|
|
const createRouteList = (context: any, isIndex: boolean) => {
|
|
|
|
Object.entries(context).forEach(([filePath, exportRouteConfig]) => {
|
|
|
|
const fileInfo = routeFilenameHelper(filePath);
|
|
|
|
// 组装路由对象
|
|
|
|
const route: any = {
|
|
|
|
name: fileInfo?.name,
|
|
|
|
path: fileInfo!.path,
|
2024-05-08 14:18:50 +08:00
|
|
|
component: isIndex ? () => import(/* @vite-ignore */ fileInfo!.filePath, {}) : undefined,
|
2024-05-08 13:43:24 +08:00
|
|
|
};
|
|
|
|
// 初始化赋值
|
|
|
|
if (isIndex) {
|
|
|
|
routeMap[route.path] = route;
|
2024-05-08 14:18:50 +08:00
|
|
|
} else {
|
2024-05-08 13:43:24 +08:00
|
|
|
// 导出当前存在的路由并重新赋值
|
|
|
|
const existingRoute = routeMap[route.path];
|
|
|
|
if (existingRoute) {
|
|
|
|
routeMap[route.path] = { ...existingRoute, ...(exportRouteConfig as any) };
|
|
|
|
}
|
|
|
|
}
|
2024-05-08 14:18:50 +08:00
|
|
|
});
|
2024-05-08 13:43:24 +08:00
|
|
|
};
|
|
|
|
|
|
|
|
// * 生成路由信息
|
2024-05-08 14:18:50 +08:00
|
|
|
contexts.forEach(({ context, isIndex }) => createRouteList(context, isIndex));
|
2024-05-08 13:43:24 +08:00
|
|
|
|
2024-05-08 14:18:50 +08:00
|
|
|
export const pageRoutes: Array<RouteRecordRaw> = [...Object.values(routeMap)];
|