@venus/tools
Venus 项目工具包,提供通用的工具函数和 Vite 插件。
安装
pnpm add @venus/tools
功能
自动路由生成插件
自动根据 views 目录中的文件结构生成 Vue Router 路由配置。
使用方法
在 vite.config.ts 中配置:
import { defineConfig } from "vite";
import { autoRoutes } from "@venus/tools/src/vite-plugins/auto-routes";
export default defineConfig({
plugins: [
autoRoutes({
viewsDir: "src/views", // views 目录
outputFile: "src/router/auto-routes.ts", // 输出文件
routePrefix: "", // 路由前缀
exclude: ["components", "NotFound.vue"], // 排除文件
watch: true, // 开发模式监听
customRules: (filePath, routeConfig) => {
// 自定义路由规则
return routeConfig;
},
}),
],
});
配置选项
-
viewsDir: views 目录路径(默认:'src/views') -
outputFile: 生成的路由文件路径(默认:'src/router/auto-routes.ts') -
routePrefix: 路由前缀(默认:'') -
exclude: 排除的文件或目录(默认:['components', 'components/', 'NotFound.vue']) -
watch: 是否在开发模式下监听文件变化(默认:true) -
customRules: 自定义路由生成规则函数
路由生成规则
-
一级目录(index 文件):作为父路由,
level: 0,id为文件夹名小写 -
子菜单(非 index 文件):作为父路由的
children,level: 2,id为文件夹名-文件名 -
默认属性:
-
icon: 空字符串 -
hidden: false -
meta.authority: 根据路由路径自动生成 -
meta.keepAlive: false -
meta.order: 非 index 文件默认为 1 -
meta.requiresAuth: false
-
导出
// Vite 插件
import { autoRoutes } from "@venus/tools/src/vite-plugins/auto-routes";
import type { AutoRoutesOptions } from "@venus/tools/src/vite-plugins/auto-routes";
// 路由生成工具
import { generateRoutes, loadRouteConfig } from "@venus/tools/src/utils/route-generator";
import type { RouteConfig, RouteGeneratorOptions } from "@venus/tools/src/utils/route-generator";
开发
# 构建
pnpm build
# 类型检查
pnpm type-check