2022-02-11 16:14:30 +08:00
|
|
|
|
import type { Component } from 'vue';
|
|
|
|
|
|
|
|
|
|
type ViewComponent = Record<string, () => Promise<Component>>;
|
|
|
|
|
|
|
|
|
|
const importViews = import.meta.glob('./**/index.vue');
|
|
|
|
|
|
|
|
|
|
const COMPONENTS_KEY = 'components';
|
2022-03-06 01:03:34 +08:00
|
|
|
|
const PREFIX = './';
|
|
|
|
|
const SUFFIX = '/index.vue';
|
2022-02-11 16:14:30 +08:00
|
|
|
|
const PATH_SPLIT_MARK = '/';
|
|
|
|
|
const ROUTE_KEY_SPLIT_MARK = '_';
|
2022-03-06 01:03:34 +08:00
|
|
|
|
/** 系统的内置路由,该文件夹名称不作为RouteKey */
|
|
|
|
|
const SYSTEM_VIEW = 'system-view_';
|
2022-02-11 16:14:30 +08:00
|
|
|
|
|
|
|
|
|
/** 过滤掉组件文件 */
|
2022-03-12 16:21:40 +08:00
|
|
|
|
const viewKeys = Object.keys(importViews).filter((key) => !key.includes(COMPONENTS_KEY));
|
2022-02-11 16:14:30 +08:00
|
|
|
|
|
|
|
|
|
function getViewComponent() {
|
|
|
|
|
const components: ViewComponent = {};
|
2022-03-12 16:21:40 +08:00
|
|
|
|
viewKeys.forEach((key) => {
|
2022-03-06 01:03:34 +08:00
|
|
|
|
const routeKey = key
|
|
|
|
|
.replace(PREFIX, '')
|
|
|
|
|
.replace(SUFFIX, '')
|
|
|
|
|
.replace(new RegExp(PATH_SPLIT_MARK, 'g'), ROUTE_KEY_SPLIT_MARK)
|
|
|
|
|
.replace(SYSTEM_VIEW, '');
|
2022-02-11 16:14:30 +08:00
|
|
|
|
components[routeKey] = importViews[key];
|
|
|
|
|
});
|
|
|
|
|
return components;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
export const views = getViewComponent();
|