Skip to content

Commit

Permalink
feat(projects): add getRoutePath transform
Browse files Browse the repository at this point in the history
  • Loading branch information
honghuangdc committed Nov 5, 2023
1 parent 3695b32 commit 4286946
Show file tree
Hide file tree
Showing 6 changed files with 111 additions and 62 deletions.
26 changes: 25 additions & 1 deletion examples/template-vue/src/router/elegant/transform.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@

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

/**
* transform elegant const routes to vue routes
Expand Down Expand Up @@ -129,5 +130,28 @@ function transformElegantRouteToVueRoute(
vueRoutes.unshift(vueRoute);

return vueRoutes;
}
}

export function getRoutePath(key: RouteKey) {
const routeMap: RouteMap = {
"root": "/",
"not-found": "/:pathMatch(.*)*",
"403": "/403",
"404": "/404",
"500": "/500",
"about": "/about",
"list": "/list",
"list_detail": "/list/detail",
"list_home": "/list/home",
"multi-menu": "/multi-menu",
"multi-menu_first": "/multi-menu/first",
"multi-menu_first_child": "/multi-menu/first/child",
"multi-menu_second": "/multi-menu/second",
"multi-menu_second_child": "/multi-menu/second/child",
"multi-menu_second_child_home": "/multi-menu/second/child/home",
"user": "/user/:id"
};

return routeMap[key];
}

59 changes: 4 additions & 55 deletions packages/vue/src/core/dts.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
import path from 'node:path';
import { writeFile } from 'node:fs/promises';
import { PAGE_DEGREE_SPLITTER, splitRouterName, transformRouterNameToPath } from '@elegant-router/core';
import { PAGE_DEGREE_SPLITTER } from '@elegant-router/core';
import type { ElegantRouterFile, ElegantRouterNamePathEntry } from '@elegant-router/core';
import { ensureFile } from '../shared/fs';
import type { ElegantVueRouterOption } from '../types';
import { createPrefixCommentOfGenFile } from './comment';
import { getCustomRouteConfig } from './shared';
import { ensureFile } from '../shared/fs';
import type { ElegantVueRouterOption, CustomRouteConfig } from '../types';
import { LAYOUT_PREFIX, VIEW_PREFIX } from '../constants';

export async function genDtsFile(
Expand All @@ -27,58 +28,6 @@ export async function genDtsFile(
await writeFile(dtsPath, code);
}

type CustomRouteConfig = {
entries: ElegantRouterNamePathEntry[];
firstLevelRoutes: string[];
lastLevelRoutes: string[];
};

/**
* get custom route config
* @param options
*/
function getCustomRouteConfig(options: ElegantVueRouterOption): CustomRouteConfig {
const { map, names } = options.customRoutes;

const entries: ElegantRouterNamePathEntry[] = [];

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

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);
});

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 (index === 0) {
firstLevelRoutes.push(routeName);
}

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

return {
entries,
firstLevelRoutes,
lastLevelRoutes
};
}

function getDtsCode(
files: ElegantRouterFile[],
entries: ElegantRouterNamePathEntry[],
Expand Down
2 changes: 1 addition & 1 deletion packages/vue/src/core/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ export default class ElegantVueRouter {

this.options = createPluginOptions(this.elegantRouter.options, options);

genTransformFile(this.options);
genTransformFile(this.options, this.elegantRouter.entries);

this.generate();
}
Expand Down
49 changes: 49 additions & 0 deletions packages/vue/src/core/shared.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
import { splitRouterName, transformRouterNameToPath } from '@elegant-router/core';
import type { ElegantRouterNamePathEntry } from '@elegant-router/core';
import type { ElegantVueRouterOption, CustomRouteConfig } from '../types';

/**
* get custom route config
* @param options
*/
export function getCustomRouteConfig(options: ElegantVueRouterOption): CustomRouteConfig {
const { map, names } = options.customRoutes;

const entries: ElegantRouterNamePathEntry[] = [];

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

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);
});

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 (index === 0) {
firstLevelRoutes.push(routeName);
}

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

return {
entries,
firstLevelRoutes,
lastLevelRoutes
};
}
26 changes: 22 additions & 4 deletions packages/vue/src/core/transform.ts
Original file line number Diff line number Diff line change
@@ -1,15 +1,17 @@
import path from 'node:path';
import { writeFile } from 'node:fs/promises';
import type { ElegantRouterNamePathEntry } from '@elegant-router/core';
import { createPrefixCommentOfGenFile } from './comment';
import { getCustomRouteConfig } from './shared';
import { ensureFile } from '../shared/fs';
import type { ElegantVueRouterOption } from '../types';

/**
* generate the transform file
* @param options
*/
export async function genTransformFile(options: ElegantVueRouterOption) {
const code = getTransformCode();
export async function genTransformFile(options: ElegantVueRouterOption, entries: ElegantRouterNamePathEntry[]) {
const code = getTransformCode(options, entries);

const transformPath = path.posix.join(options.cwd, options.transformDir);

Expand All @@ -18,13 +20,18 @@ export async function genTransformFile(options: ElegantVueRouterOption) {
await writeFile(transformPath, code);
}

function getTransformCode() {
function getTransformCode(options: ElegantVueRouterOption, entries: ElegantRouterNamePathEntry[]) {
const prefixComment = createPrefixCommentOfGenFile();

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

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

const code = `${prefixComment}
import type { RouteRecordRaw, RouteComponent } from 'vue-router';
import type { ElegantConstRoute } from '@elegant-router/vue';
import type { RouteKey, RouteMap } from '@elegant-router/types';
/**
* transform elegant const routes to vue routes
Expand Down Expand Up @@ -149,8 +156,19 @@ function transformElegantRouteToVueRoute(
vueRoutes.unshift(vueRoute);
return vueRoutes;
}
}
/**
* get route path by route key
* @param key route key
*/
export function getRoutePath(key: RouteKey) {
const routeMap: RouteMap = {
${allEntries.map(([routeName, routePath]) => `"${routeName}": "${routePath}"`).join(',\n ')}
};
return routeMap[key];
}
`;

return code;
Expand Down
11 changes: 10 additions & 1 deletion packages/vue/src/types/index.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import type { _RouteRecordBase } from 'vue-router';
import type { ElegantRouterOption } from '@elegant-router/core';
import type { ElegantRouterOption, ElegantRouterNamePathEntry } from '@elegant-router/core';

export interface ElegantVueRouterOption extends ElegantRouterOption {
/**
Expand Down Expand Up @@ -106,6 +106,15 @@ export interface ElegantVueRouterOption extends ElegantRouterOption {
onRouteMetaGen(routeName: string): Record<string, unknown>;
}

/**
*
*/
export type CustomRouteConfig = {
entries: ElegantRouterNamePathEntry[];
firstLevelRoutes: string[];
lastLevelRoutes: string[];
};

/**
* elegant const route
*/
Expand Down

0 comments on commit 4286946

Please sign in to comment.