import stageConfig from '@/config/stage' ; import loadable , { LoadableComponent } from '@loadable/component' ; import { IRouterItem } from '@/types/project' ; export interface IHomeRouterItem { path : string | null ; name : symbol | string | null ; component : LoadableComponent < any > ; meta : IHomeRouterItemMeta ; } export interface IHomeRouterItemMeta { title : string ; icon : string ; permission ? : string [ ] ; type : string ; } function deepTravel < T extends { children ? : T [ ] ; } > ( config , fuc ) { if ( Array . isArray ( config ) ) { config . forEach ( subConfig => { deepTravel ( subConfig , fuc ) ; } ) ; } else if ( config . children ) { config . children . forEach ( ( subConfig ) => { deepTravel ( subConfig , fuc ) ; } ) ; } else { fuc ( config ) ; } } const homeRouter = [ ] ; deepTravel < IRouterItem > ( stageConfig , viewConfig => { const viewRouter = { } as IHomeRouterItem ; viewRouter . path = viewConfig . route ; viewRouter . name = viewConfig . name ; const filePath = viewConfig . filePath . match ( /^(\w+\/)(.+)$/ ) ?. [ 2 ] ; if ( filePath ) { viewRouter . component = viewConfig . filePath . startsWith ( 'views/' ) ? loadable ( ( ) => import ( `@/views/${ filePath }` ) ) : loadable ( ( ) => import ( `@/plugins/${ filePath }` ) ) ; } viewRouter . meta = { title : viewConfig . title , icon : viewConfig . icon , permission : viewConfig . permission , type : viewConfig . type } ; homeRouter . push ( viewRouter ) ; } ) ; export default homeRouter ;