import { lazy } from "react";
import { RouteObject } from "react-router-dom";

const context = require.context('../page/', true, /index\.(tsx)$/, 'lazy')

function lazyLoad(path: string) {
    const Module = lazy(() => import(`../page${path}`));
    return <Module />;
}

const createRootChildren = (name: string): RouteObject[] => {
    const rootRouter = context.keys().filter(key => !key.includes('components')).map(key => {
        key = key.replace(/(\.|\/index.tsx)/ig, '')
        return {
            path: '/' + name + key,
            element: lazyLoad(key),
        }
    })
    return rootRouter;
}
const router = createRootChildren('weComTask')
const routerConfig = router
export default routerConfig