@onoxm/vite-plugin-auto-router 0.2.1 → 0.3.0

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 ADDED
@@ -0,0 +1,432 @@
1
+ # @onoxm/vite-plugin-auto-router
2
+
3
+ A Vite plugin for automatically generating React or Vue route files.
4
+
5
+ English | [中文](./README.zh-CN.md)
6
+
7
+ ## ✨ Features
8
+
9
+ - Auto-generate route configuration, no manual maintenance needed
10
+ - Supports both React and Vue frameworks
11
+ - Convention-based routing, automatically mapped by directory structure
12
+ - Supports dynamic route `[id]` syntax
13
+ - Configurable lazy loading and hot module replacement
14
+ - Supports page-level configuration files
15
+ - TypeScript type safety
16
+
17
+ ## 🚀 Installation
18
+
19
+ ```bash
20
+ npm install -D @onoxm/vite-plugin-auto-router
21
+ ```
22
+
23
+ ## 📖 Usage Guide
24
+
25
+ ### React Project
26
+
27
+ #### Install Dependencies
28
+
29
+ ```bash
30
+ npm install react-router
31
+ ```
32
+
33
+ #### Configure Vite
34
+
35
+ ```typescript
36
+ // vite.config.ts
37
+ import { defineConfig } from 'vite'
38
+ import react from '@vitejs/plugin-react'
39
+ import autoRouter from '@onoxm/vite-plugin-auto-router'
40
+
41
+ export default defineConfig({
42
+ plugins: [react(), autoRouter()]
43
+ })
44
+ ```
45
+
46
+ #### Directory Structure
47
+
48
+ ```
49
+ src/
50
+ ├── pages/
51
+ │ ├── index.tsx
52
+ │ ├── root.tsx
53
+ │ ├── 404.tsx
54
+ │ └── user/
55
+ │ ├── index.tsx
56
+ │ ├── index.config.ts
57
+ │ ├── [id].tsx
58
+ │ └── [id].config.ts
59
+ ```
60
+
61
+ #### Special Pages
62
+
63
+ - **`home` page**: Path automatically converted to `/`, used as home route
64
+ - **`root` page**: Used as root route container, wrapping all other routes
65
+ - **`404` or `notfound` page**: Path automatically converted to `/*`, used as 404 route
66
+
67
+ #### Page Configuration
68
+
69
+ Inherits from [React Router RouteObject](https://reactrouter.com/start/data/route-object), with the following modifications:
70
+
71
+ - **Removed**: `path`, `Component`, `element`, `children`
72
+ - **Added**: `type?: 'single' | 'wrap'`
73
+
74
+ ##### type: 'single'
75
+
76
+ When `type` is set to `single`, the page component will be generated as an independent route:
77
+
78
+ ```typescript
79
+ // src/pages/user/index.config.ts
80
+ import { defineConfig } from '../../router/autoRouter'
81
+
82
+ export default defineConfig({
83
+ type: 'single'
84
+ })
85
+ ```
86
+
87
+ Generated route structure:
88
+
89
+ ```typescript
90
+ // src/router/autoRouter.tsx
91
+ import type { RouteObject } from 'react-router'
92
+ import Pages404 from './../pages/404.tsx'
93
+ import Pages from './../pages/index.tsx'
94
+ import PagesRoot from './../pages/root.tsx'
95
+ import PagesUser from './../pages/user/index.tsx'
96
+ import PagesUserId from './../pages/user/[id]/index.tsx'
97
+
98
+ type PageConfig = Partial<
99
+ Omit<RouteObject, 'path' | 'Component' | 'element' | 'children'> & {
100
+ type?: 'single' | 'wrap'
101
+ }
102
+ >
103
+
104
+ export const defineConfig = (config: PageConfig) => config
105
+
106
+ export const routes: RouteObject[] = [
107
+ {
108
+ path: '/',
109
+ element: <PagesRoot />,
110
+ children: [
111
+ {
112
+ path: '/',
113
+ element: <Pages />
114
+ },
115
+ {
116
+ path: '/user',
117
+ children: [
118
+ {
119
+ path: '',
120
+ index: true,
121
+ element: <PagesUser />
122
+ },
123
+ {
124
+ path: ':id',
125
+ children: [
126
+ {
127
+ path: '',
128
+ index: true,
129
+ action: async () => {},
130
+ loader: async ({ params }) => await { params },
131
+ element: <PagesUserId />
132
+ }
133
+ ]
134
+ }
135
+ ]
136
+ }
137
+ ]
138
+ },
139
+ {
140
+ path: '/*',
141
+ element: <Pages404 />
142
+ }
143
+ ]
144
+ ```
145
+
146
+ ##### type: 'wrap'
147
+
148
+ When `type` is set to `wrap`, the page component will act as a parent route container wrapping its child routes:
149
+
150
+ ```typescript
151
+ // src/pages/user/index.config.ts
152
+ import { defineConfig } from '../../router/autoRouter'
153
+
154
+ export default defineConfig({
155
+ type: 'wrap'
156
+ })
157
+ ```
158
+
159
+ Generated route structure:
160
+
161
+ ```typescript
162
+ // src/router/autoRouter.tsx
163
+ import type { RouteObject } from 'react-router'
164
+ import Pages404 from './../pages/404.tsx'
165
+ import Pages from './../pages/index.tsx'
166
+ import PagesRoot from './../pages/root.tsx'
167
+ import PagesUser from './../pages/user/index.tsx'
168
+ import PagesUserId from './../pages/user/[id]/index.tsx'
169
+
170
+ type PageConfig = Partial<
171
+ Omit<RouteObject, 'path' | 'Component' | 'element' | 'children'> & {
172
+ type?: 'single' | 'wrap'
173
+ }
174
+ >
175
+
176
+ export const defineConfig = (config: PageConfig) => config
177
+
178
+ export const routes: RouteObject[] = [
179
+ {
180
+ path: '/',
181
+ element: <PagesRoot />,
182
+ children: [
183
+ {
184
+ path: '/',
185
+ element: <Pages />
186
+ },
187
+ {
188
+ path: '/user',
189
+ element: <PagesUser />,
190
+ children: [
191
+ {
192
+ path: ':id',
193
+ children: [
194
+ {
195
+ path: '',
196
+ index: true,
197
+ action: async () => {},
198
+ loader: async ({ params }) => await { params },
199
+ element: <PagesUserId />
200
+ }
201
+ ]
202
+ }
203
+ ]
204
+ }
205
+ ]
206
+ },
207
+ {
208
+ path: '/*',
209
+ element: <Pages404 />
210
+ }
211
+ ]
212
+ ```
213
+
214
+ ### Vue Project
215
+
216
+ #### Install Dependencies
217
+
218
+ ```bash
219
+ npm install vue-router
220
+ ```
221
+
222
+ #### Configure Vite
223
+
224
+ ```typescript
225
+ // vite.config.ts
226
+ import { defineConfig } from 'vite'
227
+ import vue from '@vitejs/plugin-vue'
228
+ import autoRouter from '@onoxm/vite-plugin-auto-router'
229
+
230
+ export default defineConfig({
231
+ plugins: [
232
+ vue(),
233
+ autoRouter({
234
+ framework: 'vue',
235
+ pagesDir: './src/views'
236
+ })
237
+ ]
238
+ })
239
+ ```
240
+
241
+ #### Directory Structure
242
+
243
+ ```
244
+ src/
245
+ ├── views/
246
+ │ ├── 404.vue
247
+ │ ├── home/
248
+ │ │ ├── index.vue
249
+ │ │ └── index.config.ts
250
+ │ └── user/
251
+ │ ├── index.vue
252
+ │ ├── index.config.ts
253
+ │ ├── [id].vue
254
+ │ └── [id].config.ts
255
+ ```
256
+
257
+ #### Special Pages
258
+
259
+ - **`home` page**: Path automatically converted to `/`, used as home route
260
+ - **`root` page**: Used as root route container, wrapping all other routes
261
+ - **`404` or `notfound` page**: Path automatically converted to `/:pathMatch(.*)*`, used as 404 route
262
+
263
+ #### Page Configuration
264
+
265
+ Inherits from [Vue Router RouteRecordRaw](https://router.vuejs.org/api/#routerecordraw), with the following modifications:
266
+
267
+ - **Removed**: `path`, `component`, `children`
268
+ - **Added**: `type?: 'single' | 'wrap'`
269
+
270
+ ##### type: 'single'
271
+
272
+ When `type` is set to `single`, the page component will be generated as an independent route:
273
+
274
+ ```typescript
275
+ // src/views/user/index.config.ts
276
+ import { defineConfig } from '../../router/autoRouter'
277
+
278
+ export default defineConfig({
279
+ type: 'single'
280
+ })
281
+ ```
282
+
283
+ Generated route structure:
284
+
285
+ ```typescript
286
+ // src/router/autoRouter.ts
287
+ import type { RouteRecordRaw } from 'vue-router'
288
+ import Views404 from './../views/404.vue'
289
+ import ViewsHome from './../views/home/index.vue'
290
+ import ViewsUser from './../views/user/index.vue'
291
+ import ViewsUserId from './../views/user/[id]/index.vue'
292
+
293
+ type PageConfig = Partial<
294
+ Omit<RouteRecordRaw, 'path' | 'component' | 'children'> & {
295
+ type?: 'single' | 'wrap'
296
+ }
297
+ >
298
+
299
+ export const defineConfig = (config: PageConfig) => config
300
+
301
+ export const routes: RouteRecordRaw[] = [
302
+ {
303
+ path: '/',
304
+ children: [
305
+ {
306
+ path: '',
307
+ name: 'home',
308
+ component: ViewsHome
309
+ }
310
+ ]
311
+ },
312
+ {
313
+ path: '/user',
314
+ children: [
315
+ {
316
+ path: '',
317
+ component: ViewsUser
318
+ },
319
+ {
320
+ path: ':id',
321
+ children: [
322
+ {
323
+ path: '',
324
+ component: ViewsUserId
325
+ }
326
+ ]
327
+ }
328
+ ]
329
+ },
330
+ {
331
+ path: '/:pathMatch(.*)*',
332
+ children: [
333
+ {
334
+ path: '',
335
+ component: Views404
336
+ }
337
+ ]
338
+ }
339
+ ]
340
+ ```
341
+
342
+ ##### type: 'wrap'
343
+
344
+ When `type` is set to `wrap`, the page component will act as a parent route container wrapping its child routes:
345
+
346
+ ```typescript
347
+ // src/views/user/index.config.ts
348
+ import { defineConfig } from '../../router/autoRouter'
349
+
350
+ export default defineConfig({
351
+ type: 'wrap'
352
+ })
353
+ ```
354
+
355
+ Generated route structure:
356
+
357
+ ```typescript
358
+ // src/router/autoRouter.ts
359
+ import type { RouteRecordRaw } from 'vue-router'
360
+ import Views404 from './../views/404.vue'
361
+ import ViewsHome from './../views/home/index.vue'
362
+ import ViewsUser from './../views/user/index.vue'
363
+ import ViewsUserId from './../views/user/[id]/index.vue'
364
+
365
+ type PageConfig = Partial<
366
+ Omit<RouteRecordRaw, 'path' | 'component' | 'children'> & {
367
+ type?: 'single' | 'wrap'
368
+ }
369
+ >
370
+
371
+ export const defineConfig = (config: PageConfig) => config
372
+
373
+ export const routes: RouteRecordRaw[] = [
374
+ {
375
+ path: '/',
376
+ children: [
377
+ {
378
+ path: '',
379
+ name: 'home',
380
+ component: ViewsHome
381
+ }
382
+ ]
383
+ },
384
+ {
385
+ path: '/user',
386
+ component: ViewsUser,
387
+ children: [
388
+ {
389
+ path: ':id',
390
+ children: [
391
+ {
392
+ path: '',
393
+ component: ViewsUserId
394
+ }
395
+ ]
396
+ }
397
+ ]
398
+ },
399
+ {
400
+ path: '/:pathMatch(.*)*',
401
+ children: [
402
+ {
403
+ path: '',
404
+ component: Views404
405
+ }
406
+ ]
407
+ }
408
+ ]
409
+ ```
410
+
411
+ ## ⚙️ Configuration Options
412
+
413
+ ### Plugin Configuration
414
+
415
+ | Option | Type | Default | Description |
416
+ | ------------ | ------------------ | --------------- | ----------------------------------- |
417
+ | `framework` | `'react' \| 'vue'` | `'react'` | Framework type |
418
+ | `pagesDir` | `string` | `'./src/pages'` | Pages directory |
419
+ | `routesFile` | `string` | `undefined` | Generated route file path |
420
+ | `lazy` | `boolean` | `true` | Whether to enable lazy loading |
421
+ | `hmr` | `boolean` | `false` | Whether to enable hot module update |
422
+
423
+ ### Page Configuration
424
+
425
+ | Option | Type | Default | Description |
426
+ | ------ | -------------------- | ---------- | --------------------------- |
427
+ | `type` | `'single' \| 'wrap'` | `'single'` | Route type |
428
+ | `*` | `any` | `any` | Inherits from router config |
429
+
430
+ ## 📄 License
431
+
432
+ MIT