Commit 6262c079 by Lin

feat: 自动路由文件类型补充

1 parent 4947e631
/**
* 路由类型扩展
* 扩展 Vue Router 的 RouteRecordRaw 接口,支持自定义字段
*/
import 'vue-router';
declare module 'vue-router' {
interface RouteRecordRaw {
/** 路由唯一标识 */
id?: string;
/** 路由标题 */
title?: string;
/** 路由图标 */
icon?: string;
/** 路由层级 */
level?: number;
/** 是否隐藏路由 */
hidden?: boolean;
/** 允许其他自定义属性 */
[key: string]: unknown;
}
}
......@@ -4,33 +4,33 @@ import { existsSync, readdirSync, statSync } from "node:fs";
import { basename, dirname, extname, join, relative } from "node:path";
export interface RouteConfig {
// 路由路径(如果不提供,将根据文件路径自动生成)
path?: string;
// 路由名称(如果不提供,将根据文件路径自动生成)
name?: string;
// 路由标题(用于 meta.title)
title?: string;
// 子路由配置
children?: RouteConfig[];
// 是否隐藏路由(不生成)
hidden?: boolean;
// 是否在菜单中隐藏
hideInMenu?: boolean;
// 路由元数据
meta?: Record<string, any>;
// 子路由配置
children?: RouteConfig[];
// 路由名称(如果不提供,将根据文件路径自动生成)
name?: string;
// 路由路径(如果不提供,将根据文件路径自动生成)
path?: string;
// 路由标题(用于 meta.title)
title?: string;
}
export interface RouteGeneratorOptions {
// views 目录路径
viewsDir: string;
// 路由配置文件路径(可选,用于覆盖默认规则)
configFile?: string;
// 路由前缀(所有路由都会加上这个前缀)
routePrefix?: string;
// 排除的文件或目录
exclude?: (RegExp | string)[];
// 自定义路由生成规则
customRules?: (filePath: string, routeConfig: RouteConfig) => null | RouteConfig;
// 排除的文件或目录
exclude?: (RegExp | string)[];
// 路由前缀(所有路由都会加上这个前缀)
routePrefix?: string;
// views 目录路径
viewsDir: string;
}
/**
......@@ -47,7 +47,7 @@ function pathToRoutePath(filePath: string, viewsDir: string): string {
const pathWithoutExt = relativePath.replace(/\.(vue|tsx|jsx)$/, "");
// 处理 index.vue 文件
let routePath = pathWithoutExt.replace(/\/index$/, "").replace(/\\/g, "/");
let routePath = pathWithoutExt.replace(/\/index$/, "").replaceAll("\\", "/");
// 转换为 kebab-case
routePath = routePath
......@@ -55,7 +55,7 @@ function pathToRoutePath(filePath: string, viewsDir: string): string {
.map((segment: string) => {
// 将 PascalCase 或 camelCase 转换为 kebab-case
return segment
.replace(/([A-Z])/g, "-$1")
.replaceAll(/([A-Z])/g, "-$1")
.toLowerCase()
.replace(/^-/, "");
})
......@@ -86,7 +86,7 @@ function pathToRouteName(filePath: string, viewsDir: string): string {
const pathWithoutExt = relativePath.replace(/\.(vue|tsx|jsx)$/, "");
// 移除 index
let name = pathWithoutExt.replace(/\/index$/, "").replace(/\\/g, "/");
let name = pathWithoutExt.replace(/\/index$/, "").replaceAll("\\", "/");
// 转换为 PascalCase
name = name
......@@ -116,7 +116,7 @@ function extractTitle(filePath: string, _viewsDir: string): string {
// 如果是 index.vue,使用目录名
if (fileName === "index") {
return dirName
.replace(/([A-Z])/g, " $1")
.replaceAll(/([A-Z])/g, " $1")
.trim()
.split(" ")
.map((word: string) => word.charAt(0).toUpperCase() + word.slice(1))
......@@ -125,7 +125,7 @@ function extractTitle(filePath: string, _viewsDir: string): string {
// 否则使用文件名
return fileName
.replace(/([A-Z])/g, " $1")
.replaceAll(/([A-Z])/g, " $1")
.trim()
.split(" ")
.map((word: string) => word.charAt(0).toUpperCase() + word.slice(1))
......@@ -149,7 +149,7 @@ function generateAuthority(routePath: string): string {
}
// 将路径中的 / 替换为 :
authority = authority.replace(/\//g, ":");
authority = authority.replaceAll("/", ":");
// 转换为小写
return authority.toLowerCase();
......@@ -191,7 +191,7 @@ function generateRouteId(filePath: string, viewsDir: string, isIndex: boolean):
// 将目录名转换为小写 kebab-case
const dirId = dirName
.replace(/([A-Z])/g, "-$1")
.replaceAll(/([A-Z])/g, "-$1")
.toLowerCase()
.replace(/^-/, "");
......@@ -202,7 +202,7 @@ function generateRouteId(filePath: string, viewsDir: string, isIndex: boolean):
// 非 index 文件:目录名-文件名
const fileId = fileName
.replace(/([A-Z])/g, "-$1")
.replaceAll(/([A-Z])/g, "-$1")
.toLowerCase()
.replace(/^-/, "");
......@@ -213,7 +213,7 @@ function generateRouteId(filePath: string, viewsDir: string, isIndex: boolean):
* 检查文件是否应该被排除
*/
function shouldExclude(filePath: string, exclude: (RegExp | string)[] = []): boolean {
const relativePath = filePath.replace(/\\/g, "/");
const relativePath = filePath.replaceAll("\\", "/");
for (const pattern of exclude) {
if (typeof pattern === "string") {
......@@ -248,13 +248,13 @@ export function generateRoutes(options: RouteGeneratorOptions): RouteRecordRaw[]
// 用于存储目录下的文件,以便后续组织成嵌套路由
interface FileInfo {
directory: string;
fullPath: string;
routePath: string;
routeName: string;
title: string;
isIndex: boolean;
directory: string;
routeConfig: RouteConfig;
routeName: string;
routePath: string;
title: string;
}
/**
......@@ -332,7 +332,7 @@ export function generateRoutes(options: RouteGeneratorOptions): RouteRecordRaw[]
const directoryMap = new Map<string, FileInfo[]>();
for (const file of allFiles) {
const dir = file.directory.replace(/\\/g, "/");
const dir = file.directory.replaceAll("\\", "/");
if (!directoryMap.has(dir)) {
directoryMap.set(dir, []);
}
......@@ -345,7 +345,7 @@ export function generateRoutes(options: RouteGeneratorOptions): RouteRecordRaw[]
// 首先生成所有 index 文件作为父路由
for (const file of allFiles) {
if (file.isIndex && !processedFiles.has(file)) {
const dir = file.directory.replace(/\\/g, "/");
const dir = file.directory.replaceAll("\\", "/");
const dirFiles = directoryMap.get(dir) || [];
// 查找同目录下的非 index 文件作为子路由
......@@ -357,7 +357,7 @@ export function generateRoutes(options: RouteGeneratorOptions): RouteRecordRaw[]
// 例如:父路由 /assistant,子文件 addUpdate.vue -> 子路由路径为 add-update
const childFileName = basename(childFile.fullPath, extname(childFile.fullPath));
const childRoutePath = childFileName
.replace(/([A-Z])/g, "-$1")
.replaceAll(/([A-Z])/g, "-$1")
.toLowerCase()
.replace(/^-/, "");
......@@ -365,7 +365,7 @@ export function generateRoutes(options: RouteGeneratorOptions): RouteRecordRaw[]
const childId = generateRouteId(childFile.fullPath, viewsDir, false);
// 计算组件导入路径
const childRelativeToViews = relative(viewsDir, childFile.fullPath).replace(/\\/g, "/");
const childRelativeToViews = relative(viewsDir, childFile.fullPath).replaceAll("\\", "/");
const childComponentPath = `@/views/${childRelativeToViews}`;
const childRoute: RouteRecordRaw = {
......@@ -390,7 +390,7 @@ export function generateRoutes(options: RouteGeneratorOptions): RouteRecordRaw[]
}
// 计算组件导入路径
const relativeToViews = relative(viewsDir, file.fullPath).replace(/\\/g, "/");
const relativeToViews = relative(viewsDir, file.fullPath).replaceAll("\\", "/");
const componentPath = `@/views/${relativeToViews}`;
const authority = generateAuthority(file.routePath);
const routeId = generateRouteId(file.fullPath, viewsDir, true);
......@@ -426,7 +426,7 @@ export function generateRoutes(options: RouteGeneratorOptions): RouteRecordRaw[]
// 处理没有 index 父路由的非 index 文件(根目录下的独立文件)
for (const file of allFiles) {
if (!processedFiles.has(file)) {
const relativeToViews = relative(viewsDir, file.fullPath).replace(/\\/g, "/");
const relativeToViews = relative(viewsDir, file.fullPath).replaceAll("\\", "/");
const componentPath = `@/views/${relativeToViews}`;
const authority = generateAuthority(file.routePath);
// 对于根目录下的独立文件,目录名为空,使用文件名作为 id
......
Markdown is supported
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!