Skip to content

Commit

Permalink
refactor: getConventionRoutes (#12576)
Browse files Browse the repository at this point in the history
* refactor: getConventionRoutes

* fix: iter()

* chore: delete core utils findParentRouteId
  • Loading branch information
Jinbao1001 committed Jul 18, 2024
1 parent ce4eff5 commit d63da9e
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 17 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
7 changes: 0 additions & 7 deletions packages/core/src/route/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,13 +13,6 @@ export function byLongestFirst(a: string, b: string): number {
return b.length - a.length;
}

export function findParentRouteId(
routeIds: string[],
childRouteId: string,
): string | undefined {
return routeIds.find((id) => childRouteId.startsWith(`${id}/`));
}

const routeModuleExts = ['.js', '.jsx', '.ts', '.tsx', '.md', '.mdx', '.vue'];
export function isRouteModuleFile(opts: { file: string; exclude?: RegExp[] }) {
// TODO: add cache strategy
Expand Down

0 comments on commit d63da9e

Please sign in to comment.