Skip to content

Commit

Permalink
refactor: getConventionRoutes
Browse files Browse the repository at this point in the history
  • Loading branch information
Jinbao1001 committed Jul 16, 2024
1 parent 216bf94 commit 919a736
Showing 1 changed file with 16 additions and 10 deletions.
26 changes: 16 additions & 10 deletions packages/core/src/route/routesConvention.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,7 @@ import { winPath } from '@umijs/utils';
import { existsSync, lstatSync, readdirSync, statSync } from 'fs';
import { extname, relative, resolve } from 'path';
import { defineRoutes } from './defineRoutes';
import {
byLongestFirst,
createRouteId,
findParentRouteId,
isRouteModuleFile,
} from './utils';
import { byLongestFirst, createRouteId, isRouteModuleFile } from './utils';

// opts.base: path of pages
export function getConventionRoutes(opts: {
Expand All @@ -30,11 +25,22 @@ export function getConventionRoutes(opts: {
});

const routeIds = Object.keys(files).sort(byLongestFirst);

const parentToChildrenMap = new Map();
routeIds.forEach((id) => {
const prefix = `${id}/`;
routeIds
.filter((childId) => childId.startsWith(prefix) && childId !== id)
.forEach((childId) => {
if (!parentToChildrenMap.has(id)) {
parentToChildrenMap.set(id, []);
}
parentToChildrenMap.get(id).push(childId);
});
});
function defineNestedRoutes(defineRoute: any, parentId?: string) {
const childRouteIds = routeIds.filter(
(id) => findParentRouteId(routeIds, id) === parentId,
);
const childRouteIds = parentId
? parentToChildrenMap.get(parentId)
: routeIds;
for (let routeId of childRouteIds) {
let routePath = createRoutePath(
parentId ? routeId.slice(parentId.length + 1) : routeId,
Expand Down

0 comments on commit 919a736

Please sign in to comment.