vite-plugin-generoutes 0.2.4 → 0.2.6
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 +11 -34
- package/dist/index.d.ts +1 -1
- package/dist/index.js +13 -6
- package/package.json +2 -2
package/README.md
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
# vite-plugin-generoutes
|
|
2
2
|
|
|
3
|
-
A Vite plugin that
|
|
3
|
+
A Vite plugin that generate routes based on the file structure, supports dynamic routes, and supports custom meta data for each route.
|
|
4
4
|
|
|
5
5
|
## Usage
|
|
6
6
|
|
|
@@ -39,68 +39,44 @@ src/routes/pages
|
|
|
39
39
|
└── [...all].vue
|
|
40
40
|
```
|
|
41
41
|
|
|
42
|
-
The above example will generate the routes file `src/
|
|
42
|
+
The above example will generate the routes file `src/pages/generoutes.js` with the following content:
|
|
43
43
|
```js
|
|
44
44
|
export const routes = [
|
|
45
45
|
{
|
|
46
46
|
name: 'Index',
|
|
47
47
|
path: '/',
|
|
48
48
|
component: () => import('/src/pages/index.vue'),
|
|
49
|
-
meta: {
|
|
50
|
-
title: 'Index',
|
|
51
|
-
show: true,
|
|
52
|
-
keepAlive: false,
|
|
53
|
-
},
|
|
49
|
+
meta: {},
|
|
54
50
|
},
|
|
55
51
|
{
|
|
56
52
|
name: 'User',
|
|
57
53
|
path: '/user',
|
|
58
54
|
component: () => import('/src/pages/user/index.vue'),
|
|
59
|
-
meta: {
|
|
60
|
-
title: 'User',
|
|
61
|
-
show: true,
|
|
62
|
-
keepAlive: false,
|
|
63
|
-
},
|
|
55
|
+
meta: {},
|
|
64
56
|
},
|
|
65
57
|
{
|
|
66
58
|
name: 'User_[id]',
|
|
67
59
|
path: '/user/:id',
|
|
68
60
|
component: () => import('/src/pages/user/[id]/index.vue'),
|
|
69
|
-
meta: {
|
|
70
|
-
title: 'User_[id]',
|
|
71
|
-
show: true,
|
|
72
|
-
keepAlive: false,
|
|
73
|
-
},
|
|
61
|
+
meta: {},
|
|
74
62
|
},
|
|
75
63
|
{
|
|
76
64
|
name: 'User_Post',
|
|
77
65
|
path: '/user/post',
|
|
78
66
|
component: () => import('/src/pages/user/post/index.vue'),
|
|
79
|
-
meta: {
|
|
80
|
-
title: 'User_Post',
|
|
81
|
-
show: true,
|
|
82
|
-
keepAlive: false,
|
|
83
|
-
},
|
|
67
|
+
meta: {},
|
|
84
68
|
},
|
|
85
69
|
{
|
|
86
70
|
name: 'Index_[...all]',
|
|
87
71
|
path: '/:pathMatch(.*)*',
|
|
88
72
|
component: () => import('/src/pages/[...all].vue'),
|
|
89
|
-
meta: {
|
|
90
|
-
title: '404 Not Found',
|
|
91
|
-
show: true,
|
|
92
|
-
keepAlive: false,
|
|
93
|
-
},
|
|
73
|
+
meta: {},
|
|
94
74
|
},
|
|
95
75
|
{
|
|
96
76
|
name: 'User_Post_[...all]',
|
|
97
77
|
path: '/user/post/:pathMatch(.*)*',
|
|
98
78
|
component: () => import('/src/pages/user/post/[...all].vue'),
|
|
99
|
-
meta: {
|
|
100
|
-
title: 'User_Post_[...all]',
|
|
101
|
-
show: true,
|
|
102
|
-
keepAlive: false,
|
|
103
|
-
},
|
|
79
|
+
meta: {},
|
|
104
80
|
},
|
|
105
81
|
]
|
|
106
82
|
```
|
|
@@ -109,7 +85,7 @@ export const routes = [
|
|
|
109
85
|
|
|
110
86
|
```js
|
|
111
87
|
import { createRouter, createWebHashHistory } from 'vue-router'
|
|
112
|
-
import { routes } from './generoutes'
|
|
88
|
+
import { routes } from './pages/generoutes'
|
|
113
89
|
|
|
114
90
|
const router = createRouter({
|
|
115
91
|
history: createWebHashHistory(),
|
|
@@ -127,9 +103,10 @@ export default router
|
|
|
127
103
|
- Support custom meta data for each route.
|
|
128
104
|
- Support ghost paths, For example, the (admin) folder will not be part of the route path, which is very useful for folder grouping.
|
|
129
105
|
- Support immediate update of the routes file when the file structure or defineOptions changes.
|
|
106
|
+
- Support nested route.
|
|
130
107
|
|
|
131
108
|
## Custom route info,including name and meta
|
|
132
109
|
|
|
133
110
|
You can define `name` and `meta` fields in the `defineOptions` of your `.vue` file, which will be used to override the default properties of the generated route. The `name` field will be used as the route name, which is very useful for `KeepAlive`. Any property in `defineOptions.meta` will be used as a property of the route `meta`, which makes the route metadata very flexible.
|
|
134
111
|
|
|
135
|
-
When you make any changes that may affect the route result, the `src/
|
|
112
|
+
When you make any changes that may affect the route result, the `src/pages/generoutes.js` file will be updated immediately, and the page will be refreshed without restarting the server.
|
package/dist/index.d.ts
CHANGED
|
@@ -3,7 +3,7 @@ import { Plugin } from 'vite';
|
|
|
3
3
|
/**********************************
|
|
4
4
|
* @Author: Ronnie Zhang
|
|
5
5
|
* @LastEditor: Ronnie Zhang
|
|
6
|
-
* @LastEditTime:
|
|
6
|
+
* @LastEditTime: 2024/07/01 14:51:47
|
|
7
7
|
* @Email: zclzone@outlook.com
|
|
8
8
|
* Copyright © 2023 Ronnie Zhang(大脸怪) | https://isme.top
|
|
9
9
|
**********************************/
|
package/dist/index.js
CHANGED
|
@@ -79,19 +79,20 @@ function VitePluginGeneroutes(options = {}) {
|
|
|
79
79
|
filePath = slash(filePath);
|
|
80
80
|
const defineOptions = parseDefineOptions(filePath) || {};
|
|
81
81
|
defineOptionsCache.set(filePath, JSON.stringify(defineOptions));
|
|
82
|
-
const meta = defineOptions
|
|
82
|
+
const meta = defineOptions.meta || {};
|
|
83
83
|
if (meta.enabled === false)
|
|
84
84
|
return null;
|
|
85
85
|
const pathSegments = filePath.replace(`${pagesFolder}`, "").replace(".vue", "").replace("index", "").split("/").filter((item) => !!item && !/^\(.*\)$/.test(item));
|
|
86
|
-
const name = defineOptions
|
|
86
|
+
const name = defineOptions.name || pathSegments.map((item) => toPascalCase(item)).join("_") || "Index";
|
|
87
87
|
const component = `##/${filePath}##`;
|
|
88
88
|
const routePath = `/${pathSegments.map((item) => item.replace(/\[(.*?)\]/g, (_, p1) => p1 === "...all" ? ":pathMatch(.*)*" : p1.split(",").map((i) => `:${i}`).join("/"))).join("/")}`;
|
|
89
89
|
return {
|
|
90
90
|
name,
|
|
91
91
|
path: routePath,
|
|
92
|
+
redirect: defineOptions.redirect,
|
|
92
93
|
component,
|
|
93
94
|
meta,
|
|
94
|
-
parent: defineOptions
|
|
95
|
+
parent: defineOptions.parent
|
|
95
96
|
};
|
|
96
97
|
}).filter(Boolean);
|
|
97
98
|
const { duplicateNames, duplicatePaths } = findDuplicateRoutes(routes);
|
|
@@ -118,15 +119,18 @@ function VitePluginGeneroutes(options = {}) {
|
|
|
118
119
|
fs.writeFileSync(filePath, routesStr);
|
|
119
120
|
}
|
|
120
121
|
const debounceWriter = debounce(500, writerRoutesFile);
|
|
122
|
+
let watcher;
|
|
121
123
|
function createWatcher() {
|
|
122
|
-
|
|
124
|
+
watcher = chokidar.watch(`${pagesFolder}/**/*.vue`, { ignoreInitial: true });
|
|
123
125
|
return watcher.on("all", async (event, path2) => {
|
|
124
126
|
if (ignoreFolders.some((folder) => slash(path2).includes(`/${folder}/`)))
|
|
125
127
|
return;
|
|
126
128
|
if (path2.endsWith(".vue") && (event === "add" || event === "unlink")) {
|
|
127
129
|
debounceWriter();
|
|
128
|
-
|
|
129
|
-
|
|
130
|
+
if (watcher) {
|
|
131
|
+
await watcher.close();
|
|
132
|
+
createWatcher();
|
|
133
|
+
}
|
|
130
134
|
}
|
|
131
135
|
});
|
|
132
136
|
}
|
|
@@ -145,6 +149,9 @@ function VitePluginGeneroutes(options = {}) {
|
|
|
145
149
|
debounceWriter();
|
|
146
150
|
}
|
|
147
151
|
}
|
|
152
|
+
},
|
|
153
|
+
closeBundle() {
|
|
154
|
+
watcher && watcher.close();
|
|
148
155
|
}
|
|
149
156
|
};
|
|
150
157
|
}
|
package/package.json
CHANGED
|
@@ -1,9 +1,9 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "vite-plugin-generoutes",
|
|
3
3
|
"type": "module",
|
|
4
|
-
"version": "0.2.
|
|
4
|
+
"version": "0.2.6",
|
|
5
5
|
"packageManager": "pnpm@9.1.1",
|
|
6
|
-
"description": "
|
|
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>",
|
|
8
8
|
"license": "MIT",
|
|
9
9
|
"homepage": "https://github.com/zclzone/vite-plugin-generoutes",
|