vite-plugin-generoutes 1.0.1 → 1.1.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/LICENSE +21 -21
- package/README.md +130 -130
- package/README.zh_CN.md +130 -130
- package/dist/index.d.ts +57 -2
- package/dist/index.js +42 -6
- package/package.json +89 -82
package/LICENSE
CHANGED
|
@@ -1,21 +1,21 @@
|
|
|
1
|
-
MIT License
|
|
2
|
-
|
|
3
|
-
Copyright (c) 2024 Ronnie Zhang(大脸怪)
|
|
4
|
-
|
|
5
|
-
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
-
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
-
in the Software without restriction, including without limitation the rights
|
|
8
|
-
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
-
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
-
furnished to do so, subject to the following conditions:
|
|
11
|
-
|
|
12
|
-
The above copyright notice and this permission notice shall be included in all
|
|
13
|
-
copies or substantial portions of the Software.
|
|
14
|
-
|
|
15
|
-
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
-
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
-
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
-
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
-
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
-
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
-
SOFTWARE.
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2024 Ronnie Zhang(大脸怪)
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
|
13
|
+
copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
+
SOFTWARE.
|
package/README.md
CHANGED
|
@@ -1,130 +1,130 @@
|
|
|
1
|
-
# vite-plugin-generoutes
|
|
2
|
-
|
|
3
|
-
A Vite plugin that automatically generates Vue router configuration based on file system.
|
|
4
|
-
|
|
5
|
-
### ✨ Features
|
|
6
|
-
|
|
7
|
-
- 📁 File-system based routing
|
|
8
|
-
- 🔄 Dynamic and nested routes support
|
|
9
|
-
- 🛠️ Hot reload - routes update when files change
|
|
10
|
-
- 🎨 Customizable route configuration
|
|
11
|
-
- 🧩 Support for route metadata via `defineOptions`
|
|
12
|
-
- 🚦 Route redirection support
|
|
13
|
-
|
|
14
|
-
### 📦 Installation
|
|
15
|
-
|
|
16
|
-
```bash
|
|
17
|
-
# npm
|
|
18
|
-
npm install vite-plugin-generoutes -D
|
|
19
|
-
|
|
20
|
-
# yarn
|
|
21
|
-
yarn add vite-plugin-generoutes -D
|
|
22
|
-
|
|
23
|
-
# pnpm
|
|
24
|
-
pnpm add vite-plugin-generoutes -D
|
|
25
|
-
```
|
|
26
|
-
|
|
27
|
-
### 🔨 Usage
|
|
28
|
-
|
|
29
|
-
Configure the plugin in your `vite.config.js`:
|
|
30
|
-
|
|
31
|
-
```javascript
|
|
32
|
-
import vue from '@vitejs/plugin-vue'
|
|
33
|
-
import { defineConfig } from 'vite'
|
|
34
|
-
import generoutes from 'vite-plugin-generoutes'
|
|
35
|
-
|
|
36
|
-
export default defineConfig({
|
|
37
|
-
plugins: [
|
|
38
|
-
vue(),
|
|
39
|
-
generoutes({
|
|
40
|
-
// options
|
|
41
|
-
})
|
|
42
|
-
]
|
|
43
|
-
})
|
|
44
|
-
```
|
|
45
|
-
|
|
46
|
-
### ⚙️ Configuration Options
|
|
47
|
-
|
|
48
|
-
| Option | Type | Default | Description |
|
|
49
|
-
| --------------- | ---------- | ------------------------ | ------------------------------------------------------- |
|
|
50
|
-
| `pagesFolder` | `string` | `'src/pages'` | Path to pages folder |
|
|
51
|
-
| `ignoreFolders` | `string[]` | `['components']` | Folders to ignore when generating routes |
|
|
52
|
-
| `routesPath` | `string` | `'src/router/routes.js'` | Path to generated routes file, can also be a `.ts` file |
|
|
53
|
-
| `nested` | `boolean` | `false` | Whether to generate nested routes |
|
|
54
|
-
|
|
55
|
-
### 📝 Routing Conventions
|
|
56
|
-
|
|
57
|
-
#### Basic Routes
|
|
58
|
-
|
|
59
|
-
- `src/pages/index.vue` -> `/`
|
|
60
|
-
- `src/pages/about.vue` -> `/about`
|
|
61
|
-
- `src/pages/users/index.vue` -> `/users`
|
|
62
|
-
- `src/pages/users/profile.vue` -> `/users/profile`
|
|
63
|
-
|
|
64
|
-
#### Dynamic Routes
|
|
65
|
-
|
|
66
|
-
- `src/pages/users/[id].vue` -> `/users/:id`
|
|
67
|
-
- `src/pages/users/[...all].vue` -> `/users/:pathMatch(.*)*`
|
|
68
|
-
- `src/pages/[org]/[repo].vue` -> `/:org/:repo`
|
|
69
|
-
|
|
70
|
-
#### Virtual Directories
|
|
71
|
-
|
|
72
|
-
- Paths with parentheses in the filename are treated as virtual directories. The parenthesized part will be omitted in the generated route path.
|
|
73
|
-
- For example: `src/pages/(auth)/login.vue` -> `/login`
|
|
74
|
-
|
|
75
|
-
### 🧠 Custom Routes
|
|
76
|
-
|
|
77
|
-
You can use `defineOptions` in your Vue files to customize route configuration:
|
|
78
|
-
|
|
79
|
-
```vue
|
|
80
|
-
<script setup>
|
|
81
|
-
defineOptions({
|
|
82
|
-
name: 'CustomRouteName',
|
|
83
|
-
meta: {
|
|
84
|
-
title: 'Page Title',
|
|
85
|
-
icon: 'home',
|
|
86
|
-
requiresAuth: true,
|
|
87
|
-
enabled: true // Set to false to exclude this route
|
|
88
|
-
},
|
|
89
|
-
redirect: '/other-route' // Set redirection
|
|
90
|
-
})
|
|
91
|
-
</script>
|
|
92
|
-
```
|
|
93
|
-
|
|
94
|
-
### 🌲 Nested Routes
|
|
95
|
-
|
|
96
|
-
With the `nested: true` option enabled, you can set nested route relationships using the `parent` property:
|
|
97
|
-
|
|
98
|
-
```vue
|
|
99
|
-
<script setup>
|
|
100
|
-
defineOptions({
|
|
101
|
-
name: 'ChildRoute',
|
|
102
|
-
parent: 'ParentRouteName',
|
|
103
|
-
meta: {
|
|
104
|
-
title: 'Child Route'
|
|
105
|
-
}
|
|
106
|
-
})
|
|
107
|
-
</script>
|
|
108
|
-
```
|
|
109
|
-
|
|
110
|
-
### 🚀 Complete Example
|
|
111
|
-
|
|
112
|
-
```javascript
|
|
113
|
-
import vue from '@vitejs/plugin-vue'
|
|
114
|
-
import { defineConfig } from 'vite'
|
|
115
|
-
import generoutes from 'vite-plugin-generoutes'
|
|
116
|
-
|
|
117
|
-
export default defineConfig({
|
|
118
|
-
plugins: [
|
|
119
|
-
vue(),
|
|
120
|
-
generoutes({
|
|
121
|
-
pagesFolder: 'src/views',
|
|
122
|
-
ignoreFolders: ['components', 'assets'],
|
|
123
|
-
routesPath: 'src/router/routes.js',
|
|
124
|
-
nested: true
|
|
125
|
-
})
|
|
126
|
-
]
|
|
127
|
-
})
|
|
128
|
-
```
|
|
129
|
-
|
|
130
|
-
[中文文档](./README.zh_CN.md)
|
|
1
|
+
# vite-plugin-generoutes
|
|
2
|
+
|
|
3
|
+
A Vite plugin that automatically generates Vue router configuration based on file system.
|
|
4
|
+
|
|
5
|
+
### ✨ Features
|
|
6
|
+
|
|
7
|
+
- 📁 File-system based routing
|
|
8
|
+
- 🔄 Dynamic and nested routes support
|
|
9
|
+
- 🛠️ Hot reload - routes update when files change
|
|
10
|
+
- 🎨 Customizable route configuration
|
|
11
|
+
- 🧩 Support for route metadata via `defineOptions`
|
|
12
|
+
- 🚦 Route redirection support
|
|
13
|
+
|
|
14
|
+
### 📦 Installation
|
|
15
|
+
|
|
16
|
+
```bash
|
|
17
|
+
# npm
|
|
18
|
+
npm install vite-plugin-generoutes -D
|
|
19
|
+
|
|
20
|
+
# yarn
|
|
21
|
+
yarn add vite-plugin-generoutes -D
|
|
22
|
+
|
|
23
|
+
# pnpm
|
|
24
|
+
pnpm add vite-plugin-generoutes -D
|
|
25
|
+
```
|
|
26
|
+
|
|
27
|
+
### 🔨 Usage
|
|
28
|
+
|
|
29
|
+
Configure the plugin in your `vite.config.js`:
|
|
30
|
+
|
|
31
|
+
```javascript
|
|
32
|
+
import vue from '@vitejs/plugin-vue'
|
|
33
|
+
import { defineConfig } from 'vite'
|
|
34
|
+
import generoutes from 'vite-plugin-generoutes'
|
|
35
|
+
|
|
36
|
+
export default defineConfig({
|
|
37
|
+
plugins: [
|
|
38
|
+
vue(),
|
|
39
|
+
generoutes({
|
|
40
|
+
// options
|
|
41
|
+
})
|
|
42
|
+
]
|
|
43
|
+
})
|
|
44
|
+
```
|
|
45
|
+
|
|
46
|
+
### ⚙️ Configuration Options
|
|
47
|
+
|
|
48
|
+
| Option | Type | Default | Description |
|
|
49
|
+
| --------------- | ---------- | ------------------------ | ------------------------------------------------------- |
|
|
50
|
+
| `pagesFolder` | `string` | `'src/pages'` | Path to pages folder |
|
|
51
|
+
| `ignoreFolders` | `string[]` | `['components']` | Folders to ignore when generating routes |
|
|
52
|
+
| `routesPath` | `string` | `'src/router/routes.js'` | Path to generated routes file, can also be a `.ts` file |
|
|
53
|
+
| `nested` | `boolean` | `false` | Whether to generate nested routes |
|
|
54
|
+
|
|
55
|
+
### 📝 Routing Conventions
|
|
56
|
+
|
|
57
|
+
#### Basic Routes
|
|
58
|
+
|
|
59
|
+
- `src/pages/index.vue` -> `/`
|
|
60
|
+
- `src/pages/about.vue` -> `/about`
|
|
61
|
+
- `src/pages/users/index.vue` -> `/users`
|
|
62
|
+
- `src/pages/users/profile.vue` -> `/users/profile`
|
|
63
|
+
|
|
64
|
+
#### Dynamic Routes
|
|
65
|
+
|
|
66
|
+
- `src/pages/users/[id].vue` -> `/users/:id`
|
|
67
|
+
- `src/pages/users/[...all].vue` -> `/users/:pathMatch(.*)*`
|
|
68
|
+
- `src/pages/[org]/[repo].vue` -> `/:org/:repo`
|
|
69
|
+
|
|
70
|
+
#### Virtual Directories
|
|
71
|
+
|
|
72
|
+
- Paths with parentheses in the filename are treated as virtual directories. The parenthesized part will be omitted in the generated route path.
|
|
73
|
+
- For example: `src/pages/(auth)/login.vue` -> `/login`
|
|
74
|
+
|
|
75
|
+
### 🧠 Custom Routes
|
|
76
|
+
|
|
77
|
+
You can use `defineOptions` in your Vue files to customize route configuration:
|
|
78
|
+
|
|
79
|
+
```vue
|
|
80
|
+
<script setup>
|
|
81
|
+
defineOptions({
|
|
82
|
+
name: 'CustomRouteName',
|
|
83
|
+
meta: {
|
|
84
|
+
title: 'Page Title',
|
|
85
|
+
icon: 'home',
|
|
86
|
+
requiresAuth: true,
|
|
87
|
+
enabled: true // Set to false to exclude this route
|
|
88
|
+
},
|
|
89
|
+
redirect: '/other-route' // Set redirection
|
|
90
|
+
})
|
|
91
|
+
</script>
|
|
92
|
+
```
|
|
93
|
+
|
|
94
|
+
### 🌲 Nested Routes
|
|
95
|
+
|
|
96
|
+
With the `nested: true` option enabled, you can set nested route relationships using the `parent` property:
|
|
97
|
+
|
|
98
|
+
```vue
|
|
99
|
+
<script setup>
|
|
100
|
+
defineOptions({
|
|
101
|
+
name: 'ChildRoute',
|
|
102
|
+
parent: 'ParentRouteName',
|
|
103
|
+
meta: {
|
|
104
|
+
title: 'Child Route'
|
|
105
|
+
}
|
|
106
|
+
})
|
|
107
|
+
</script>
|
|
108
|
+
```
|
|
109
|
+
|
|
110
|
+
### 🚀 Complete Example
|
|
111
|
+
|
|
112
|
+
```javascript
|
|
113
|
+
import vue from '@vitejs/plugin-vue'
|
|
114
|
+
import { defineConfig } from 'vite'
|
|
115
|
+
import generoutes from 'vite-plugin-generoutes'
|
|
116
|
+
|
|
117
|
+
export default defineConfig({
|
|
118
|
+
plugins: [
|
|
119
|
+
vue(),
|
|
120
|
+
generoutes({
|
|
121
|
+
pagesFolder: 'src/views',
|
|
122
|
+
ignoreFolders: ['components', 'assets'],
|
|
123
|
+
routesPath: 'src/router/routes.js',
|
|
124
|
+
nested: true
|
|
125
|
+
})
|
|
126
|
+
]
|
|
127
|
+
})
|
|
128
|
+
```
|
|
129
|
+
|
|
130
|
+
[中文文档](./README.zh_CN.md)
|
package/README.zh_CN.md
CHANGED
|
@@ -1,130 +1,130 @@
|
|
|
1
|
-
# vite-plugin-generoutes
|
|
2
|
-
|
|
3
|
-
一个基于文件系统自动生成Vue路由配置的Vite插件。
|
|
4
|
-
|
|
5
|
-
### ✨ 特性
|
|
6
|
-
|
|
7
|
-
- 📁 基于文件系统的路由生成
|
|
8
|
-
- 🔄 支持动态路由和嵌套路由
|
|
9
|
-
- 🛠️ 热重载 - 文件变化时自动更新路由
|
|
10
|
-
- 🎨 可自定义路由配置
|
|
11
|
-
- 🧩 支持通过`defineOptions`设置路由元数据
|
|
12
|
-
- 🚦 支持路由重定向
|
|
13
|
-
|
|
14
|
-
### 📦 安装
|
|
15
|
-
|
|
16
|
-
```bash
|
|
17
|
-
# npm
|
|
18
|
-
npm install vite-plugin-generoutes -D
|
|
19
|
-
|
|
20
|
-
# yarn
|
|
21
|
-
yarn add vite-plugin-generoutes -D
|
|
22
|
-
|
|
23
|
-
# pnpm
|
|
24
|
-
pnpm add vite-plugin-generoutes -D
|
|
25
|
-
```
|
|
26
|
-
|
|
27
|
-
### 🔨 使用方法
|
|
28
|
-
|
|
29
|
-
在`vite.config.js`中配置插件:
|
|
30
|
-
|
|
31
|
-
```javascript
|
|
32
|
-
import vue from '@vitejs/plugin-vue'
|
|
33
|
-
import { defineConfig } from 'vite'
|
|
34
|
-
import generoutes from 'vite-plugin-generoutes'
|
|
35
|
-
|
|
36
|
-
export default defineConfig({
|
|
37
|
-
plugins: [
|
|
38
|
-
vue(),
|
|
39
|
-
generoutes({
|
|
40
|
-
// 配置选项
|
|
41
|
-
})
|
|
42
|
-
]
|
|
43
|
-
})
|
|
44
|
-
```
|
|
45
|
-
|
|
46
|
-
### ⚙️ 配置选项
|
|
47
|
-
|
|
48
|
-
| 选项 | 类型 | 默认值 | 描述 |
|
|
49
|
-
| --------------- | ---------- | ------------------------ | ------------------------------------- |
|
|
50
|
-
| `pagesFolder` | `string` | `'src/pages'` | 页面文件夹路径 |
|
|
51
|
-
| `ignoreFolders` | `string[]` | `['components']` | 生成路由时忽略的文件夹 |
|
|
52
|
-
| `routesPath` | `string` | `'src/router/routes.js'` | 生成的路由文件路径,也可以是`.ts`文件 |
|
|
53
|
-
| `nested` | `boolean` | `false` | 是否生成嵌套路由 |
|
|
54
|
-
|
|
55
|
-
### 📝 路由规则
|
|
56
|
-
|
|
57
|
-
#### 基本路由
|
|
58
|
-
|
|
59
|
-
- `src/pages/index.vue` -> `/`
|
|
60
|
-
- `src/pages/about.vue` -> `/about`
|
|
61
|
-
- `src/pages/users/index.vue` -> `/users`
|
|
62
|
-
- `src/pages/users/profile.vue` -> `/users/profile`
|
|
63
|
-
|
|
64
|
-
#### 动态路由
|
|
65
|
-
|
|
66
|
-
- `src/pages/users/[id].vue` -> `/users/:id`
|
|
67
|
-
- `src/pages/users/[...all].vue` -> `/users/:pathMatch(.*)*`
|
|
68
|
-
- `src/pages/[org]/[repo].vue` -> `/:org/:repo`
|
|
69
|
-
|
|
70
|
-
#### 虚拟目录
|
|
71
|
-
|
|
72
|
-
- 文件名带括号的路径会被当做虚拟目录,生成的路由path会忽略带括号的这一层路径
|
|
73
|
-
- 例如:`src/pages/(auth)/login.vue` -> `/login`
|
|
74
|
-
|
|
75
|
-
### 🧠 自定义路由
|
|
76
|
-
|
|
77
|
-
您可以在Vue文件中使用`defineOptions`来自定义路由配置:
|
|
78
|
-
|
|
79
|
-
```vue
|
|
80
|
-
<script setup>
|
|
81
|
-
defineOptions({
|
|
82
|
-
name: 'CustomRouteName',
|
|
83
|
-
meta: {
|
|
84
|
-
title: '页面标题',
|
|
85
|
-
icon: 'home',
|
|
86
|
-
requiresAuth: true,
|
|
87
|
-
enabled: true // 设置为false则不会生成此路由
|
|
88
|
-
},
|
|
89
|
-
redirect: '/other-route' // 设置重定向
|
|
90
|
-
})
|
|
91
|
-
</script>
|
|
92
|
-
```
|
|
93
|
-
|
|
94
|
-
### 🌲 嵌套路由
|
|
95
|
-
|
|
96
|
-
启用`nested: true`选项后,可以通过`parent`属性设置嵌套路由关系:
|
|
97
|
-
|
|
98
|
-
```vue
|
|
99
|
-
<script setup>
|
|
100
|
-
defineOptions({
|
|
101
|
-
name: 'ChildRoute',
|
|
102
|
-
parent: 'ParentRouteName',
|
|
103
|
-
meta: {
|
|
104
|
-
title: '子路由'
|
|
105
|
-
}
|
|
106
|
-
})
|
|
107
|
-
</script>
|
|
108
|
-
```
|
|
109
|
-
|
|
110
|
-
### 🚀 完整示例
|
|
111
|
-
|
|
112
|
-
```javascript
|
|
113
|
-
import vue from '@vitejs/plugin-vue'
|
|
114
|
-
import { defineConfig } from 'vite'
|
|
115
|
-
import generoutes from 'vite-plugin-generoutes'
|
|
116
|
-
|
|
117
|
-
export default defineConfig({
|
|
118
|
-
plugins: [
|
|
119
|
-
vue(),
|
|
120
|
-
generoutes({
|
|
121
|
-
pagesFolder: 'src/views',
|
|
122
|
-
ignoreFolders: ['components', 'assets'],
|
|
123
|
-
routesPath: 'src/router/routes.js',
|
|
124
|
-
nested: true
|
|
125
|
-
})
|
|
126
|
-
]
|
|
127
|
-
})
|
|
128
|
-
```
|
|
129
|
-
|
|
130
|
-
[English Documentation](./README.md)
|
|
1
|
+
# vite-plugin-generoutes
|
|
2
|
+
|
|
3
|
+
一个基于文件系统自动生成Vue路由配置的Vite插件。
|
|
4
|
+
|
|
5
|
+
### ✨ 特性
|
|
6
|
+
|
|
7
|
+
- 📁 基于文件系统的路由生成
|
|
8
|
+
- 🔄 支持动态路由和嵌套路由
|
|
9
|
+
- 🛠️ 热重载 - 文件变化时自动更新路由
|
|
10
|
+
- 🎨 可自定义路由配置
|
|
11
|
+
- 🧩 支持通过`defineOptions`设置路由元数据
|
|
12
|
+
- 🚦 支持路由重定向
|
|
13
|
+
|
|
14
|
+
### 📦 安装
|
|
15
|
+
|
|
16
|
+
```bash
|
|
17
|
+
# npm
|
|
18
|
+
npm install vite-plugin-generoutes -D
|
|
19
|
+
|
|
20
|
+
# yarn
|
|
21
|
+
yarn add vite-plugin-generoutes -D
|
|
22
|
+
|
|
23
|
+
# pnpm
|
|
24
|
+
pnpm add vite-plugin-generoutes -D
|
|
25
|
+
```
|
|
26
|
+
|
|
27
|
+
### 🔨 使用方法
|
|
28
|
+
|
|
29
|
+
在`vite.config.js`中配置插件:
|
|
30
|
+
|
|
31
|
+
```javascript
|
|
32
|
+
import vue from '@vitejs/plugin-vue'
|
|
33
|
+
import { defineConfig } from 'vite'
|
|
34
|
+
import generoutes from 'vite-plugin-generoutes'
|
|
35
|
+
|
|
36
|
+
export default defineConfig({
|
|
37
|
+
plugins: [
|
|
38
|
+
vue(),
|
|
39
|
+
generoutes({
|
|
40
|
+
// 配置选项
|
|
41
|
+
})
|
|
42
|
+
]
|
|
43
|
+
})
|
|
44
|
+
```
|
|
45
|
+
|
|
46
|
+
### ⚙️ 配置选项
|
|
47
|
+
|
|
48
|
+
| 选项 | 类型 | 默认值 | 描述 |
|
|
49
|
+
| --------------- | ---------- | ------------------------ | ------------------------------------- |
|
|
50
|
+
| `pagesFolder` | `string` | `'src/pages'` | 页面文件夹路径 |
|
|
51
|
+
| `ignoreFolders` | `string[]` | `['components']` | 生成路由时忽略的文件夹 |
|
|
52
|
+
| `routesPath` | `string` | `'src/router/routes.js'` | 生成的路由文件路径,也可以是`.ts`文件 |
|
|
53
|
+
| `nested` | `boolean` | `false` | 是否生成嵌套路由 |
|
|
54
|
+
|
|
55
|
+
### 📝 路由规则
|
|
56
|
+
|
|
57
|
+
#### 基本路由
|
|
58
|
+
|
|
59
|
+
- `src/pages/index.vue` -> `/`
|
|
60
|
+
- `src/pages/about.vue` -> `/about`
|
|
61
|
+
- `src/pages/users/index.vue` -> `/users`
|
|
62
|
+
- `src/pages/users/profile.vue` -> `/users/profile`
|
|
63
|
+
|
|
64
|
+
#### 动态路由
|
|
65
|
+
|
|
66
|
+
- `src/pages/users/[id].vue` -> `/users/:id`
|
|
67
|
+
- `src/pages/users/[...all].vue` -> `/users/:pathMatch(.*)*`
|
|
68
|
+
- `src/pages/[org]/[repo].vue` -> `/:org/:repo`
|
|
69
|
+
|
|
70
|
+
#### 虚拟目录
|
|
71
|
+
|
|
72
|
+
- 文件名带括号的路径会被当做虚拟目录,生成的路由path会忽略带括号的这一层路径
|
|
73
|
+
- 例如:`src/pages/(auth)/login.vue` -> `/login`
|
|
74
|
+
|
|
75
|
+
### 🧠 自定义路由
|
|
76
|
+
|
|
77
|
+
您可以在Vue文件中使用`defineOptions`来自定义路由配置:
|
|
78
|
+
|
|
79
|
+
```vue
|
|
80
|
+
<script setup>
|
|
81
|
+
defineOptions({
|
|
82
|
+
name: 'CustomRouteName',
|
|
83
|
+
meta: {
|
|
84
|
+
title: '页面标题',
|
|
85
|
+
icon: 'home',
|
|
86
|
+
requiresAuth: true,
|
|
87
|
+
enabled: true // 设置为false则不会生成此路由
|
|
88
|
+
},
|
|
89
|
+
redirect: '/other-route' // 设置重定向
|
|
90
|
+
})
|
|
91
|
+
</script>
|
|
92
|
+
```
|
|
93
|
+
|
|
94
|
+
### 🌲 嵌套路由
|
|
95
|
+
|
|
96
|
+
启用`nested: true`选项后,可以通过`parent`属性设置嵌套路由关系:
|
|
97
|
+
|
|
98
|
+
```vue
|
|
99
|
+
<script setup>
|
|
100
|
+
defineOptions({
|
|
101
|
+
name: 'ChildRoute',
|
|
102
|
+
parent: 'ParentRouteName',
|
|
103
|
+
meta: {
|
|
104
|
+
title: '子路由'
|
|
105
|
+
}
|
|
106
|
+
})
|
|
107
|
+
</script>
|
|
108
|
+
```
|
|
109
|
+
|
|
110
|
+
### 🚀 完整示例
|
|
111
|
+
|
|
112
|
+
```javascript
|
|
113
|
+
import vue from '@vitejs/plugin-vue'
|
|
114
|
+
import { defineConfig } from 'vite'
|
|
115
|
+
import generoutes from 'vite-plugin-generoutes'
|
|
116
|
+
|
|
117
|
+
export default defineConfig({
|
|
118
|
+
plugins: [
|
|
119
|
+
vue(),
|
|
120
|
+
generoutes({
|
|
121
|
+
pagesFolder: 'src/views',
|
|
122
|
+
ignoreFolders: ['components', 'assets'],
|
|
123
|
+
routesPath: 'src/router/routes.js',
|
|
124
|
+
nested: true
|
|
125
|
+
})
|
|
126
|
+
]
|
|
127
|
+
})
|
|
128
|
+
```
|
|
129
|
+
|
|
130
|
+
[English Documentation](./README.md)
|
package/dist/index.d.ts
CHANGED
|
@@ -1,5 +1,40 @@
|
|
|
1
1
|
import { Plugin } from 'vite';
|
|
2
2
|
|
|
3
|
+
/**
|
|
4
|
+
* Route meta information
|
|
5
|
+
*/
|
|
6
|
+
interface RouteMeta {
|
|
7
|
+
/** Route title */
|
|
8
|
+
title?: string;
|
|
9
|
+
/** Route icon */
|
|
10
|
+
icon?: string;
|
|
11
|
+
/** Whether authentication is required */
|
|
12
|
+
requiresAuth?: boolean;
|
|
13
|
+
/** Whether the route is enabled */
|
|
14
|
+
enabled?: boolean;
|
|
15
|
+
/** Custom properties */
|
|
16
|
+
[key: string]: unknown;
|
|
17
|
+
}
|
|
18
|
+
/**
|
|
19
|
+
* Internal route representation used during generation
|
|
20
|
+
*/
|
|
21
|
+
interface InternalRoute {
|
|
22
|
+
/** Route name */
|
|
23
|
+
name: string;
|
|
24
|
+
/** Route path */
|
|
25
|
+
path: string;
|
|
26
|
+
/** Redirect path */
|
|
27
|
+
redirect?: string;
|
|
28
|
+
/** Route component placeholder */
|
|
29
|
+
component?: string;
|
|
30
|
+
/** Route meta information */
|
|
31
|
+
meta: RouteMeta;
|
|
32
|
+
/** Parent route name (for nested routes) */
|
|
33
|
+
parent?: string;
|
|
34
|
+
/** Child routes */
|
|
35
|
+
children?: InternalRoute[];
|
|
36
|
+
}
|
|
37
|
+
|
|
3
38
|
/**********************************
|
|
4
39
|
* @Author: Ronnie Zhang
|
|
5
40
|
* @LastEditor: Ronnie Zhang
|
|
@@ -8,6 +43,25 @@ import { Plugin } from 'vite';
|
|
|
8
43
|
* Copyright © 2023 Ronnie Zhang(大脸怪) | https://isme.top
|
|
9
44
|
**********************************/
|
|
10
45
|
|
|
46
|
+
/**
|
|
47
|
+
* Generated route record with proper typing for vue-router
|
|
48
|
+
*/
|
|
49
|
+
interface GeneratedRoute {
|
|
50
|
+
/** Route name */
|
|
51
|
+
name: string;
|
|
52
|
+
/** Route path */
|
|
53
|
+
path: string;
|
|
54
|
+
/** Redirect path */
|
|
55
|
+
redirect?: string;
|
|
56
|
+
/** Route component */
|
|
57
|
+
component?: () => Promise<any>;
|
|
58
|
+
/** Route meta information */
|
|
59
|
+
meta: RouteMeta;
|
|
60
|
+
/** Parent route name (for nested routes) */
|
|
61
|
+
parent?: string;
|
|
62
|
+
/** Child routes */
|
|
63
|
+
children?: GeneratedRoute[];
|
|
64
|
+
}
|
|
11
65
|
interface Options {
|
|
12
66
|
/**
|
|
13
67
|
* pages folder
|
|
@@ -23,8 +77,9 @@ interface Options {
|
|
|
23
77
|
ignoreFolders: string[];
|
|
24
78
|
/**
|
|
25
79
|
* routes file path, It can also be a ts file,such as `src/router/routes.ts`
|
|
80
|
+
* Auto-detected based on tsconfig.json presence if not specified
|
|
26
81
|
*
|
|
27
|
-
* @default src/router/routes.js
|
|
82
|
+
* @default src/router/routes.ts (if tsconfig.json exists) or src/router/routes.js
|
|
28
83
|
*/
|
|
29
84
|
routesPath: string;
|
|
30
85
|
/**
|
|
@@ -36,4 +91,4 @@ interface Options {
|
|
|
36
91
|
}
|
|
37
92
|
declare function VitePluginGeneroutes(options?: Partial<Options>): Plugin;
|
|
38
93
|
|
|
39
|
-
export { VitePluginGeneroutes as default };
|
|
94
|
+
export { type GeneratedRoute, type InternalRoute, type Options, type RouteMeta, VitePluginGeneroutes, VitePluginGeneroutes as default };
|
package/dist/index.js
CHANGED
|
@@ -61,13 +61,18 @@ function findDuplicateRoutes(routes) {
|
|
|
61
61
|
// src/index.ts
|
|
62
62
|
function VitePluginGeneroutes(options = {}) {
|
|
63
63
|
if (options.routesPath && ![".ts", ".js"].includes(path.extname(options.routesPath)))
|
|
64
|
-
throw new Error("routesPath must be a js or ts file path, such as src/router/
|
|
64
|
+
throw new Error("routesPath must be a js or ts file path, such as src/router/routes.js");
|
|
65
65
|
let rootDir;
|
|
66
|
+
let routesPath;
|
|
67
|
+
let isTypeScript;
|
|
66
68
|
const pagesFolder = options.pagesFolder || "src/pages";
|
|
67
|
-
const routesPath = options.routesPath || "src/router/routes.js";
|
|
68
69
|
const ignoreFolders = options.ignoreFolders || ["components"];
|
|
69
70
|
const nested = options.nested || false;
|
|
70
71
|
const defineOptionsCache = /* @__PURE__ */ new Map();
|
|
72
|
+
function detectTypeScriptProject() {
|
|
73
|
+
const tsconfigPath = path.resolve(rootDir, "tsconfig.json");
|
|
74
|
+
return fs.existsSync(tsconfigPath);
|
|
75
|
+
}
|
|
71
76
|
function generateMenusAndRoutes() {
|
|
72
77
|
const pages = globSync(`${pagesFolder}/**/*.vue`, { ignore: ignoreFolders.map((folder) => `${pagesFolder}/**/${folder}/**`) });
|
|
73
78
|
const routes = pages.map((filePath) => {
|
|
@@ -89,7 +94,7 @@ function VitePluginGeneroutes(options = {}) {
|
|
|
89
94
|
meta,
|
|
90
95
|
parent: defineOptions.parent
|
|
91
96
|
};
|
|
92
|
-
}).filter(
|
|
97
|
+
}).filter((route) => route !== null);
|
|
93
98
|
const { duplicateNames, duplicatePaths } = findDuplicateRoutes(routes);
|
|
94
99
|
if (duplicateNames.length)
|
|
95
100
|
console.warn(`Warning: Duplicate names found in routes: ${duplicateNames.join(", ")}`);
|
|
@@ -101,13 +106,40 @@ function VitePluginGeneroutes(options = {}) {
|
|
|
101
106
|
}
|
|
102
107
|
async function writerRoutesFile(isInit = false) {
|
|
103
108
|
const { routes } = generateMenusAndRoutes();
|
|
109
|
+
const typeDefinitions = isTypeScript ? `
|
|
110
|
+
import type { RouteRecordRaw } from 'vue-router'
|
|
111
|
+
|
|
112
|
+
/**
|
|
113
|
+
* Route meta information
|
|
114
|
+
*/
|
|
115
|
+
export interface RouteMeta {
|
|
116
|
+
/** Route title */
|
|
117
|
+
title?: string
|
|
118
|
+
/** Route icon */
|
|
119
|
+
icon?: string
|
|
120
|
+
/** Whether authentication is required */
|
|
121
|
+
requiresAuth?: boolean
|
|
122
|
+
/** Custom properties */
|
|
123
|
+
[key: string]: unknown
|
|
124
|
+
}
|
|
125
|
+
|
|
126
|
+
/**
|
|
127
|
+
* Generated route type with proper typing
|
|
128
|
+
*/
|
|
129
|
+
export type GeneratedRoute = RouteRecordRaw & {
|
|
130
|
+
meta?: RouteMeta
|
|
131
|
+
children?: GeneratedRoute[]
|
|
132
|
+
}
|
|
133
|
+
` : "";
|
|
134
|
+
const routesType = isTypeScript ? ": GeneratedRoute[]" : "";
|
|
104
135
|
let routesStr = `
|
|
105
136
|
// Generated by vite-plugin-generoutes, don't modify it directly.
|
|
106
|
-
|
|
107
|
-
export const routes = ${JSON.stringify(routes, null, 2)}
|
|
137
|
+
${typeDefinitions}
|
|
138
|
+
export const routes${routesType} = ${JSON.stringify(routes, null, 2)}
|
|
108
139
|
`;
|
|
109
140
|
routesStr = routesStr.replace(/"##(.*)##"/g, (_, p1) => `async () => ({...(await import('${p1.split("@@")[0]}')).default, name: '${p1.split("@@")[1]}'})`);
|
|
110
|
-
|
|
141
|
+
const parser = isTypeScript ? "typescript" : "babel";
|
|
142
|
+
routesStr = await prettier.format(routesStr, { parser, semi: false, singleQuote: true });
|
|
111
143
|
const filePath = path.resolve(rootDir, routesPath);
|
|
112
144
|
await fs.ensureDir(path.dirname(filePath));
|
|
113
145
|
fs.writeFileSync(filePath, routesStr);
|
|
@@ -118,6 +150,9 @@ function VitePluginGeneroutes(options = {}) {
|
|
|
118
150
|
name: "vite-plugin-generoutes",
|
|
119
151
|
async configResolved(config) {
|
|
120
152
|
rootDir = config.root;
|
|
153
|
+
const isTs = detectTypeScriptProject();
|
|
154
|
+
routesPath = options.routesPath || (isTs ? "src/router/routes.ts" : "src/router/routes.js");
|
|
155
|
+
isTypeScript = routesPath.endsWith(".ts");
|
|
121
156
|
await writerRoutesFile(true);
|
|
122
157
|
},
|
|
123
158
|
configureServer(server) {
|
|
@@ -162,5 +197,6 @@ function parseDefineOptions(filePath, content) {
|
|
|
162
197
|
}
|
|
163
198
|
var index_default = VitePluginGeneroutes;
|
|
164
199
|
export {
|
|
200
|
+
VitePluginGeneroutes,
|
|
165
201
|
index_default as default
|
|
166
202
|
};
|
package/package.json
CHANGED
|
@@ -1,82 +1,89 @@
|
|
|
1
|
-
{
|
|
2
|
-
"name": "vite-plugin-generoutes",
|
|
3
|
-
"type": "module",
|
|
4
|
-
"version": "1.0.
|
|
5
|
-
"packageManager": "pnpm@10.6.2",
|
|
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
|
-
"author": "Ronnie Zhang <zclzone@outlook.com>",
|
|
8
|
-
"license": "MIT",
|
|
9
|
-
"homepage": "https://github.com/zclzone/vite-plugin-generoutes",
|
|
10
|
-
"repository": {
|
|
11
|
-
"type": "git",
|
|
12
|
-
"url": "git+https://github.com/zclzone/vite-plugin-generoutes.git"
|
|
13
|
-
},
|
|
14
|
-
"bugs": "https://github.com/zclzone/vite-plugin-generoutes/issues",
|
|
15
|
-
"keywords": [],
|
|
16
|
-
"sideEffects": false,
|
|
17
|
-
"exports": {
|
|
18
|
-
".": {
|
|
19
|
-
"types": "./dist/index.d.ts",
|
|
20
|
-
"import": "./dist/index.js"
|
|
21
|
-
}
|
|
22
|
-
},
|
|
23
|
-
"main": "./dist/index.js",
|
|
24
|
-
"module": "./dist/index.js",
|
|
25
|
-
"types": "./dist/index.d.ts",
|
|
26
|
-
"typesVersions": {
|
|
27
|
-
"*": {
|
|
28
|
-
"*": [
|
|
29
|
-
"./dist/*",
|
|
30
|
-
"./dist/index.d.ts"
|
|
31
|
-
]
|
|
32
|
-
}
|
|
33
|
-
},
|
|
34
|
-
"files": [
|
|
35
|
-
"dist"
|
|
36
|
-
],
|
|
37
|
-
"scripts": {
|
|
38
|
-
"start": "tsup --watch",
|
|
39
|
-
"build": "tsup",
|
|
40
|
-
"lint": "eslint .",
|
|
41
|
-
"prepublishOnly": "nr build",
|
|
42
|
-
"release": "bumpp && npm publish",
|
|
43
|
-
"test": "vitest",
|
|
44
|
-
"typecheck": "tsc --noEmit",
|
|
45
|
-
"prepare": "simple-git-hooks",
|
|
46
|
-
"up:deps": "taze major -I"
|
|
47
|
-
},
|
|
48
|
-
"peerDependencies": {
|
|
49
|
-
"vite": "^6.1.0"
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
"
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
"
|
|
61
|
-
"
|
|
62
|
-
"
|
|
63
|
-
"
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
"
|
|
67
|
-
"
|
|
68
|
-
"
|
|
69
|
-
"
|
|
70
|
-
"
|
|
71
|
-
"
|
|
72
|
-
"
|
|
73
|
-
"
|
|
74
|
-
"
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
"
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
"
|
|
81
|
-
|
|
82
|
-
}
|
|
1
|
+
{
|
|
2
|
+
"name": "vite-plugin-generoutes",
|
|
3
|
+
"type": "module",
|
|
4
|
+
"version": "1.1.0-beta.2",
|
|
5
|
+
"packageManager": "pnpm@10.6.2",
|
|
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
|
+
"author": "Ronnie Zhang <zclzone@outlook.com>",
|
|
8
|
+
"license": "MIT",
|
|
9
|
+
"homepage": "https://github.com/zclzone/vite-plugin-generoutes",
|
|
10
|
+
"repository": {
|
|
11
|
+
"type": "git",
|
|
12
|
+
"url": "git+https://github.com/zclzone/vite-plugin-generoutes.git"
|
|
13
|
+
},
|
|
14
|
+
"bugs": "https://github.com/zclzone/vite-plugin-generoutes/issues",
|
|
15
|
+
"keywords": [],
|
|
16
|
+
"sideEffects": false,
|
|
17
|
+
"exports": {
|
|
18
|
+
".": {
|
|
19
|
+
"types": "./dist/index.d.ts",
|
|
20
|
+
"import": "./dist/index.js"
|
|
21
|
+
}
|
|
22
|
+
},
|
|
23
|
+
"main": "./dist/index.js",
|
|
24
|
+
"module": "./dist/index.js",
|
|
25
|
+
"types": "./dist/index.d.ts",
|
|
26
|
+
"typesVersions": {
|
|
27
|
+
"*": {
|
|
28
|
+
"*": [
|
|
29
|
+
"./dist/*",
|
|
30
|
+
"./dist/index.d.ts"
|
|
31
|
+
]
|
|
32
|
+
}
|
|
33
|
+
},
|
|
34
|
+
"files": [
|
|
35
|
+
"dist"
|
|
36
|
+
],
|
|
37
|
+
"scripts": {
|
|
38
|
+
"start": "tsup --watch",
|
|
39
|
+
"build": "tsup",
|
|
40
|
+
"lint": "eslint .",
|
|
41
|
+
"prepublishOnly": "nr build",
|
|
42
|
+
"release": "bumpp && npm publish",
|
|
43
|
+
"test": "vitest",
|
|
44
|
+
"typecheck": "tsc --noEmit",
|
|
45
|
+
"prepare": "simple-git-hooks",
|
|
46
|
+
"up:deps": "taze major -I"
|
|
47
|
+
},
|
|
48
|
+
"peerDependencies": {
|
|
49
|
+
"vite": "^6.1.0",
|
|
50
|
+
"vue-router": "^4.0.0"
|
|
51
|
+
},
|
|
52
|
+
"peerDependenciesMeta": {
|
|
53
|
+
"vue-router": {
|
|
54
|
+
"optional": true
|
|
55
|
+
}
|
|
56
|
+
},
|
|
57
|
+
"dependencies": {
|
|
58
|
+
"@antfu/utils": "^9.1.0",
|
|
59
|
+
"@vue/compiler-sfc": "^3.5.13",
|
|
60
|
+
"chalk": "^5.4.1",
|
|
61
|
+
"fs-extra": "^11.3.0",
|
|
62
|
+
"glob": "^11.0.1",
|
|
63
|
+
"prettier": "^3.5.3"
|
|
64
|
+
},
|
|
65
|
+
"devDependencies": {
|
|
66
|
+
"@antfu/eslint-config": "^4.10.1",
|
|
67
|
+
"@types/fs-extra": "^11.0.4",
|
|
68
|
+
"@types/node": "^20.17.24",
|
|
69
|
+
"bumpp": "^10.1.0",
|
|
70
|
+
"eslint": "^9.22.0",
|
|
71
|
+
"esno": "^4.8.0",
|
|
72
|
+
"lint-staged": "^15.5.0",
|
|
73
|
+
"pnpm": "^10.6.2",
|
|
74
|
+
"rimraf": "^5.0.10",
|
|
75
|
+
"simple-git-hooks": "^2.11.1",
|
|
76
|
+
"taze": "^19.0.2",
|
|
77
|
+
"tsup": "^8.4.0",
|
|
78
|
+
"typescript": "^5.8.2",
|
|
79
|
+
"unbuild": "^3.5.0",
|
|
80
|
+
"vitest": "^3.0.8",
|
|
81
|
+
"vue-router": "^4.5.0"
|
|
82
|
+
},
|
|
83
|
+
"simple-git-hooks": {
|
|
84
|
+
"pre-commit": "pnpm lint-staged"
|
|
85
|
+
},
|
|
86
|
+
"lint-staged": {
|
|
87
|
+
"*": "eslint --fix"
|
|
88
|
+
}
|
|
89
|
+
}
|