vite-plugin-generoutes 2.0.0-beta.1 → 2.0.0-beta.2

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/README.md CHANGED
@@ -56,7 +56,6 @@ export default defineConfig({
56
56
  | `layoutsFolder` | `string` | `'src/layouts'` | Path to layouts folder |
57
57
  | `ignoreFolders` | `string[]` | `['components']` | Folders to ignore when generating routes |
58
58
  | `routesPath` | `string` | Auto-detected | Path to generated routes file. Auto-detected based on `tsconfig.json` presence (`.ts` if exists, otherwise `.js`) |
59
- | `nested` | `boolean` | `false` | Whether to generate nested routes |
60
59
 
61
60
  ### 📘 TypeScript Support
62
61
 
@@ -107,7 +106,7 @@ defineOptions({
107
106
 
108
107
  ### 🌲 Nested Routes
109
108
 
110
- With the `nested: true` option enabled, you can set nested route relationships using the `parent` property:
109
+ Use the `parent` property to set nested route relationships (handled automatically):
111
110
 
112
111
  ```vue
113
112
  <script setup>
@@ -197,8 +196,7 @@ export default defineConfig({
197
196
  generoutes({
198
197
  pagesFolder: 'src/views',
199
198
  ignoreFolders: ['components', 'assets'],
200
- routesPath: 'src/router/routes.js',
201
- nested: true
199
+ routesPath: 'src/router/routes.js'
202
200
  })
203
201
  ]
204
202
  })
package/README.zh_CN.md CHANGED
@@ -56,7 +56,6 @@ export default defineConfig({
56
56
  | `layoutsFolder` | `string` | `'src/layouts'` | 布局组件文件夹路径 |
57
57
  | `ignoreFolders` | `string[]` | `['components']` | 生成路由时忽略的文件夹 |
58
58
  | `routesPath` | `string` | 自动检测 | 生成的路由文件路径,根据 `tsconfig.json` 是否存在自动检测(存在则为 `.ts`,否则为 `.js`) |
59
- | `nested` | `boolean` | `false` | 是否生成嵌套路由 |
60
59
 
61
60
  ### 📘 TypeScript 支持
62
61
 
@@ -107,7 +106,7 @@ defineOptions({
107
106
 
108
107
  ### 🌲 嵌套路由
109
108
 
110
- 启用`nested: true`选项后,可以通过`parent`属性设置嵌套路由关系:
109
+ 使用 `parent` 属性即可建立嵌套路由关系(无需额外开关,自动生效):
111
110
 
112
111
  ```vue
113
112
  <script setup>
@@ -197,8 +196,7 @@ export default defineConfig({
197
196
  generoutes({
198
197
  pagesFolder: 'src/views',
199
198
  ignoreFolders: ['components', 'assets'],
200
- routesPath: 'src/router/routes.js',
201
- nested: true
199
+ routesPath: 'src/router/routes.js'
202
200
  })
203
201
  ],
204
202
  })
package/dist/index.d.ts CHANGED
@@ -1,41 +1,31 @@
1
1
  import { Plugin } from 'vite';
2
+ import { RouteRecordRaw } from 'vue-router';
2
3
 
3
4
  /**
4
5
  * Route meta information
5
6
  */
6
7
  interface RouteMeta {
7
- /** Route title */
8
+ /** Page title */
8
9
  title?: string;
9
- /** Route icon */
10
+ /** Page icon */
10
11
  icon?: string;
12
+ /** Page code */
13
+ code?: string;
14
+ /** Page layout */
15
+ layout?: string | false;
11
16
  /** Whether authentication is required */
12
- requiresAuth?: boolean;
17
+ requireAuth?: boolean;
18
+ /** Whether to keep alive */
19
+ keepAlive?: boolean;
13
20
  /** Whether the route is enabled */
14
21
  enabled?: boolean;
15
- /** Layout component name, false to disable layout wrapping */
16
- layout?: string | false;
22
+ /** Whether it's the home page */
23
+ isHome?: boolean;
24
+ /** Whether it's a login page */
25
+ isLogin?: boolean;
17
26
  /** Custom properties */
18
27
  [key: string]: unknown;
19
28
  }
20
- /**
21
- * Internal route representation used during generation
22
- */
23
- interface InternalRoute {
24
- /** Route name */
25
- name: string;
26
- /** Route path */
27
- path: string;
28
- /** Redirect path */
29
- redirect?: string;
30
- /** Route component placeholder */
31
- component?: string;
32
- /** Route meta information */
33
- meta: RouteMeta;
34
- /** Parent route name (for nested routes) */
35
- parent?: string;
36
- /** Child routes */
37
- children?: InternalRoute[];
38
- }
39
29
 
40
30
  /**********************************
41
31
  * @Author: Ronnie Zhang
@@ -48,22 +38,10 @@ interface InternalRoute {
48
38
  /**
49
39
  * Generated route record with proper typing for vue-router
50
40
  */
51
- interface GeneratedRoute {
52
- /** Route name */
53
- name: string;
54
- /** Route path */
55
- path: string;
56
- /** Redirect path */
57
- redirect?: string;
58
- /** Route component */
59
- component?: () => Promise<any>;
60
- /** Route meta information */
61
- meta: RouteMeta;
62
- /** Parent route name (for nested routes) */
63
- parent?: string;
64
- /** Child routes */
41
+ type GeneratedRoute = RouteRecordRaw & {
42
+ meta?: RouteMeta;
65
43
  children?: GeneratedRoute[];
66
- }
44
+ };
67
45
  interface Options {
68
46
  /**
69
47
  * pages folder
@@ -90,13 +68,7 @@ interface Options {
90
68
  * @default src/router/routes.ts (if tsconfig.json exists) or src/router/routes.js
91
69
  */
92
70
  routesPath: string;
93
- /**
94
- * nested routes
95
- *
96
- * @default false
97
- */
98
- nested: boolean;
99
71
  }
100
72
  declare function VitePluginGeneroutes(options?: Partial<Options>): Plugin;
101
73
 
102
- export { type GeneratedRoute, type InternalRoute, type Options, type RouteMeta, VitePluginGeneroutes, VitePluginGeneroutes as default };
74
+ export { type GeneratedRoute, type Options, type RouteMeta, VitePluginGeneroutes, VitePluginGeneroutes as default };
package/dist/index.js CHANGED
@@ -13,7 +13,7 @@ function toPascalCase(str) {
13
13
  return p1.toUpperCase();
14
14
  });
15
15
  }
16
- function convertToTree(routes) {
16
+ function nestRoutes(routes) {
17
17
  const nodeMap = {};
18
18
  const result = [];
19
19
  routes.forEach((route) => {
@@ -68,43 +68,40 @@ function VitePluginGeneroutes(options = {}) {
68
68
  const pagesFolder = options.pagesFolder || "src/pages";
69
69
  const layoutsFolder = options.layoutsFolder || "src/layouts";
70
70
  const ignoreFolders = options.ignoreFolders || ["components"];
71
- const nested = options.nested || false;
72
71
  const defineOptionsCache = /* @__PURE__ */ new Map();
73
72
  function detectTypeScriptProject() {
74
73
  const tsconfigPath = path.resolve(rootDir, "tsconfig.json");
75
74
  return fs.existsSync(tsconfigPath);
76
75
  }
77
- function generateMenusAndRoutes() {
76
+ function generateRoutes() {
78
77
  const pages = globSync(`${pagesFolder}/**/*.vue`, { ignore: ignoreFolders.map((folder) => `${pagesFolder}/**/${folder}/**`) });
79
- const routes = pages.map((filePath) => {
78
+ const routes = [];
79
+ for (let filePath of pages) {
80
80
  filePath = slash(filePath);
81
81
  const defineOptions = parseDefineOptions(filePath) || {};
82
82
  defineOptionsCache.set(filePath, JSON.stringify(defineOptions));
83
83
  const meta = defineOptions.meta || {};
84
84
  if (meta.enabled === false)
85
- return null;
85
+ continue;
86
86
  const pathSegments = filePath.replace(`${pagesFolder}`, "").replace(".vue", "").replace("index", "").split("/").filter((item) => !!item && !/^\(.*\)$/.test(item));
87
87
  const name = defineOptions.name || pathSegments.map((item) => toPascalCase(item)).join("_") || "Index";
88
88
  const component = `##/${filePath}@@${name}##`;
89
89
  const routePath = `/${pathSegments.map((item) => item.replace(/\[(.*?)\]/g, (_, p1) => p1 === "...all" ? ":pathMatch(.*)*" : p1.split(",").map((i) => `:${i}`).join("/"))).join("/")}`;
90
- return {
90
+ routes.push({
91
91
  name,
92
92
  path: routePath,
93
93
  redirect: defineOptions.redirect,
94
+ parent: defineOptions.parent,
94
95
  component: defineOptions.redirect ? void 0 : component,
95
- meta,
96
- parent: defineOptions.parent
97
- };
98
- }).filter((route) => route !== null);
96
+ meta
97
+ });
98
+ }
99
99
  const { duplicateNames, duplicatePaths } = findDuplicateRoutes(routes);
100
100
  if (duplicateNames.length)
101
101
  console.warn(`Warning: Duplicate names found in routes: ${duplicateNames.join(", ")}`);
102
102
  if (duplicatePaths.length)
103
103
  console.warn(`Warning: Duplicate paths found in routes: ${duplicatePaths.join(", ")}`);
104
- const processedRoutes = wrapRoutesWithLayout(nested ? convertToTree(routes) : routes);
105
- return {
106
- routes: processedRoutes
107
- };
104
+ return wrapRoutesWithLayout(nestRoutes(routes));
108
105
  }
109
106
  function wrapRoutesWithLayout(routes) {
110
107
  const layoutGroups = /* @__PURE__ */ new Map();
@@ -124,50 +121,17 @@ function VitePluginGeneroutes(options = {}) {
124
121
  }
125
122
  }
126
123
  const layoutRoutes = Array.from(layoutGroups, ([layoutName, children]) => ({
127
- name: `LAYOUT_${layoutName.toUpperCase()}`,
124
+ name: `__LAYOUT_${layoutName.toUpperCase()}__`,
128
125
  path: `/__layout_${layoutName}__`,
129
126
  component: `@@layout@@/${layoutsFolder}/${layoutName}.vue@@`,
130
- meta: {},
131
127
  children
132
128
  }));
133
- return [...noLayoutRoutes, ...layoutRoutes];
129
+ return layoutRoutes.concat(noLayoutRoutes);
134
130
  }
135
131
  async function writerRoutesFile(isInit = false) {
136
- const { routes } = generateMenusAndRoutes();
132
+ const routes = generateRoutes();
137
133
  const typeDefinitions = isTypeScript ? `
138
- import type { RouteRecordRaw } from 'vue-router'
139
-
140
- /**
141
- * Route meta information
142
- */
143
- export interface RouteMeta {
144
- /** Page title */
145
- title?: string
146
- /** Page icon */
147
- icon?: string
148
- /** Page code */
149
- code?: string
150
- /** Page layout */
151
- layout?: string | false
152
- /** Whether authentication is required */
153
- requireAuth?: boolean
154
- /** Whether to keep alive */
155
- keepAlive?: boolean
156
- /** Whether it's the home page */
157
- isHome?: boolean
158
- /** Whether it's a login page */
159
- isLogin?: boolean
160
- /** Custom properties */
161
- [key: string]: unknown
162
- }
163
-
164
- /**
165
- * Generated route type with proper typing
166
- */
167
- export type GeneratedRoute = RouteRecordRaw & {
168
- meta?: RouteMeta
169
- children?: GeneratedRoute[]
170
- }
134
+ import type { GeneratedRoute } from 'vite-plugin-generoutes'
171
135
  ` : "";
172
136
  const routesType = isTypeScript ? ": GeneratedRoute[]" : "";
173
137
  let routesStr = `
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "vite-plugin-generoutes",
3
3
  "type": "module",
4
- "version": "2.0.0-beta.1",
4
+ "version": "2.0.0-beta.2",
5
5
  "packageManager": "pnpm@10.27.0",
6
6
  "description": "A Vite plugin that generate routes based on the file structure, supports dynamic routes, and supports custom meta data for each route.",
7
7
  "author": "Ronnie Zhang <zclzone@outlook.com>",