@onoxm/vite-plugin-auto-router 0.2.1 → 0.3.1
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 +436 -0
- package/README.zh-CN.md +435 -0
- package/dist/core-CEQjBvM3.js +1 -0
- package/dist/core-CHYsjsIG.js +1 -0
- package/dist/generate-react-routes-hmr.cjs +1 -1
- package/dist/generate-react-routes-hmr.d.ts +1 -1
- package/dist/generate-react-routes-hmr.js +1 -1
- package/dist/generate-react-routes.cjs +1 -1
- package/dist/generate-react-routes.d.ts +1 -1
- package/dist/generate-react-routes.js +1 -1
- package/dist/generate-vue-routes-hmr.cjs +1 -1
- package/dist/generate-vue-routes-hmr.d.ts +1 -1
- package/dist/generate-vue-routes-hmr.js +1 -1
- package/dist/generate-vue-routes.cjs +1 -1
- package/dist/generate-vue-routes.d.ts +1 -1
- package/dist/generate-vue-routes.js +1 -1
- package/dist/index.cjs +1 -1
- package/dist/index.d.ts +2 -0
- package/dist/index.js +1 -1
- package/dist/react.cjs +1 -1
- package/dist/react.d.ts +2 -0
- package/dist/react.js +1 -1
- package/dist/utils.cjs +1 -1
- package/dist/utils.d.ts +1 -1
- package/dist/utils.js +1 -1
- package/dist/vue.cjs +1 -1
- package/dist/vue.d.ts +2 -0
- package/dist/vue.js +1 -1
- package/package.json +7 -3
- package/dist/core-DJR8BtMK.js +0 -1
- package/dist/core-wPeKAJfw.js +0 -1
package/README.zh-CN.md
ADDED
|
@@ -0,0 +1,435 @@
|
|
|
1
|
+
# @onoxm/vite-plugin-auto-router
|
|
2
|
+
|
|
3
|
+
一个用于自动生成 React 或 Vue 路由文件的 Vite 插件。
|
|
4
|
+
|
|
5
|
+
[English](./README.md) | 中文
|
|
6
|
+
|
|
7
|
+
## ✨ 特性
|
|
8
|
+
|
|
9
|
+
- 自动生成路由配置,无需手动维护
|
|
10
|
+
- 支持 React 和 Vue 双框架
|
|
11
|
+
- 约定式路由,按目录结构自动映射
|
|
12
|
+
- 支持动态路由 `[id]` 语法
|
|
13
|
+
- `home` 页面路径自动转换为 `/`(可配置)
|
|
14
|
+
- `root` 页面作为根路由容器(可配置)
|
|
15
|
+
- 可配置懒加载和热更新
|
|
16
|
+
- 支持页面级配置文件
|
|
17
|
+
- TypeScript 类型安全
|
|
18
|
+
|
|
19
|
+
## 🚀 安装
|
|
20
|
+
|
|
21
|
+
```bash
|
|
22
|
+
npm install -D @onoxm/vite-plugin-auto-router
|
|
23
|
+
```
|
|
24
|
+
|
|
25
|
+
## 📖 使用指南
|
|
26
|
+
|
|
27
|
+
### React 项目
|
|
28
|
+
|
|
29
|
+
#### 安装依赖
|
|
30
|
+
|
|
31
|
+
```bash
|
|
32
|
+
npm install react-router
|
|
33
|
+
```
|
|
34
|
+
|
|
35
|
+
#### 配置 Vite
|
|
36
|
+
|
|
37
|
+
```typescript
|
|
38
|
+
// vite.config.ts
|
|
39
|
+
import { defineConfig } from 'vite'
|
|
40
|
+
import react from '@vitejs/plugin-react'
|
|
41
|
+
import autoRouter from '@onoxm/vite-plugin-auto-router'
|
|
42
|
+
|
|
43
|
+
export default defineConfig({
|
|
44
|
+
plugins: [react(), autoRouter()]
|
|
45
|
+
})
|
|
46
|
+
```
|
|
47
|
+
|
|
48
|
+
#### 目录结构
|
|
49
|
+
|
|
50
|
+
```
|
|
51
|
+
src/
|
|
52
|
+
├── pages/
|
|
53
|
+
│ ├── index.tsx
|
|
54
|
+
│ ├── root.tsx
|
|
55
|
+
│ ├── 404.tsx
|
|
56
|
+
│ └── user/
|
|
57
|
+
│ ├── index.tsx
|
|
58
|
+
│ ├── index.config.ts
|
|
59
|
+
│ ├── [id].tsx
|
|
60
|
+
│ └── [id].config.ts
|
|
61
|
+
```
|
|
62
|
+
|
|
63
|
+
#### 特殊页面
|
|
64
|
+
|
|
65
|
+
- **`home` 页面**:路径会自动转换为 `/`,作为首页路由
|
|
66
|
+
- **`root` 页面**:作为根路由容器,包裹所有其他路由
|
|
67
|
+
- **`404` 或 `notfound` 页面**:路径会自动转换为 `/*`,作为 404 路由
|
|
68
|
+
|
|
69
|
+
#### 页面配置
|
|
70
|
+
|
|
71
|
+
继承自 [React Router RouteObject](https://reactrouter.com/start/data/route-object),并进行以下修改:
|
|
72
|
+
|
|
73
|
+
- **移除**: `path`, `Component`, `element`, `children`
|
|
74
|
+
- **新增**: `type?: 'single' | 'wrap'`
|
|
75
|
+
|
|
76
|
+
##### type: 'single'
|
|
77
|
+
|
|
78
|
+
当 `type` 配置为 `single` 时,该页面组件会作为独立路由生成:
|
|
79
|
+
|
|
80
|
+
```typescript
|
|
81
|
+
// src/pages/user/index.config.ts
|
|
82
|
+
import { defineConfig } from '../../router/autoRouter'
|
|
83
|
+
|
|
84
|
+
export default defineConfig({
|
|
85
|
+
type: 'single'
|
|
86
|
+
})
|
|
87
|
+
```
|
|
88
|
+
|
|
89
|
+
生成的路由结构:
|
|
90
|
+
|
|
91
|
+
```typescript
|
|
92
|
+
// src/router/autoRouter.tsx
|
|
93
|
+
import type { RouteObject } from 'react-router'
|
|
94
|
+
import Pages404 from './../pages/404.tsx'
|
|
95
|
+
import Pages from './../pages/index.tsx'
|
|
96
|
+
import PagesRoot from './../pages/root.tsx'
|
|
97
|
+
import PagesUser from './../pages/user/index.tsx'
|
|
98
|
+
import PagesUserId from './../pages/user/[id]/index.tsx'
|
|
99
|
+
|
|
100
|
+
type PageConfig = Partial<
|
|
101
|
+
Omit<RouteObject, 'path' | 'Component' | 'element' | 'children'> & {
|
|
102
|
+
type?: 'single' | 'wrap'
|
|
103
|
+
}
|
|
104
|
+
>
|
|
105
|
+
|
|
106
|
+
export const defineConfig = (config: PageConfig) => config
|
|
107
|
+
|
|
108
|
+
export const routes: RouteObject[] = [
|
|
109
|
+
{
|
|
110
|
+
path: '/',
|
|
111
|
+
element: <PagesRoot />,
|
|
112
|
+
children: [
|
|
113
|
+
{
|
|
114
|
+
path: '/',
|
|
115
|
+
element: <Pages />
|
|
116
|
+
},
|
|
117
|
+
{
|
|
118
|
+
path: '/user',
|
|
119
|
+
children: [
|
|
120
|
+
{
|
|
121
|
+
path: '',
|
|
122
|
+
index: true,
|
|
123
|
+
element: <PagesUser />
|
|
124
|
+
},
|
|
125
|
+
{
|
|
126
|
+
path: ':id',
|
|
127
|
+
children: [
|
|
128
|
+
{
|
|
129
|
+
path: '',
|
|
130
|
+
index: true,
|
|
131
|
+
action: async () => {},
|
|
132
|
+
loader: async ({ params }) => await { params },
|
|
133
|
+
element: <PagesUserId />
|
|
134
|
+
}
|
|
135
|
+
]
|
|
136
|
+
}
|
|
137
|
+
]
|
|
138
|
+
}
|
|
139
|
+
]
|
|
140
|
+
},
|
|
141
|
+
{
|
|
142
|
+
path: '/*',
|
|
143
|
+
element: <Pages404 />
|
|
144
|
+
}
|
|
145
|
+
]
|
|
146
|
+
```
|
|
147
|
+
|
|
148
|
+
##### type: 'wrap'
|
|
149
|
+
|
|
150
|
+
当 `type` 配置为 `wrap` 时,该页面组件会作为父路由容器包裹其下的子路由:
|
|
151
|
+
|
|
152
|
+
```typescript
|
|
153
|
+
// src/pages/user/index.config.ts
|
|
154
|
+
import { defineConfig } from '../../router/autoRouter'
|
|
155
|
+
|
|
156
|
+
export default defineConfig({
|
|
157
|
+
type: 'wrap'
|
|
158
|
+
})
|
|
159
|
+
```
|
|
160
|
+
|
|
161
|
+
生成的路由结构:
|
|
162
|
+
|
|
163
|
+
```typescript
|
|
164
|
+
// src/router/autoRouter.tsx
|
|
165
|
+
import type { RouteObject } from 'react-router'
|
|
166
|
+
import Pages404 from './../pages/404.tsx'
|
|
167
|
+
import Pages from './../pages/index.tsx'
|
|
168
|
+
import PagesRoot from './../pages/root.tsx'
|
|
169
|
+
import PagesUser from './../pages/user/index.tsx'
|
|
170
|
+
import PagesUserId from './../pages/user/[id]/index.tsx'
|
|
171
|
+
|
|
172
|
+
type PageConfig = Partial<
|
|
173
|
+
Omit<RouteObject, 'path' | 'Component' | 'element' | 'children'> & {
|
|
174
|
+
type?: 'single' | 'wrap'
|
|
175
|
+
}
|
|
176
|
+
>
|
|
177
|
+
|
|
178
|
+
export const defineConfig = (config: PageConfig) => config
|
|
179
|
+
|
|
180
|
+
export const routes: RouteObject[] = [
|
|
181
|
+
{
|
|
182
|
+
path: '/',
|
|
183
|
+
element: <PagesRoot />,
|
|
184
|
+
children: [
|
|
185
|
+
{
|
|
186
|
+
path: '/',
|
|
187
|
+
element: <Pages />
|
|
188
|
+
},
|
|
189
|
+
{
|
|
190
|
+
path: '/user',
|
|
191
|
+
element: <PagesUser />,
|
|
192
|
+
children: [
|
|
193
|
+
{
|
|
194
|
+
path: ':id',
|
|
195
|
+
children: [
|
|
196
|
+
{
|
|
197
|
+
path: '',
|
|
198
|
+
index: true,
|
|
199
|
+
action: async () => {},
|
|
200
|
+
loader: async ({ params }) => await { params },
|
|
201
|
+
element: <PagesUserId />
|
|
202
|
+
}
|
|
203
|
+
]
|
|
204
|
+
}
|
|
205
|
+
]
|
|
206
|
+
}
|
|
207
|
+
]
|
|
208
|
+
},
|
|
209
|
+
{
|
|
210
|
+
path: '/*',
|
|
211
|
+
element: <Pages404 />
|
|
212
|
+
}
|
|
213
|
+
]
|
|
214
|
+
```
|
|
215
|
+
|
|
216
|
+
### Vue 项目
|
|
217
|
+
|
|
218
|
+
#### 安装依赖
|
|
219
|
+
|
|
220
|
+
```bash
|
|
221
|
+
npm install vue-router
|
|
222
|
+
```
|
|
223
|
+
|
|
224
|
+
#### 配置 Vite
|
|
225
|
+
|
|
226
|
+
```typescript
|
|
227
|
+
// vite.config.ts
|
|
228
|
+
import { defineConfig } from 'vite'
|
|
229
|
+
import vue from '@vitejs/plugin-vue'
|
|
230
|
+
import autoRouter from '@onoxm/vite-plugin-auto-router'
|
|
231
|
+
|
|
232
|
+
export default defineConfig({
|
|
233
|
+
plugins: [
|
|
234
|
+
vue(),
|
|
235
|
+
autoRouter({
|
|
236
|
+
framework: 'vue',
|
|
237
|
+
pagesDir: './src/views'
|
|
238
|
+
})
|
|
239
|
+
]
|
|
240
|
+
})
|
|
241
|
+
```
|
|
242
|
+
|
|
243
|
+
#### 目录结构
|
|
244
|
+
|
|
245
|
+
```
|
|
246
|
+
src/
|
|
247
|
+
├── views/
|
|
248
|
+
│ ├── 404.vue
|
|
249
|
+
│ ├── home/
|
|
250
|
+
│ │ ├── index.vue
|
|
251
|
+
│ │ └── index.config.ts
|
|
252
|
+
│ └── user/
|
|
253
|
+
│ ├── index.vue
|
|
254
|
+
│ ├── index.config.ts
|
|
255
|
+
│ ├── [id].vue
|
|
256
|
+
│ └── [id].config.ts
|
|
257
|
+
```
|
|
258
|
+
|
|
259
|
+
#### 特殊页面
|
|
260
|
+
|
|
261
|
+
- **`home` 页面**:路径会自动转换为 `/`,作为首页路由
|
|
262
|
+
- **`root` 页面**:作为根路由容器,包裹所有其他路由
|
|
263
|
+
- **`404` 或 `notfound` 页面**:路径会自动转换为 `/:pathMatch(.*)*`,作为 404 路由
|
|
264
|
+
|
|
265
|
+
#### 页面配置
|
|
266
|
+
|
|
267
|
+
继承自 [Vue Router RouteRecordRaw](https://router.vuejs.org/api/#routerecordraw),并进行以下修改:
|
|
268
|
+
|
|
269
|
+
- **移除**: `path`, `component`, `children`
|
|
270
|
+
- **新增**: `type?: 'single' | 'wrap'`
|
|
271
|
+
|
|
272
|
+
##### type: 'single'
|
|
273
|
+
|
|
274
|
+
当 `type` 配置为 `single` 时,该页面组件会作为独立路由生成:
|
|
275
|
+
|
|
276
|
+
```typescript
|
|
277
|
+
// src/views/user/index.config.ts
|
|
278
|
+
import { defineConfig } from '../../router/autoRouter'
|
|
279
|
+
|
|
280
|
+
export default defineConfig({
|
|
281
|
+
type: 'single'
|
|
282
|
+
})
|
|
283
|
+
```
|
|
284
|
+
|
|
285
|
+
生成的路由结构:
|
|
286
|
+
|
|
287
|
+
```typescript
|
|
288
|
+
// src/router/autoRouter.ts
|
|
289
|
+
import type { RouteRecordRaw } from 'vue-router'
|
|
290
|
+
import Views404 from './../views/404.vue'
|
|
291
|
+
import ViewsHome from './../views/home/index.vue'
|
|
292
|
+
import ViewsUser from './../views/user/index.vue'
|
|
293
|
+
import ViewsUserId from './../views/user/[id]/index.vue'
|
|
294
|
+
|
|
295
|
+
type PageConfig = Partial<
|
|
296
|
+
Omit<RouteRecordRaw, 'path' | 'component' | 'children'> & {
|
|
297
|
+
type?: 'single' | 'wrap'
|
|
298
|
+
}
|
|
299
|
+
>
|
|
300
|
+
|
|
301
|
+
export const defineConfig = (config: PageConfig) => config
|
|
302
|
+
|
|
303
|
+
export const routes: RouteRecordRaw[] = [
|
|
304
|
+
{
|
|
305
|
+
path: '/',
|
|
306
|
+
children: [
|
|
307
|
+
{
|
|
308
|
+
path: '',
|
|
309
|
+
name: 'home',
|
|
310
|
+
component: ViewsHome
|
|
311
|
+
}
|
|
312
|
+
]
|
|
313
|
+
},
|
|
314
|
+
{
|
|
315
|
+
path: '/user',
|
|
316
|
+
children: [
|
|
317
|
+
{
|
|
318
|
+
path: '',
|
|
319
|
+
component: ViewsUser
|
|
320
|
+
},
|
|
321
|
+
{
|
|
322
|
+
path: ':id',
|
|
323
|
+
children: [
|
|
324
|
+
{
|
|
325
|
+
path: '',
|
|
326
|
+
component: ViewsUserId
|
|
327
|
+
}
|
|
328
|
+
]
|
|
329
|
+
}
|
|
330
|
+
]
|
|
331
|
+
},
|
|
332
|
+
{
|
|
333
|
+
path: '/:pathMatch(.*)*',
|
|
334
|
+
children: [
|
|
335
|
+
{
|
|
336
|
+
path: '',
|
|
337
|
+
component: Views404
|
|
338
|
+
}
|
|
339
|
+
]
|
|
340
|
+
}
|
|
341
|
+
]
|
|
342
|
+
```
|
|
343
|
+
|
|
344
|
+
##### type: 'wrap'
|
|
345
|
+
|
|
346
|
+
当 `type` 配置为 `wrap` 时,该页面组件会作为父路由容器包裹其下的子路由:
|
|
347
|
+
|
|
348
|
+
```typescript
|
|
349
|
+
// src/views/user/index.config.ts
|
|
350
|
+
import { defineConfig } from '../../router/autoRouter'
|
|
351
|
+
|
|
352
|
+
export default defineConfig({
|
|
353
|
+
type: 'wrap'
|
|
354
|
+
})
|
|
355
|
+
```
|
|
356
|
+
|
|
357
|
+
生成的路由结构:
|
|
358
|
+
|
|
359
|
+
```typescript
|
|
360
|
+
// src/router/autoRouter.ts
|
|
361
|
+
import type { RouteRecordRaw } from 'vue-router'
|
|
362
|
+
import ViewsHome from './../views/home/index.vue'
|
|
363
|
+
import ViewsUser from './../views/user/index.vue'
|
|
364
|
+
import ViewsUserId from './../views/user/[id]/index.vue'
|
|
365
|
+
|
|
366
|
+
type PageConfig = Partial<
|
|
367
|
+
Omit<RouteRecordRaw, 'path' | 'component' | 'children'> & {
|
|
368
|
+
type?: 'single' | 'wrap'
|
|
369
|
+
}
|
|
370
|
+
>
|
|
371
|
+
|
|
372
|
+
export const defineConfig = (config: PageConfig) => config
|
|
373
|
+
|
|
374
|
+
export const routes: RouteRecordRaw[] = [
|
|
375
|
+
{
|
|
376
|
+
path: '/',
|
|
377
|
+
children: [
|
|
378
|
+
{
|
|
379
|
+
path: '',
|
|
380
|
+
name: 'home',
|
|
381
|
+
component: ViewsHome
|
|
382
|
+
}
|
|
383
|
+
]
|
|
384
|
+
},
|
|
385
|
+
{
|
|
386
|
+
path: '/user',
|
|
387
|
+
component: ViewsUser,
|
|
388
|
+
children: [
|
|
389
|
+
{
|
|
390
|
+
path: ':id',
|
|
391
|
+
children: [
|
|
392
|
+
{
|
|
393
|
+
path: '',
|
|
394
|
+
component: ViewsUserId
|
|
395
|
+
}
|
|
396
|
+
]
|
|
397
|
+
}
|
|
398
|
+
]
|
|
399
|
+
},
|
|
400
|
+
{
|
|
401
|
+
path: '/:pathMatch(.*)*',
|
|
402
|
+
children: [
|
|
403
|
+
{
|
|
404
|
+
path: '',
|
|
405
|
+
component: Views404
|
|
406
|
+
}
|
|
407
|
+
]
|
|
408
|
+
}
|
|
409
|
+
]
|
|
410
|
+
```
|
|
411
|
+
|
|
412
|
+
## ⚙️ 配置选项
|
|
413
|
+
|
|
414
|
+
### 插件配置
|
|
415
|
+
|
|
416
|
+
| 选项 | 类型 | 默认值 | 说明 |
|
|
417
|
+
| ------------ | ------------------ | --------------- | ------------------ |
|
|
418
|
+
| `framework` | `'react' \| 'vue'` | `'react'` | 框架类型 |
|
|
419
|
+
| `pagesDir` | `string` | `'./src/pages'` | 页面目录 |
|
|
420
|
+
| `routesFile` | `string` | `undefined` | 生成的路由文件路径 |
|
|
421
|
+
| `keepHome` | `boolean` | `false` | 是否保留 home 页面 |
|
|
422
|
+
| `keepRoot` | `boolean` | `false` | 是否保留 root 页面 |
|
|
423
|
+
| `lazy` | `boolean` | `true` | 是否启用懒加载 |
|
|
424
|
+
| `hmr` | `boolean` | `false` | 是否启用热更新 |
|
|
425
|
+
|
|
426
|
+
### 页面配置
|
|
427
|
+
|
|
428
|
+
| 选项 | 类型 | 默认值 | 说明 |
|
|
429
|
+
| ------ | -------------------- | ---------- | -------------- |
|
|
430
|
+
| `type` | `'single' \| 'wrap'` | `'single'` | 路由类型 |
|
|
431
|
+
| `*` | `any` | `any` | 继承自路由配置 |
|
|
432
|
+
|
|
433
|
+
## 📄 License
|
|
434
|
+
|
|
435
|
+
MIT
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
var e=Object.create,t=Object.defineProperty,n=Object.getOwnPropertyDescriptor,o=Object.getOwnPropertyNames,r=Object.getPrototypeOf,s=Object.prototype.hasOwnProperty,a=(a,i,u)=>(u=null!=a?e(r(a)):{},((e,r,a,i)=>{if(r&&"object"==typeof r||"function"==typeof r)for(var u,c=o(r),p=0,l=c.length;p<l;p++)u=c[p],s.call(e,u)||u===a||t(e,u,{get:(e=>r[e]).bind(null,u),enumerable:!(i=n(r,u))||i.enumerable});return e})(!i&&a&&a.__esModule?u:t(u,"default",{value:a,enumerable:!0}),a));let i=require("node:fs");i=a(i);let u=require("node:path");u=a(u);let c=require("fast-glob");function p(e){let t=l(e,0),n=!1;if(t+5<=e.length&&"async"===e.slice(t,t+5).toLowerCase()&&(n=!0,t=l(e,t+5)),t<e.length&&"("===e[t]){const n=h(e,t);if(-1!==n){let t=l(e,n+1);if(t+1<e.length&&"="===e[t]&&">"===e[t+1])return e}}const o=e.indexOf("(");if(-1===o)return e;const r=h(e,o);if(-1===r)return e;return(n?"async ":"")+e.slice(o,r+1)+" => "+e.slice(r+1)}function l(e,t){for(;t<e.length&&/\s/.test(e[t]);)t++;return t}function h(e,t){let n=0,o=!1,r="";for(let s=t;s<e.length;s++){const t=e[s];if('"'!==t&&"'"!==t&&"`"!==t){if(!o)if("("===t)n++;else if(")"===t&&(n--,0===n))return s}else o?t===r&&(o=!1):(o=!0,r=t)}return-1}c=a(c);var f=e=>{try{if(!i.default.existsSync(e))return;i.default.unlinkSync(e)}catch(t){console.error("删除文件失败:",t)}},d=e=>{try{if(!i.default.existsSync(e))return;i.default.rmSync(e,{recursive:!0})}catch(t){console.error("删除目录失败:",t)}},g=(e,t,n)=>{const o={};for(const[r,s]of Object.entries(t))if("function"==typeof s){const t=`${e}-${r}`;o[r]=t,n.set(t,`<start>${p(s.toString())}</end>`)}else o[r]=s;return o},m=e=>e.startsWith("/")?e.slice(1):e.endsWith("/")?e.slice(0,-1):e.startsWith("./")?e.slice(2):e,P=e=>i.default.existsSync(u.default.resolve(e,"tsconfig.json")),R={react:{ts:"src/router/autoRouter.tsx",js:"src/router/autoRouter.jsx"},vue:{ts:"src/router/autoRouter.ts",js:"src/router/autoRouter.js"}},y=(e,t,n)=>{let o=n;return o=o?u.default.resolve(t,o):u.default.resolve(t,R[e][P(t)?"ts":"js"]),o},$=/[/\\]/,j=/[^a-zA-Z0-9]/g,b=/^(\d)/,x=e=>e?e.charAt(0).toUpperCase()+e.slice(1).toLowerCase():"";function C(e){const t=u.default.extname(e),n=u.default.basename(e,t),o=u.default.dirname(e).split($).filter(Boolean);return"index"!==n&&"Index"!==n&&o.push(n),o.map(e=>(e=>e.replace(j,"_").replace(b,"_$1"))(e).split("_").map(x).join("")).join("")||"Component"}var O={"/home":"/","/root":"/"},w=(e,t)=>{const n=e.split("/"),o=n[n.length-1];return t&&o.endsWith(t)?o.slice(0,-t.length):o},M=(e,t,n)=>{const o=((e,t,n)=>t&&"/home"===e?"/home":n&&"/root"===e?"/root":O[e])(e,t,n);return o||("/notfound"===e.toLowerCase()||"/404"===e?"/*":e)},v=(e,t)=>e.endsWith(".config."+(t?"ts":"js"))?"index"===w(e,".config."+(t?"ts":"js"))?e.replace("index.config."+(t?"ts":"js"),"index."+(t?"ts":"js")):e.slice(0,-10)+"."+(t?"tsx":"jsx"):e.replace("index.config."+(t?"ts":"js"),"index."+(t?"ts":"js")),S=(e,t,n)=>{if(e.endsWith(".config."+(n?"ts":"js"))){const o=w(e,".config."+(n?"ts":"js")),r=(e=>{const t=e.split("/");return t.pop(),t.join("/")})(e).replace(t,"");return"index"===o?r||"/":r?`${r}/${o}`:`/${o}`}return e.replace(t,"").replace("/index.config."+(n?"ts":"js"),"")||"/"},T=async(e,t,n,o,r,s)=>{const a=[];for(const[i,u]of((e,t,n)=>Object.keys(e).map(e=>{const o=((e,t)=>e.endsWith(t?".tsx":".jsx")?e.slice(0,-4)+".config."+(t?"ts":"js"):e.replace("index."+(t?"ts":"js"),"index.config."+(t?"ts":"js")))(e,n);return[o,t[o]||{}]}))(e,t,o)){const t=v(i,o);if(!e[t])continue;const c=xe(S(i,`/${n}`,o)||"/",o,"react"),p=M(c,r,s);a.push({...u,path:p,type:u?.type,isRoot:!s&&"/root"===c,element:e[t]})}return a},E=e=>{if(e.index)return e;if("wrap"!==e.type){const{path:t,children:n,...o}=e;if(Object.keys(o).length>0)return{path:t,children:[{path:"",index:!0,...o},...(n||[]).map(E)]}}return e.children?{...e,children:e.children.map(E)}:e},k=e=>(e=>e.map(E))((e=>{const t=new Map,n=[],o=new Map,r=e=>{let t=o.get(e);return void 0===t&&(t="/"===e?[]:e.split("/").filter(Boolean),o.set(e,t)),t},s=e.slice().sort((e,t)=>e.path.length-t.path.length);for(const a of s){const e=a.path,o=r(e),s={...a,children:[]};if(t.set(e,s),o.length<=1){n.push(s);continue}const i="/"+o.slice(0,-1).join("/"),u=t.get(i);u?(s.path=o[o.length-1],u.children.push(s)):n.push(s)}return n})(e)),N=e=>{const{isRoot:t,type:n,children:o,...r}=e;return o?.length?{...r,children:o.map(N)}:r},F=e=>{const{children:t}=e;if(!t?.length){const{children:t,...n}=e;return n}const n=t.map(F);if("wrap"===e.type)return{...e,children:n};const{path:o,element:r,type:s,children:a,...i}=e;return r?{path:o,children:[{index:!0,element:r,...i},...n]}:{path:o,...i,children:n}},W=e=>N(F(e)),B=e=>k(e).map(W),L=e=>{if(e.some(e=>e.isRoot))return(e=>{let t,n,o;const r=[];for(const i of e)if(i.isRoot){if(t)throw new Error("There cannot be multiple root routes in a route");t=i}else"/"===i.path?n=i:"/*"===i.path?o=i:r.push(i);if($e(r,"path"))throw new Error("There cannot be duplicate paths in a route");const s=[n&&{...n,index:!0},...B(r)].filter(e=>Boolean(e)),a=[];return t&&a.push(N({...t,children:s})),o&&a.push(N(o)),a})(e);if($e(e,"path"))throw new Error("There cannot be duplicate paths in a route");return B(e)},z=(e,t)=>({import:`import ${e} from './${t}'`,component:`<start><${e} /></end>`,lazy:`const ${e} = lazy(() => import('${t}'))`,lazyComponent:`<start><Suspense><${e} /></Suspense></end>`});var D="index.tsx",A="index.jsx",I=(e,t,n,o,r,s,a)=>{const i=((e,t)=>e.includes(t?D:A)?e.replace(t?D:A,t?Me:ve):e)(Pe(e,t),a),u=me(de(n,e)),c=he(C(u),s),p=`element(${u})`;o[`/${i}`]=p,r.set(p,{path:u,componentName:c})},_=(e,t,n,o)=>{const r=Pe(e,t);n[`/${r}`]=g(r,fe(e),o)};function K(e,t,n,o,r,s,a,i,u){const p={},l=e.replace(/^\/+/,""),h=c.default.sync(l,{cwd:t,absolute:!0,onlyFiles:!0}).filter(ye);for(const c of h)if("component"===r){if(c.includes(".config."))continue;I(c,n,o,p,s,a,u)}else _(c,n,p,i);return p}var H=async(e,t,n,o,r,s,a,i)=>{const u=new Map,c=new Set,p=new Map;we(o,((e,t,n,o)=>{let r=e;for(const[s,a]of t){const e=z(a.componentName,a.path);r=r.replace(s,o?e.lazyComponent:e.component)}for(const[s,a]of n)r=r.replace(s,a);return be(r)})(function(e,t,n,o){const r=n.length>0?`\n${n.join("\n")}\n`:"";return`// Auto-generated by ono-auto-router\n${o?"import type { RouteObject } from 'react-router'\n":""}${t.join("\n")}${o?"\n\ntype PageConfig = Partial<\n Omit<RouteObject, 'path' | 'Component' | 'element' | 'children'> & {\n type?: 'single' | 'wrap'\n }\n>\n":""}${o?"\nexport const defineConfig = (config: PageConfig) => config":""}\n${r}\nexport const routes${o?": RouteObject[]":""} = ${JSON.stringify(e,null,2)}\n`}(L(await T(K("/**/*.{jsx,tsx}",t,n,o,"component",u,c,p,s),K("/**/*.config.{js,ts,jsx,tsx}",t,n,o,"config",u,c,p,s),e,s,a,i)),((e,t)=>{if(e)return["import { lazy, Suspense } from 'react'"];const n=[];for(const o of t.values())n.push(z(o.componentName,o.path).import);return n})(r,u),((e,t)=>{if(!e)return[];const n=[];for(const o of t.values())n.push(z(o.componentName,o.path).lazy);return n})(r,u),s),u,p,r))},U=(e,t,n,o,r,s)=>{const a=t.split(".")[0],i=(e=>`// Auto-generated by ono-auto-router${e?"\nexport { defineConfig } from './defineConfig'":""}\nexport { routes } from './autoRouter'\n`)(o),c=n?((e,t,n,o)=>`\n${n&&o?"":`const PATH_REPLACEMENTS${t?": Record<string, string>":""} = {\n${n||o?n?"'/root': '/'":"'/home': '/'":" '/home': '/',\n '/root': '/'"}\n}\n\n`}const cachedLazyComponents${t?": Record<\n string,\n LazyExoticComponent<ComponentType<unknown>>\n>":""} = {}\n\nconst getConfigFileName = ${t?"(pagePath: string): string":"(pagePath)"} => {\n if (pagePath.endsWith('.${t?"tsx":"jsx"}')) {\n return pagePath.slice(0, -4) + '.config.${t?"ts":"js"}'\n }\n return pagePath.replace('index.${t?"ts":"js"}', 'index.config.${t?"ts":"js"}')\n}\n\nconst formatPageModule = (${t?"\n pagesComponent: Record<string, ComponentType<unknown>>,\n pagesModule: Record<string, PageConfig>\n":"pagesComponent, pagesModule"})${t?": [string, PageConfig][]":""} => {\n return Object.keys(pagesComponent).map(page => {\n const configKey = getConfigFileName(page)\n return [configKey, pagesModule[configKey] || {}]\n })\n}\n\nconst normalizePath = ${t?"(pathStr: string): string":"(pathStr)"} => {\n${n&&o?"":" if (PATH_REPLACEMENTS[pathStr]) {\n return PATH_REPLACEMENTS[pathStr]\n }\n\n"} const lowerPath = pathStr.toLowerCase()\n if (lowerPath === '/notfound' || pathStr === '/404') {\n return '/*'\n }\n\n return pathStr\n}\n\nconst getBasename = ${t?"(filePath: string, ext?: string): string":"(filePath, ext)"} => {\n const parts = filePath.split('/')\n const lastPart = parts[parts.length - 1]\n if (ext && lastPart.endsWith(ext)) {\n return lastPart.slice(0, -ext.length)\n }\n return lastPart\n}\n\nconst getDirname = ${t?"(filePath: string): string":"(filePath)"} => {\n const parts = filePath.split('/')\n parts.pop()\n return parts.join('/')\n}\n\nconst getCompPathFromConfig = ${t?"(configPath: string): string":"(configPath)"} => {\n if (configPath.endsWith('.config.${t?"ts":"js"}')) {\n const fileName = getBasename(configPath, '.config.${t?"ts":"js"}')\n if (fileName === 'index') {\n return configPath.replace('index.config.${t?"ts":"js"}', 'index.${t?"tsx":"jsx"}')\n }\n return configPath.slice(0, -10) + '.${t?"tsx":"jsx"}'\n }\n return configPath.replace('index.config.${t?"ts":"js"}', 'index.${t?"tsx":"jsx"}')\n}\n\nconst getRawPathFromConfig = ${t?"(configPath: string): string":"(configPath)"} => {\n if (configPath.endsWith('.config.${t?"ts":"js"}')) {\n const fileName = getBasename(configPath, '.config.${t?"ts":"js"}')\n const dirPath = getDirname(configPath).replace('/${e}', '')\n if (fileName === 'index') {\n return dirPath || '/'\n }\n return dirPath ? dirPath + '/' + fileName : '/' + fileName\n }\n return (\n configPath.replace('/${e}', '').replace('/index.config.${t?"ts":"js"}', '') || '/'\n )\n}\n\nconst getFileDefault = ${t?"(modules: Record<string, { default: unknown }>)":"(modules)"} => {\n const result${t?": Record<string, unknown>":""} = {}\n\n for (const [path, module] of Object.entries(modules)) {\n if ('default' in module) {\n result[path] = module.default\n }\n }\n\n return result\n}\n\nconst filterUnComponent = ${t?"(pagesComponent: PagesComponentType)":"(pagesComponent)"} => {\n const result${t?": PagesComponentType":""} = {}\n\n for (const key of Object.keys(pagesComponent)) {\n if (!key.includes('.config.${t?"tsx":"jsx"}')) {\n result[key] = pagesComponent[key]\n }\n }\n\n return result\n}\n\nconst LazyComponent = ({\n Element\n}${t?": {\n Element: LazyExoticComponent<ComponentType<unknown>>\n}":""}) => {\n return (\n <Suspense>\n <Element />\n </Suspense>\n )\n}\n\nconst createBaseRoutes = () => {\n const pagesComponent = filterUnComponent({\n ...import.meta.glob('/${e}/**/*.${t?"tsx":"jsx"}')${t?" as PagesComponentType":""}\n })\n\n const pagesString = getFileDefault({\n ...import.meta.glob('/${e}/**/*.${t?"tsx":"jsx"}', {\n eager: true\n })\n })${t?" as Record<string, ComponentType<unknown>>":""}\n\n const pagesModule = getFileDefault({\n ...import.meta.glob('/${e}/**/*.config.${t?"ts":"js"}', {\n eager: true\n })\n })${t?" as Record<string, PageConfig>":""}\n\n const result${t?": (MyRouteObject & { isRoot?: boolean })[]":""} = []\n\n for (const [pagePath, page] of formatPageModule(pagesString, pagesModule)) {\n const compPath = getCompPathFromConfig(pagePath)\n\n if (!pagesComponent[compPath]) continue\n\n const rawPath = getRawPathFromConfig(pagePath) || '/'\n const processedPath = replaceBracketsWithColon(rawPath)\n const routePath = normalizePath(processedPath)\n\n let Component${t?": MyRouteObject['element']":""}\n if (!cachedLazyComponents[compPath]) {\n cachedLazyComponents[compPath] = lazy(pagesComponent[compPath])\n }\n Component = <LazyComponent Element={cachedLazyComponents[compPath]} />\n\n result.push({\n ...page,\n path: routePath,\n type: page?.type,\n isRoot: ${!o&&"processedPath === '/root'"},\n element: Component\n })\n }\n\n return result\n}`)(e,o,r,s):((e,t,n,o)=>`\n${n&&o?"":`const PATH_REPLACEMENTS${t?": Record<string, string>":""} = {\n${n||o?n?"'/root': '/'":"'/home': '/'":" '/home': '/',\n '/root': '/'"}\n}\n\n`}const getConfigFileName = ${t?"(pagePath: string): string":"(pagePath)"} => {\n if (pagePath.endsWith('.${t?"tsx":"jsx"}')) {\n return pagePath.slice(0, -4) + '.config.${t?"ts":"js"}'\n }\n return pagePath.replace('index.${t?"ts":"js"}', 'index.config.${t?"ts":"js"}')\n}\n\nconst formatPageModule = (${t?"\n pagesComponent: PagesComponentType,\n pagesModule: Record<string, PageConfig>\n":"pagesComponent, pagesModule"})${t?": [string, PageConfig][]":""} => {\n return Object.keys(pagesComponent).map(page => {\n const configKey = getConfigFileName(page)\n return [configKey, pagesModule[configKey] || {}]\n })\n}\n\nconst normalizePath = ${t?"(pathStr: string): string":"(pathStr)"} => {\n${n&&o?"":" if (PATH_REPLACEMENTS[pathStr]) {\n return PATH_REPLACEMENTS[pathStr]\n }\n\n"} const lowerPath = pathStr.toLowerCase()\n if (lowerPath === '/notfound' || pathStr === '/404') {\n return '/*'\n }\n\n return pathStr\n}\n\nconst getBasename = ${t?"(filePath: string, ext?: string): string":"(filePath, ext)"} => {\n const parts = filePath.split('/')\n const lastPart = parts[parts.length - 1]\n if (ext && lastPart.endsWith(ext)) {\n return lastPart.slice(0, -ext.length)\n }\n return lastPart\n}\n\nconst getDirname = ${t?"(filePath: string): string":"(filePath)"} => {\n const parts = filePath.split('/')\n parts.pop()\n return parts.join('/')\n}\n\nconst getCompPathFromConfig = ${t?"(configPath: string): string":"(configPath)"} => {\n if (configPath.endsWith('.config.${t?"ts":"js"}')) {\n const fileName = getBasename(configPath, '.config.${t?"ts":"js"}')\n if (fileName === 'index') {\n return configPath.replace('index.config.${t?"ts":"js"}', 'index.${t?"tsx":"jsx"}')\n }\n return configPath.slice(0, -10) + '.${t?"tsx":"jsx"}'\n }\n return configPath.replace('index.config.${t?"ts":"js"}', 'index.${t?"tsx":"jsx"}')\n}\n\nconst getRawPathFromConfig = ${t?"(configPath: string): string":"(configPath)"} => {\n if (configPath.endsWith('.config.${t?"ts":"js"}')) {\n const fileName = getBasename(configPath, '.config.${t?"ts":"js"}')\n const dirPath = getDirname(configPath).replace('/${e}', '')\n if (fileName === 'index') {\n return dirPath || '/'\n }\n return dirPath ? dirPath + '/' + fileName : '/' + fileName\n }\n return (\n configPath.replace('/${e}', '').replace('/index.config.${t?"ts":"js"}', '') || '/'\n )\n}\n\nconst getFileDefault = ${t?"(modules: Record<string, { default: unknown }>)":"(modules)"} => {\n const result${t?": PagesComponentType | Record<string, PageConfig>":""} = {}\n\n for (const [path, module] of Object.entries(modules)) {\n if ('default' in module) {\n result[path] = module.default${t?" as ComponentType<unknown> | PageConfig":""}\n }\n }\n\n return result\n}\n\nconst filterUnComponent = ${t?"(pagesComponent: PagesComponentType)":"(pagesComponent)"} => {\n const result${t?": PagesComponentType":""} = {}\n\n for (const key of Object.keys(pagesComponent)) {\n if (!key.includes('.config.${t?"tsx":"jsx"}')) {\n result[key] = pagesComponent[key]\n }\n }\n\n return result\n}\n\nconst createBaseRoutes = () => {\n const pagesComponent = filterUnComponent(\n getFileDefault({\n ...import.meta.glob('/${e}/**/*.${t?"tsx":"jsx"}', { eager: true })\n })${t?" as PagesComponentType":""}\n )\n\n const pagesModule = getFileDefault({\n ...import.meta.glob('/${e}/**/*.config.${t?"ts":"js"}', {\n eager: true\n })\n })${t?" as Record<string, PageConfig>":""}\n\n const result${t?": (MyRouteObject & { isRoot?: boolean })[]":""} = []\n\n for (const [pagePath, page] of formatPageModule(pagesComponent, pagesModule)) {\n const compPath = getCompPathFromConfig(pagePath)\n\n if (!pagesComponent[compPath]) continue\n\n const rawPath = getRawPathFromConfig(pagePath) || '/'\n const processedPath = replaceBracketsWithColon(rawPath)\n const routePath = normalizePath(processedPath)\n\n const Component = pagesComponent[compPath]\n result.push({\n ...page,\n path: routePath,\n type: page?.type,\n isRoot: ${!o&&"processedPath === '/root'"},\n element: <Component />\n })\n }\n\n return result\n}`)(e,o,r,s),p=(e=>`\nconst processRoute = ${e?"(route: MyRouteObject): MyRouteObject":"(route)"} =>\n cleanIsRoot(removeEmptyChildren(route))\n\nconst processRoutes = ${e?"(routes: MyRouteObject[]): MyRouteObject[]":"(routes)"} =>\n buildRouteTree(routes).map(processRoute)\n\nconst handleRootRoute = ${e?"(routes: (MyRouteObject & { isRoot?: boolean })[])":"(routes)"} => {\n let rootRoute${e?": MyRouteObject | undefined":" = undefined"}\n let homeRoute${e?": MyRouteObject | undefined":" = undefined"}\n let notFoundRoute${e?": MyRouteObject | undefined":" = undefined"}\n const filteredRoutes${e?": MyRouteObject[]":""} = []\n\n for (const route of routes) {\n if (route.isRoot) {\n if (rootRoute)\n throw new Error('There cannot be multiple root routes in a route')\n rootRoute = route\n } else if (route.path === '/') {\n homeRoute = route\n } else if (route.path === '/*') {\n notFoundRoute = route\n } else {\n filteredRoutes.push(route)\n }\n }\n\n if (hasDuplicateProperty(filteredRoutes, 'path'))\n throw new Error('There cannot be duplicate paths in a route')\n\n const children = [\n homeRoute && { ...homeRoute, index: true },\n ...processRoutes(filteredRoutes)\n ].filter(${e?"(route): route is NonNullable<typeof route> => Boolean(route)":"Boolean"})\n\n const result${e?": MyRouteObject[]":""} = []\n\n if (rootRoute) {\n result.push(cleanIsRoot({ ...rootRoute, children }))\n }\n\n if (notFoundRoute) {\n result.push(cleanIsRoot(notFoundRoute))\n }\n\n return result\n}\n\nconst formatRoutes = (\n routes${e?": (MyRouteObject & { isRoot?: boolean })[]":""}\n) => {\n if (routes.some(r => r.isRoot)) return handleRootRoute(routes)\n\n if (hasDuplicateProperty(routes, 'path'))\n throw new Error('There cannot be duplicate paths in a route')\n\n return processRoutes(routes)\n}`)(o),l=(e=>`\nconst processSingleRoute = ${e?"(route: MyRouteObject): MyRouteObject":"(route)"} => {\n if (route.index) {\n return route\n }\n\n if (route.type !== 'wrap') {\n const { path: routePath, children, ...extraProps } = route\n const hasExtraProps = Object.keys(extraProps).length > 0\n\n if (hasExtraProps) {\n const indexRoute${e?": MyRouteObject":""} = {\n path: '',\n index: true,\n ...extraProps\n }\n\n return {\n path: routePath,\n children: [indexRoute, ...(children || []).map(processSingleRoute)]\n }\n }\n }\n\n if (route.children) {\n return {\n ...route,\n children: route.children.map(processSingleRoute)\n }\n }\n\n return route\n}\n\nconst formatRouteTree = ${e?"(routes: MyRouteObject[]): MyRouteObject[]":"(routes)"} => {\n return routes.map(processSingleRoute)\n}\n\nconst buildTreeStructure = ${e?"(routes: MyRouteObject[]): MyRouteObject[]":"(routes)"} => {\n const routeMap = new Map${e?"<string, MyRouteObject>":""}()\n const rootRoutes${e?": MyRouteObject[]":""} = []\n const pathPartsCache = new Map${e?"<string, string[]>":""}()\n\n const getPathParts = ${e?"(pathStr: string): string[]":"(pathStr)"} => {\n let parts = pathPartsCache.get(pathStr)\n if (parts === undefined) {\n parts = pathStr === '/' ? [] : pathStr.split('/').filter(Boolean)\n pathPartsCache.set(pathStr, parts)\n }\n return parts\n }\n\n const sortedRoutes = routes\n .slice()\n .sort((a, b) => a.path.length - b.path.length)\n\n for (const route of sortedRoutes) {\n const routePath = route.path\n const pathParts = getPathParts(routePath)\n const currentRoute = { ...route, children: [] }\n routeMap.set(routePath, currentRoute)\n\n if (pathParts.length <= 1) {\n rootRoutes.push(currentRoute)\n continue\n }\n\n const parentPath = '/' + pathParts.slice(0, -1).join('/')\n const parentRoute = routeMap.get(parentPath)\n\n if (parentRoute) {\n currentRoute.path = pathParts[pathParts.length - 1]\n parentRoute.children${e?"!":""}.push(currentRoute)\n } else {\n rootRoutes.push(currentRoute)\n }\n }\n\n return rootRoutes\n}\n\nconst buildRouteTree = ${e?"(routes: MyRouteObject[]): MyRouteObject[]":"(routes)"} => {\n const treeStructure = buildTreeStructure(routes)\n return formatRouteTree(treeStructure)\n}`)(o),h=(e=>`\nconst cleanIsRoot = ${e?"(route: RouteWithIsRoot): MyRouteObject":"(route)"} => {\n const { isRoot, type, children, ...rest } = route${e?" as RouteWithIsRoot & {\n type?: unknown\n }":""}\n\n if (!children?.length) {\n return rest\n }\n\n return {\n ...rest,\n children: children.map(cleanIsRoot)\n }\n}`)(o),f=(e=>`\nconst removeEmptyChildren = ${e?"(route: MyRouteObject): MyRouteObject":"(route)"} => {\n const { children } = route\n\n if (!children?.length) {\n const { children: _, ...rest } = route\n return rest\n }\n\n const processedChildren = children.map(removeEmptyChildren)\n\n if (route.type === 'wrap') {\n return { ...route, children: processedChildren }\n }\n\n const { path, element, type, children: _, ...rest } = route\n\n if (!element) {\n return { path, ...rest, children: processedChildren }\n }\n\n return {\n path,\n children: [\n { index: true, element, ...rest }${e?" as MyRouteObject":""},\n ...processedChildren\n ]\n }\n}`)(o),d=o?"// Auto-generated by ono-auto-router\nimport type { PageConfig } from './autoRouter'\nexport const defineConfig = (config: PageConfig) => config\n":"",g=(e=>`\nconst getRoutes = () => {\n const baseRoutes = createBaseRoutes()\n const formattedRoutes = formatRoutes(baseRoutes)${e?" as RouteObject[]":""}\n return formattedRoutes\n}\n\nexport const routes = getRoutes()`)(o),m=((e,t,n,o,r,s,a,i)=>`// Auto-generated by ono-auto-router${e}${t}${n}${o}${r}${s}${a}${i}\n`)(((e,t)=>e?`\n${t?"import {\n lazy,\n Suspense,\n type ComponentType,\n type LazyExoticComponent\n} from 'react'":"import type { ComponentType } from 'react'"}\nimport type { RouteObject } from 'react-router'\n\n${t?"type PagesComponentType = Record<\n string,\n () => Promise<{ default: ComponentType<unknown> }>\n>":"type PagesComponentType = Record<string, ComponentType<unknown>>"}\n\ninterface MyRouteObject extends Omit<RouteObject, 'path' | 'children'> {\n path: string\n type?: 'single' | 'wrap'\n children?: MyRouteObject[]\n}\n\ntype RouteWithIsRoot = MyRouteObject & { isRoot?: boolean }\n\nexport type PageConfig = Partial<\n Omit<MyRouteObject, 'Component' | 'element' | 'children' | 'path'>\n>`:t?"\nimport { lazy, Suspense } from 'react'\n":"")(o,n),(e=>`\nconst hasDuplicateProperty = ${e?"<T, K extends keyof T>(items: T[], propName: K)":"(items, propName)"} => {\n const seenValues = new Set()\n return items.some(item => {\n if (seenValues.has(item[propName])) {\n return true\n }\n seenValues.add(item[propName])\n return false\n })\n}\n\nconst replaceBracketsWithColon = ${e?"(str: string)":"(str)"} => {\n return str.replace(/\\[([^\\]]+)\\]/g, ':$1').replace('.tsx', '')\n}`)(o),f,h,l,p,c,g);o?(we(u.default.resolve(a,"./index.ts"),i),we(u.default.resolve(a,"./autoRouter.tsx"),m),we(u.default.resolve(a,"./defineConfig.ts"),d)):we(t,m)},V={"/home":"/","/root":"/"},X=(e,t)=>{const n=e.split("/"),o=n[n.length-1];return t&&o.endsWith(t)?o.slice(0,-t.length):o},q=(e,t,n)=>{const o=((e,t,n)=>t&&"/home"===e?"/home":n&&"/root"===e?"/root":V[e])(e,t,n);return o||("/notfound"===e.toLowerCase()||"/404"===e?"/:pathMatch(.*)*":e)},J=(e,t)=>e.endsWith(".config."+(t?"ts":"js"))?"index"===X(e,".config."+(t?"ts":"js"))?e.replace("index.config."+(t?"ts":"js"),"index."+(t?"ts":"js")):e.slice(0,-10)+".vue":e.replace("index.config."+(t?"ts":"js"),"index."+(t?"ts":"js")),Z=(e,t,n)=>{if(e.endsWith(".config."+(n?"ts":"js"))){const o=X(e,".config."+(n?"ts":"js")),r=(e=>{const t=e.split("/");return t.pop(),t.join("/")})(e).replace(t,"");return"index"===o?r||"/":r?`${r}/${o}`:`/${o}`}return e.replace(t,"").replace("/index.config."+(n?"ts":"js"),"")||"/"},G=async(e,t,n,o,r,s)=>{const a=[];for(const[i,u]of((e,t,n)=>Object.keys(e).map(e=>{const o=((e,t)=>e.replace("index."+(t?"ts":"js"),"index.config."+(t?"ts":"js")))(e,n);return[o,t[o]||{}]}))(e,t,o)){const t=J(i,o),c=xe(Z(i,`/${n}`,o)||"/",o,"vue"),p=q(c,r,s);a.push({...u,path:p,type:u?.type,isRoot:!s&&"/root"===c,component:e[t]})}return a},Q=e=>{if(!e.path)return e;if("wrap"!==e.type){const{path:t,children:n,...o}=e;if(Object.keys(o).length>0)return{path:t,children:[{path:"",...o},...(n||[]).map(Q)]}}return e.children?{...e,children:e.children.map(Q)}:e},Y=e=>(e=>e.map(Q))((e=>{const t=new Map,n=[],o=new Map,r=e=>{let t=o.get(e);return void 0===t&&(t="/"===e?[]:e.split("/").filter(Boolean),o.set(e,t)),t},s=e.slice().sort((e,t)=>e.path.length-t.path.length);for(const a of s){const e=a.path,o=r(e),s={...a,children:[]};if(t.set(e,s),o.length<=1){n.push(s);continue}const i="/"+o.slice(0,-1).join("/"),u=t.get(i);u?(s.path=o[o.length-1],u.children.push(s)):n.push(s)}return n})(e)),ee=e=>{const{isRoot:t,type:n,children:o,...r}=e;return o?.length?{...r,children:o.map(ee)}:r},te=e=>{const{children:t}=e;if(!t?.length){const{children:t,...n}=e;return n}const n=t.map(te);if("wrap"===e.type)return{...e,children:n};const{path:o,component:r,type:s,children:a,...i}=e;return r?{path:o,children:[{path:"",component:r,...i},...n]}:{path:o,...i,children:n}},ne=e=>ee(te(e)),oe=e=>Y(e).map(ne),re=e=>{if(e.some(e=>e.isRoot))return(e=>{let t,n,o;const r=[];for(const i of e)if(i.isRoot){if(t)throw new Error("There cannot be multiple root routes in a route");t=i}else"/"===i.path?n=i:"/:pathMatch(.*)*"===i.path?o=i:r.push(i);if($e(r,"path"))throw new Error("There cannot be duplicate paths in a route");const s=[n,...oe(r)].filter(e=>Boolean(e)),a=[];return t&&a.push(ee({...t,children:s})),o&&a.push(ee(o)),a})(e);if($e(e,"path"))throw new Error("There cannot be duplicate paths in a route");return oe(e)},se=(e,t)=>({import:`import ${e} from './${t}'`,component:`<start>${e}</end>`,lazyComponent:`<start>() => import('${t}')</end>`});var ae=(e,t,n,o,r,s,a)=>{const i=((e,t)=>e.replace("index.vue",t?Me:ve))(Pe(e,t),a),u=me(de(n,e)),c=he(C(u),s),p=`component(${u})`;o[`/${i}`]=p,r.set(p,{path:u,componentName:c})},ie=(e,t,n,o)=>{const r=Pe(e,t);n[`/${r}`]=g(r,fe(e),o)};function ue(e,t,n,o,r,s,a,i,u){const p={},l=e.replace(/^\/+/,""),h=c.default.sync(l,{cwd:t,absolute:!0,onlyFiles:!0}),f="component"===r?((e,t)=>t.map(t=>t.includes("index.vue")||2===t.replace(e.replaceAll("\\","/"),"").split("/").length?t:void 0).filter(Boolean))(t,h):h.filter(ye);for(const c of f)if("component"===r){if(c.includes(".config."))continue;ae(c,n,o,p,s,a,u)}else ie(c,n,p,i);return p}var ce=async(e,t,n,o,r,s,a,i)=>{const u=new Map,c=new Set,p=new Map;we(o,((e,t,n,o)=>{let r=e;for(const[s,a]of t){const e=se(a.componentName,a.path);r=r.replace(s,o?e.lazyComponent:e.component)}for(const[s,a]of n)r=r.replace(s,a);return be(r)})(function(e,t,n,o){return`// Auto-generated by ono-auto-router${n?"\nimport type { RouteRecordRaw } from 'vue-router'":""}${o?"":`\n${t.join("\n")}${n?"":"\n"}`}${n?"\n\ntype PageConfig = Partial<\n Omit<RouteRecordRaw, 'path' | 'component' | 'children'> & {\n type?: 'single' | 'wrap'\n }\n>\n":""}${n?"\nexport const defineConfig = (config: PageConfig) => config\n":""}\nexport const routes${n?": RouteRecordRaw[]":""} = ${JSON.stringify(e,null,2)}\n`}(re(await G(ue("/**/*.vue",t,n,o,"component",u,c,p,s),ue("/**/*.config.{js,ts}",t,n,o,"config",u,c,p,s),e,s,a,i)),((e,t)=>{if(e)return[];const n=[];for(const o of t.values())n.push(se(o.componentName,o.path).import);return n})(r,u),s,r),u,p,r))},pe=(e,t,n,o,r,s)=>{const a=t.split(".")[0],i=(e=>`// Auto-generated by ono-auto-router${e?"\nexport { defineConfig } from './defineConfig'":""}\nexport { routes } from './autoRouter'\n`)(n),c=((e,t,n,o,r)=>`\nconst pathCache = new Map${t?"<string, { compPath: string; basePath: string }>":""}()\nconst basenameCache = new Map${t?"<string, string>":""}()\n\nconst normalizePagePath = ${t?"(key: string): string":"(key)"} => {\n if (key.includes('index.vue')) return key.replace('index.vue', 'index.${t?"ts":"js"}')\n if (key.includes('root.vue')) return key.replace('root.vue', 'root/index.${t?"ts":"js"}')\n if (key.includes('404.vue')) return key.replace('404.vue', '404/index.${t?"ts":"js"}')\n if (key.toLowerCase().includes('notfound.vue'))\n return key.toLowerCase().replace('notfound.vue', 'notfound/index.${t?"ts":"js"}')\n return key\n}\n\nconst getBasename = ${t?"(filePath: string, ext?: string): string":"(filePath, ext)"} => {\n const cacheKey = ext ? filePath + ':' + ext : filePath\n if (basenameCache.has(cacheKey)) {\n return basenameCache.get(cacheKey)${t?"!":""}\n }\n const parts = filePath.split('/')\n const lastPart = parts[parts.length - 1] || ''\n let result = lastPart\n if (ext && lastPart.endsWith(ext)) {\n result = lastPart.slice(0, -ext.length)\n }\n basenameCache.set(cacheKey, result)\n return result\n}\n\nconst getDirname = ${t?"(filePath: string): string":"(filePath)"} => {\n const parts = filePath.split('/')\n parts.pop()\n return parts.join('/')\n}\n\nconst getPathsFromConfig = ${t?"(configPath: string)":"(configPath)"} => {\n const cacheKey = configPath\n if (pathCache.has(cacheKey)) {\n return pathCache.get(cacheKey)${t?"!":""}\n }\n\n let compPath${t?": string":" = ''"}\n let basePath${t?": string":" = ''"}\n\n if (configPath.endsWith('.config.${t?"ts":"js"}')) {\n const fileName = getBasename(configPath, '.config.${t?"ts":"js"}')\n const dirPath = getDirname(configPath).replace('/${e}', '')\n\n if (fileName === 'index') {\n compPath = configPath.replace('index.config.${t?"ts":"js"}', 'index.${t?"ts":"js"}')\n basePath = dirPath || '/'\n } else {\n compPath = configPath.slice(0, -10) + '.vue'\n basePath = dirPath ? dirPath + '/' + fileName : '/' + fileName\n }\n } else {\n compPath = configPath.replace('index.config.${t?"ts":"js"}', 'index.${t?"ts":"js"}')\n basePath =\n configPath.replace('/${e}', '').replace('/index.config.${t?"ts":"js"}', '') ||\n '/'\n }\n\n const result = { compPath, basePath }\n pathCache.set(cacheKey, result)\n return result\n}\n\nconst formatPath = ${t?"(path: string): string":"(path)"} => {\n const lowerPath = path.toLowerCase()\n ${o?"":"if (lowerPath === '/home') return '/'\n "}if (lowerPath === '/notfound' || path === '/404') return '/:pathMatch(.*)*'\n return path.replace('/index.${t?"ts":"js"}', '') || '/'\n}\n\nconst getConfigFileName = ${t?"(pagePath: string): string":"(pagePath)"} => {\n if (pagePath.endsWith('.vue')) {\n return pagePath.slice(0, -4) + '.config.${t?"ts":"js"}'\n }\n return pagePath.replace('index.${t?"ts":"js"}', 'index.config.${t?"ts":"js"}')\n}\n\nconst createBaseRoutes = ()${t?": MyRouteObject[]":""} => {\n const pagesComponent = Object.fromEntries(\n Object.entries({\n ...import.meta.glob('/${e}/**/index.vue'${n?"":", { eager: true, import: 'default' }"}),\n ...import.meta.glob('/${e}/*.vue'${n?"":", { eager: true, import: 'default' }"})\n }${t?" as PagesComponentType":""}).map(([key, value]) => [\n normalizePagePath(key),\n value\n ])\n )\n\n const pagesModule${t?": Record<string, PageConfig>":""} = import.meta.glob(\n '/${e}/**/*.config.${t?"ts":"js"}',\n { eager: true, import: 'default' }\n )\n\n const result${t?": (MyRouteObject & { isRoot?: boolean })[]":""} = []\n\n for (const pagePath of Object.keys(pagesComponent)) {\n const configPath = getConfigFileName(pagePath)\n const content = pagesModule[configPath] || {}\n const { compPath, basePath } = getPathsFromConfig(configPath)\n const path = replaceBracketsWithColon(basePath || '/')\n const name = path.split('/').filter(Boolean).join('-') || 'index'\n\n result.push({\n path: formatPath(path),\n name,\n component: pagesComponent[compPath],\n isRoot: ${!r&&"path === '/root'"},\n ...content${t?"!":""}\n })\n }\n\n return result\n}\n\nconst processRoutes = ${t?"(\n routes: MyRouteObject[],\n removeLeadingSlash = false\n): MyRouteObject[]":"(routes, removeLeadingSlash)"} =>\n buildRouteTree(routes, removeLeadingSlash)\n .map(removeEmptyChildren)\n .map(cleanIsRoot)\n\nconst handleRootRoute = ${t?"(\n routes: (MyRouteObject & { isRoot?: boolean })[]\n): MyRouteObject[]":"(routes)"} => {\n let rootRoute${t?": (MyRouteObject & { isRoot?: boolean }) | undefined":" = undefined"}\n let homeRoute${t?": MyRouteObject | undefined":" = undefined"}\n let notFoundRoute${t?": MyRouteObject | undefined":" = undefined"}\n const filteredRoutes${t?": (MyRouteObject & { isRoot?: boolean })[]":""} = []\n\n for (const route of routes) {\n if (route.isRoot) {\n if (rootRoute)\n throw new Error('There cannot be multiple root routes in a route')\n rootRoute = route\n } else if (route.path === '/') {\n homeRoute = route\n } else if (route.path === '/:pathMatch(.*)*') {\n notFoundRoute = route\n } else {\n filteredRoutes.push(route)\n }\n }\n\n if (hasDuplicateProperty(filteredRoutes, 'path'))\n throw new Error('There cannot be duplicate paths in a route')\n\n const processedRoutes${t?": (MyRouteObject & { isRoot?: boolean })[]":""} = []\n\n if (rootRoute) {\n const children = [\n homeRoute && { ...homeRoute, path: '' },\n ...processRoutes(filteredRoutes, true)\n ].filter(Boolean)${t?" as MyRouteObject[]":""}\n processedRoutes.push({ ...rootRoute, path: '/', children })\n }\n\n if (notFoundRoute) processedRoutes.push(notFoundRoute)\n\n return processedRoutes.map(cleanIsRoot)\n}\n\nconst createRoutes = ()${t?": RouteRecordRaw[]":""} => {\n const routes = createBaseRoutes()\n\n if (routes.some((r${t?": MyRouteObject & { isRoot?: boolean }":""}) => r.isRoot))\n return handleRootRoute(routes)${t?" as RouteRecordRaw[]":""}\n\n if (hasDuplicateProperty(routes, 'path'))\n throw new Error('There cannot be duplicate paths in a route')\n\n return processRoutes(routes)${t?" as RouteRecordRaw[]":""}\n}\n`)(e,n,o,r,s),p=(e=>`\nconst buildRouteTree = ${e?"(\n routes: MyRouteObject[],\n removeLeadingSlash: boolean = false\n)":"(routes, removeLeadingSlash)"} => {\n const routeMap = new Map${e?"<string, MyRouteObject & { isRoot?: boolean }>":""}()\n const rootRoutes${e?": MyRouteObject[]":""} = []\n\n const sortedRoutes = [...routes].sort((a, b) => a.path.length - b.path.length)\n\n for (const route of sortedRoutes) {\n let path = route.path\n if (removeLeadingSlash && path.startsWith('/')) {\n path = path.slice(1)\n }\n routeMap.set(path, { ...route, path, children: [] })\n }\n\n for (const [path, route] of routeMap) {\n const slashIndex = path.lastIndexOf('/')\n\n if (slashIndex <= 0) {\n const { isRoot, ...rest } = route\n rootRoutes.push(rest)\n continue\n }\n\n const parentPath = path.slice(0, slashIndex)\n const parentRoute = routeMap.get(parentPath)\n\n if (parentRoute) {\n const relativePath = path.slice(slashIndex + 1)\n const childRoute = { ...route, path: relativePath }\n parentRoute.children${e?"!":""}.push(childRoute)\n } else {\n const { isRoot, ...rest } = route\n rootRoutes.push(rest)\n }\n }\n\n return rootRoutes\n}\n`)(n),l=(e=>`\nconst cleanIsRoot = ${e?"(\n route: MyRouteObject & { isRoot?: boolean }\n): MyRouteObject":"(route)"} => {\n const { isRoot, type, children, ...rest } = route\n const result${e?": MyRouteObject":""} = { ...rest }\n\n if (children?.length) {\n result.children = children.map(cleanIsRoot)\n }\n\n return result\n}\n`)(n),h=(e=>`\nconst removeEmptyChildren = ${e?"(route: MyRouteObject): MyRouteObject":"(route)"} => {\n if (!route.children?.length) {\n const { children, ...rest } = route\n return rest\n }\n\n const hasIndexRoute = route.children.some(\n child => child.path === '' || child.path === '/'\n )\n\n const routeType = route.type || 'single'\n\n if (routeType === 'wrap') {\n const { children, ...rest } = route\n return { ...rest, children: children.map(removeEmptyChildren) }\n }\n\n if (routeType === 'single' || (!hasIndexRoute && route.component)) {\n const { path, component, type, ...rest } = route\n return {\n path,\n children: [\n { path: '', component, ...rest }${e?" as MyRouteObject":""},\n ...route.children.map(removeEmptyChildren)\n ]\n }\n }\n\n if (hasIndexRoute) {\n const { path, ...rest } = route\n const updatedChildren = route.children.map(child =>\n child.path === '' || child.path === '/' ? { ...rest, ...child } : child\n )\n return { path, children: updatedChildren.map(removeEmptyChildren) }\n }\n\n return { ...route, children: route.children.map(removeEmptyChildren) }\n}\n`)(n),f=n?"// Auto-generated by ono-auto-router\nimport type { PageConfig } from './autoRouter'\nexport const defineConfig = (config: PageConfig) => config\n":"",d=((e,t,n,o,r,s,a)=>`// Auto-generated by ono-auto-router${e}${t}${n}${o}${r}${s}${a}\n`)(n?"\nimport type { RouteRecordRaw } from 'vue-router'\n\ntype PagesComponentType = Record<string, () => Promise<unknown>>\n\ninterface MyRouteObject extends Omit<RouteRecordRaw, 'children'> {\n type?: 'single' | 'wrap'\n children?: MyRouteObject[]\n}\n\nexport type PageConfig = Partial<\n Omit<MyRouteObject, 'component' | 'children' | 'path'>\n>\n":"",(e=>`\nconst hasDuplicateProperty = ${e?"<T>(items: T[], propName: keyof T)":"(items, propName)"} => {\n const seenValues = new Set()\n return items.some(item => {\n if (seenValues.has(item[propName])) {\n return true\n }\n seenValues.add(item[propName])\n return false\n })\n}\n\nconst replaceBracketsWithColon = ${e?"(str: string)":"(str)"} => {\n return str.replace(/\\[([^\\]]+)\\]/g, ':$1').replace('.tsx', '')\n}\n`)(n),h,l,p,c,"\nexport const routes = createRoutes()");n?(we(u.default.resolve(a,"./index.ts"),i),we(u.default.resolve(a,"./autoRouter.ts"),d),we(u.default.resolve(a,"./defineConfig.ts"),f)):we(t,d)},le=(e,t,n,o,r,s,a,c)=>{const p=u.default.resolve(n,t);if(!i.default.existsSync(p))throw new Error(`Pages directory not found: ${p}`);const l=y(e,n,o),h=P(n),g=l.split(".")[0];f(g+".js"),f(g+".ts"),f(g+".jsx"),f(g+".tsx"),d(g),"react"===e?c?U(t,l,a,h,r,s):H(t,p,n,l,a,h,r,s):c?pe(t,l,h,a,r,s):ce(t,p,n,l,a,h,r,s),console.log(`[ono-auto-router] Routes file generated: ${c?u.default.resolve(l.replace(".tsx","").replace(".ts",""),"./autoRouter"+("react"===e?".tsx":".ts")):l}`)},he=(e,t)=>{if(!t.has(e))return t.add(e),e;let n=1;for(;t.has(`${e}_${n}`);)n++;return`${e}_${n}`};function fe(e){try{const n=i.default.readFileSync(e,"utf-8").match(/export\s+default\s+([\s\S]*?)(?=\n\w|$)/);if(!n)return console.warn("No export default found in:",e),null;const o=n[1].trim();try{if(o.startsWith("{"))return new Function(`return ${o}`)();if(o.includes("defineConfig(")){const e=o.match(/defineConfig([\s\S]*)$/);if(e)return new Function(`return ${e[1]}`)()}return null}catch(t){return console.error("Error parsing export default content:",t),null}}catch(n){return console.error("Error reading file:",n),null}}function de(e,t){let n=u.default.resolve(e);(n.endsWith(".js")||n.endsWith(".jsx")||n.endsWith(".ts")||n.endsWith(".tsx")||n.endsWith(".vue"))&&(n=u.default.dirname(n));const o=u.default.resolve(t);return u.default.relative(n,o)}var ge=/\\/g,me=e=>e.replace(ge,"/"),Pe=(e,t)=>me(u.default.relative(t,e)),Re=/export\s+default\s+[\w(\[\]\{\})]+/;function ye(e){try{if(!i.default.existsSync(e))return console.warn(`File not found: ${e}`),!1;const t=i.default.readFileSync(e,"utf-8");return Re.test(t)}catch(t){return console.error("Read error:",t),!1}}var $e=(e,t)=>{const n=new Set;return e.some(e=>!!n.has(e[t])||(n.add(e[t]),!1))};function je(e){return e.replace(/"<start>([\s\S]*?)<\/end>"/g,"$1")}function be(e){let t=e;for(;;){const e=je(t);if(e===t)break;t=e}return t}var xe=(e,t,n)=>e.replace(/\[([^\]]+)\]/g,":$1").replace("react"===n?t?".tsx":".jsx":".vue","");function Ce(e){i.default.existsSync(e)||i.default.mkdirSync(e,{recursive:!0})}function Oe(e,t){Ce(u.default.dirname(e)),i.default.writeFileSync(e,t,"utf-8")}async function we(e,t){const n=u.default.dirname(e);await i.default.promises.mkdir(n,{recursive:!0}),await i.default.promises.writeFile(e,t,"utf-8")}var Me="index.ts",ve="index.js";Object.defineProperty(exports,"INDEX_JS_SUFFIX",{enumerable:!0,get:function(){return ve}}),Object.defineProperty(exports,"INDEX_TS_SUFFIX",{enumerable:!0,get:function(){return Me}}),Object.defineProperty(exports,"capitalize",{enumerable:!0,get:function(){return x}}),Object.defineProperty(exports,"convertFunctionToArrow",{enumerable:!0,get:function(){return p}}),Object.defineProperty(exports,"deleteDir",{enumerable:!0,get:function(){return d}}),Object.defineProperty(exports,"deleteFile",{enumerable:!0,get:function(){return f}}),Object.defineProperty(exports,"ensureDirSync",{enumerable:!0,get:function(){return Ce}}),Object.defineProperty(exports,"formatFunction",{enumerable:!0,get:function(){return g}}),Object.defineProperty(exports,"formatPagesDir",{enumerable:!0,get:function(){return m}}),Object.defineProperty(exports,"formatRoutesFile",{enumerable:!0,get:function(){return y}}),Object.defineProperty(exports,"generateComponentName",{enumerable:!0,get:function(){return C}}),Object.defineProperty(exports,"generateReactRoutes",{enumerable:!0,get:function(){return H}}),Object.defineProperty(exports,"generateReactRoutesHMR",{enumerable:!0,get:function(){return U}}),Object.defineProperty(exports,"generateRoutesTemplate",{enumerable:!0,get:function(){return le}}),Object.defineProperty(exports,"generateUniqueComponentName",{enumerable:!0,get:function(){return he}}),Object.defineProperty(exports,"generateVueRoutes",{enumerable:!0,get:function(){return ce}}),Object.defineProperty(exports,"generateVueRoutesHMR",{enumerable:!0,get:function(){return pe}}),Object.defineProperty(exports,"getDefaultExportContent",{enumerable:!0,get:function(){return fe}}),Object.defineProperty(exports,"getRelativePath",{enumerable:!0,get:function(){return de}}),Object.defineProperty(exports,"getRelativeProjectPath",{enumerable:!0,get:function(){return Pe}}),Object.defineProperty(exports,"hasDefaultExport",{enumerable:!0,get:function(){return ye}}),Object.defineProperty(exports,"hasDuplicateProperty",{enumerable:!0,get:function(){return $e}}),Object.defineProperty(exports,"normalizePathWithWindows",{enumerable:!0,get:function(){return me}}),Object.defineProperty(exports,"removeCustomTagFunctionFromJSXAll",{enumerable:!0,get:function(){return be}}),Object.defineProperty(exports,"replaceBracketsWithColon",{enumerable:!0,get:function(){return xe}}),Object.defineProperty(exports,"useTs",{enumerable:!0,get:function(){return P}}),Object.defineProperty(exports,"writeFile",{enumerable:!0,get:function(){return we}}),Object.defineProperty(exports,"writeFileSync",{enumerable:!0,get:function(){return Oe}});
|