Skip to content

Commit

Permalink
fix(projects): fix customRoute generate repeat routes
Browse files Browse the repository at this point in the history
  • Loading branch information
honghuangdc committed Jan 18, 2024
1 parent 5f7bc70 commit 7bf1a1f
Show file tree
Hide file tree
Showing 8 changed files with 66 additions and 18 deletions.
3 changes: 2 additions & 1 deletion examples/template-vue/src/router/elegant/routes.ts
Original file line number Diff line number Diff line change
Expand Up @@ -126,6 +126,7 @@ export const generatedRoutes: GeneratedRoute[] = [
component: 'layout.base$view.user',
meta: {
title: 'user'
}
},
props: true
}
];
4 changes: 4 additions & 0 deletions examples/template-vue/src/router/elegant/transform.ts
Original file line number Diff line number Diff line change
Expand Up @@ -143,6 +143,10 @@ function transformElegantRouteToVueRoute(
const routeMap: RouteMap = {
"root": "/",
"not-found": "/:pathMatch(.*)*",
"exception": "/exception",
"exception_403": "/exception/403",
"exception_404": "/exception/404",
"exception_500": "/exception/500",
"403": "/403",
"404": "/404",
"500": "/500",
Expand Down
12 changes: 12 additions & 0 deletions examples/template-vue/src/typings/elegant-router.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,10 @@ declare module "@elegant-router/types" {
export type RouteMap = {
"root": "/";
"not-found": "/:pathMatch(.*)*";
"exception": "/exception";
"exception_403": "/exception/403";
"exception_404": "/exception/404";
"exception_500": "/exception/500";
"403": "/403";
"404": "/404";
"500": "/500";
Expand Down Expand Up @@ -50,6 +54,10 @@ declare module "@elegant-router/types" {
RouteKey,
| "root"
| "not-found"
| "exception"
| "exception_403"
| "exception_404"
| "exception_500"
>;

/**
Expand Down Expand Up @@ -78,6 +86,7 @@ declare module "@elegant-router/types" {
CustomRouteKey,
| "root"
| "not-found"
| "exception"
>;

/**
Expand All @@ -103,6 +112,9 @@ declare module "@elegant-router/types" {
CustomRouteKey,
| "root"
| "not-found"
| "exception_403"
| "exception_404"
| "exception_500"
>;

/**
Expand Down
3 changes: 3 additions & 0 deletions examples/template-vue/vite.config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,9 @@ export default defineConfig({
base: 'src/layouts/base-layout/index.vue',
blank: 'src/layouts/blank-layout/index.vue'
},
customRoutes: {
names: ['exception_403', 'exception_404', 'exception_500', 'list_detail']
},
layoutLazyImport: () => false
}),
Inspect()
Expand Down
2 changes: 1 addition & 1 deletion packages/vue/src/core/dts.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ export async function genDtsFile(
) {
if (files.length === 0) return;

const customEntries = getCustomRouteConfig(options);
const customEntries = getCustomRouteConfig(options, entries);

const code = getDtsCode(files, entries, customEntries, options.layouts);

Expand Down
54 changes: 41 additions & 13 deletions packages/vue/src/core/shared.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,41 +7,69 @@ import type { CustomRouteConfig, ElegantVueRouterOption } from '../types';
*
* @param options
*/
export function getCustomRouteConfig(options: ElegantVueRouterOption): CustomRouteConfig {
export function getCustomRouteConfig(
options: ElegantVueRouterOption,
generatedEntries: ElegantRouterNamePathEntry[]
): CustomRouteConfig {
const { map, names } = options.customRoutes;

const entries: ElegantRouterNamePathEntry[] = [];
const entryMap = new Map<string, string>();
const firstLevelRouteSet = new Set<string>();
const lastLevelRouteSet = new Set<string>();

const firstLevelRoutes: string[] = [];
const lastLevelRoutes: string[] = [];

Object.entries(map).forEach(([name, rPath]) => {
Object.entries(map || {}).forEach(([name, rPath]) => {
const routeName = options.routeNameTransformer(name.toLocaleLowerCase());
const routePath = options.routePathTransformer(routeName, rPath);

entries.push([routeName, routePath]);
firstLevelRoutes.push(routeName);
lastLevelRoutes.push(routeName);
if (!entryMap.has(routeName)) {
entryMap.set(routeName, routePath);
}

if (!firstLevelRouteSet.has(routeName)) {
firstLevelRouteSet.add(routeName);
}

if (!lastLevelRouteSet.has(routeName)) {
lastLevelRouteSet.add(routeName);
}
});

names.forEach(name => {
names?.forEach(name => {
const itemNames = splitRouterName(name);
itemNames.forEach((itemName, index) => {
const transformName = itemName.toLocaleLowerCase();
const routeName = options.routeNameTransformer(transformName);
const routePath = options.routePathTransformer(routeName, transformRouterNameToPath(transformName));

entries.push([routeName, routePath]);
if (!entryMap.has(routeName)) {
entryMap.set(routeName, routePath);
}

if (index === 0) {
firstLevelRoutes.push(routeName);
if (!firstLevelRouteSet.has(routeName)) {
firstLevelRouteSet.add(routeName);
}
}

if (index === itemNames.length - 1) {
lastLevelRoutes.push(routeName);
if (!lastLevelRouteSet.has(routeName)) {
lastLevelRouteSet.add(routeName);
}
}
});
});

const generatedNameSet = new Set<string>(generatedEntries.map(([name]) => name));
function isInGeneratedEntries(routeName: string) {
return generatedNameSet.has(routeName);
}

const entries: ElegantRouterNamePathEntry[] = Array.from(entryMap.entries()).filter(
([name]) => !isInGeneratedEntries(name)
);
const firstLevelRoutes: string[] = Array.from(firstLevelRouteSet).filter(name => !isInGeneratedEntries(name));
const lastLevelRoutes: string[] = Array.from(lastLevelRouteSet).filter(name => !isInGeneratedEntries(name));

return {
entries,
firstLevelRoutes,
Expand Down
2 changes: 1 addition & 1 deletion packages/vue/src/core/transform.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ export async function genTransformFile(options: ElegantVueRouterOption, entries:
function getTransformCode(options: ElegantVueRouterOption, entries: ElegantRouterNamePathEntry[]) {
const prefixComment = createPrefixCommentOfGenFile();

const { entries: customEntries } = getCustomRouteConfig(options);
const { entries: customEntries } = getCustomRouteConfig(options, entries);

const allEntries = [...customEntries, ...entries];

Expand Down
4 changes: 2 additions & 2 deletions packages/vue/src/types/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -70,8 +70,8 @@ export interface ElegantVueRouterOption extends ElegantRouterOption {
* ```
*/
customRoutes: {
map: Record<string, string>;
names: string[];
map?: Record<string, string>;
names?: string[];
};
/**
* the name and file path of the route layouts
Expand Down

0 comments on commit 7bf1a1f

Please sign in to comment.