Skip to content

Commit

Permalink
perf(projects): perf route dts
Browse files Browse the repository at this point in the history
  • Loading branch information
honghuangdc committed Nov 5, 2023
1 parent 78d9864 commit 1d6f7ab
Show file tree
Hide file tree
Showing 4 changed files with 23 additions and 10 deletions.
10 changes: 5 additions & 5 deletions examples/template-vue/src/router/elegant/transform.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@

import type { RouteRecordRaw, RouteComponent } from 'vue-router';
import type { ElegantConstRoute } from '@elegant-router/vue';
import type { RouteKey, RouteMap } from '@elegant-router/types';
import type { RouteMap, RouteKey, RoutePath } from '@elegant-router/types';

/**
* transform elegant const routes to vue routes
Expand Down Expand Up @@ -163,10 +163,10 @@ export function getRoutePath(name: RouteKey) {
* get route name by route path
* @param path route path
*/
export function getRouteName(path: RouteMap[RouteKey]) {
const routeEntries = Object.entries(routeMap) as [RouteKey, RouteMap[RouteKey]][];
export function getRouteName(path: RoutePath) {
const routeEntries = Object.entries(routeMap) as [RouteKey, RoutePath][];

const routeName = routeEntries.find(([, routePath]) => routePath === path)?.[0];
const routeName: RouteKey | null = routeEntries.find(([, routePath]) => routePath === path)?.[0] || null;

return routeName || null;
return routeName;
}
5 changes: 5 additions & 0 deletions examples/template-vue/src/typings/elegant-router.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,11 @@ declare module "@elegant-router/types" {
*/
export type RouteKey = keyof RouteMap;

/**
* route path
*/
export type RoutePath = RouteMap[RouteKey];

/**
* custom route key
*/
Expand Down
5 changes: 5 additions & 0 deletions packages/vue/src/core/dts.ts
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,11 @@ declare module "@elegant-router/types" {
*/
export type RouteKey = keyof RouteMap;
/**
* route path
*/
export type RoutePath = RouteMap[RouteKey];
/**
* custom route key
*/
Expand Down
13 changes: 8 additions & 5 deletions packages/vue/src/core/transform.ts
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ function getTransformCode(options: ElegantVueRouterOption, entries: ElegantRoute
import type { RouteRecordRaw, RouteComponent } from 'vue-router';
import type { ElegantConstRoute } from '@elegant-router/vue';
import type { RouteKey, RouteMap } from '@elegant-router/types';
import type { RouteMap, RouteKey, RoutePath } from '@elegant-router/types';
/**
* transform elegant const routes to vue routes
Expand Down Expand Up @@ -158,6 +158,9 @@ function transformElegantRouteToVueRoute(
return vueRoutes;
}
/**
* map of route name and route path
*/
const routeMap: RouteMap = {
${allEntries.map(([routeName, routePath]) => `"${routeName}": "${routePath}"`).join(',\n ')}
};
Expand All @@ -174,12 +177,12 @@ export function getRoutePath(name: RouteKey) {
* get route name by route path
* @param path route path
*/
export function getRouteName(path: RouteMap[RouteKey]) {
const routeEntries = Object.entries(routeMap) as [RouteKey, RouteMap[RouteKey]][];
export function getRouteName(path: RoutePath) {
const routeEntries = Object.entries(routeMap) as [RouteKey, RoutePath][];
const routeName = routeEntries.find(([, routePath]) => routePath === path)?.[0];
const routeName: RouteKey | null = routeEntries.find(([, routePath]) => routePath === path)?.[0] || null;
return routeName || null;
return routeName;
}
`;

Expand Down

0 comments on commit 1d6f7ab

Please sign in to comment.