vitarx-router 4.0.0-beta.2 → 4.0.0-beta.21

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.
Files changed (93) hide show
  1. package/README.md +42 -17
  2. package/dist/{plugin-vite/auto-routes → auto-routes}/handleHotUpdate.d.ts +1 -1
  3. package/dist/components/RouterView.js +5 -4
  4. package/dist/core/common/constant.d.ts +5 -6
  5. package/dist/core/common/constant.js +5 -6
  6. package/dist/core/common/utils.js +2 -1
  7. package/dist/core/router/checkOptions.d.ts +11 -0
  8. package/dist/core/router/checkOptions.js +119 -0
  9. package/dist/core/router/manager.js +27 -23
  10. package/dist/core/router/router.d.ts +154 -1
  11. package/dist/core/router/router.js +303 -230
  12. package/dist/core/router/web.d.ts +13 -0
  13. package/dist/core/router/web.js +35 -4
  14. package/dist/core/shared/link.d.ts +7 -0
  15. package/dist/core/shared/link.js +11 -8
  16. package/dist/core/shared/route.js +1 -2
  17. package/dist/core/shared/router.d.ts +3 -3
  18. package/dist/core/shared/router.js +7 -4
  19. package/dist/core/types/options.d.ts +2 -0
  20. package/dist/file-router/config/index.d.ts +2 -1
  21. package/dist/file-router/config/index.js +2 -1
  22. package/dist/file-router/config/resolve.d.ts +43 -0
  23. package/dist/file-router/config/resolve.js +69 -0
  24. package/dist/file-router/{utils/validateOptions.d.ts → config/validate.d.ts} +11 -10
  25. package/dist/file-router/config/validate.js +280 -0
  26. package/dist/file-router/constants.d.ts +12 -2
  27. package/dist/file-router/constants.js +13 -3
  28. package/dist/file-router/generator/generateRoutes.d.ts +44 -13
  29. package/dist/file-router/generator/generateRoutes.js +159 -80
  30. package/dist/file-router/generator/generateTypes.d.ts +3 -29
  31. package/dist/file-router/generator/generateTypes.js +36 -41
  32. package/dist/file-router/global.d.ts +1 -1
  33. package/dist/file-router/index.d.ts +224 -90
  34. package/dist/file-router/index.js +571 -135
  35. package/dist/file-router/macros/astValueExtractor.d.ts +1 -1
  36. package/dist/file-router/macros/astValueExtractor.js +27 -7
  37. package/dist/file-router/macros/definePage.d.ts +20 -3
  38. package/dist/file-router/macros/definePage.js +120 -40
  39. package/dist/file-router/parser/exportChecker.d.ts +4 -23
  40. package/dist/file-router/parser/exportChecker.js +38 -79
  41. package/dist/file-router/parser/filterUtils.d.ts +25 -0
  42. package/dist/file-router/parser/filterUtils.js +43 -0
  43. package/dist/file-router/parser/index.d.ts +2 -1
  44. package/dist/file-router/parser/index.js +2 -1
  45. package/dist/file-router/parser/parsePage.d.ts +56 -9
  46. package/dist/file-router/parser/parsePage.js +194 -172
  47. package/dist/file-router/parser/routePath.d.ts +22 -0
  48. package/dist/file-router/parser/routePath.js +74 -0
  49. package/dist/file-router/types/hooks.d.ts +52 -0
  50. package/dist/file-router/types/index.d.ts +3 -0
  51. package/dist/file-router/types/index.js +1 -0
  52. package/dist/file-router/types/options.d.ts +279 -0
  53. package/dist/file-router/types/options.js +1 -0
  54. package/dist/file-router/types/route.d.ts +114 -0
  55. package/dist/file-router/types/route.js +1 -0
  56. package/dist/file-router/utils/fileReader.d.ts +11 -0
  57. package/dist/file-router/utils/fileReader.js +22 -0
  58. package/dist/file-router/utils/findRoute.d.ts +8 -0
  59. package/dist/file-router/utils/findRoute.js +22 -0
  60. package/dist/file-router/utils/index.d.ts +4 -2
  61. package/dist/file-router/utils/index.js +4 -2
  62. package/dist/file-router/utils/logger.d.ts +6 -6
  63. package/dist/file-router/utils/logger.js +44 -4
  64. package/dist/file-router/utils/pathStrategy.d.ts +28 -0
  65. package/dist/file-router/utils/{namingStrategy.js → pathStrategy.js} +18 -28
  66. package/dist/file-router/utils/pathUtils.d.ts +31 -0
  67. package/dist/file-router/utils/pathUtils.js +53 -1
  68. package/dist/plugin-vite/constant.d.ts +9 -0
  69. package/dist/plugin-vite/constant.js +9 -0
  70. package/dist/plugin-vite/index.d.ts +4 -24
  71. package/dist/plugin-vite/index.js +4 -94
  72. package/dist/plugin-vite/plugin.d.ts +86 -0
  73. package/dist/plugin-vite/plugin.js +181 -0
  74. package/dist/plugin-vite/watcher.d.ts +15 -0
  75. package/dist/plugin-vite/watcher.js +65 -0
  76. package/package.json +9 -7
  77. package/dist/file-router/config/configUtils.d.ts +0 -54
  78. package/dist/file-router/config/configUtils.js +0 -88
  79. package/dist/file-router/scanner/filterUtils.d.ts +0 -35
  80. package/dist/file-router/scanner/filterUtils.js +0 -188
  81. package/dist/file-router/scanner/index.d.ts +0 -8
  82. package/dist/file-router/scanner/index.js +0 -8
  83. package/dist/file-router/scanner/routeTreeBuilder.d.ts +0 -21
  84. package/dist/file-router/scanner/routeTreeBuilder.js +0 -312
  85. package/dist/file-router/scanner/scanPages.d.ts +0 -48
  86. package/dist/file-router/scanner/scanPages.js +0 -174
  87. package/dist/file-router/types.d.ts +0 -344
  88. package/dist/file-router/utils/namingStrategy.d.ts +0 -57
  89. package/dist/file-router/utils/validateOptions.js +0 -233
  90. /package/dist/{plugin-vite/auto-routes → auto-routes}/handleHotUpdate.js +0 -0
  91. /package/dist/{plugin-vite/auto-routes → auto-routes}/index.d.ts +0 -0
  92. /package/dist/{plugin-vite/auto-routes → auto-routes}/index.js +0 -0
  93. /package/dist/file-router/{types.js → types/hooks.js} +0 -0
@@ -1,143 +1,277 @@
1
- import { type ResolvedConfig } from './config/index.js';
2
- import type { FileRouterOptions, ParsedPage } from './types.js';
3
- export * from './utils/logger.js';
4
- export * from './types.js';
5
1
  /**
6
- * 路由生成结果
2
+ * @fileoverview 文件路由管理器
3
+ *
4
+ * 封装文件路由的核心流程,可在不同构建工具中复用。
5
+ * 支持 Vite、Webpack、Rollup 等构建工具,或在 Node.js 脚本中直接使用。
7
6
  */
8
- export interface RoutesResult {
9
- /** 生成的路由代码 */
10
- code: string;
11
- /** 页面列表 */
12
- pages: ParsedPage[];
13
- /** 路由树 */
14
- routeTree: ParsedPage[];
15
- }
7
+ import type { GeneratorResult } from '@babel/generator';
8
+ import { ResolvedConfig } from './config/index.js';
9
+ import { type GenerateResult } from './generator/index.js';
10
+ import { type FilterOptions } from './parser/index.js';
11
+ import type { FileRouterOptions, ScanNode } from './types/index.js';
12
+ export { resolvePageConfigs } from './config/resolve.js';
13
+ export * from './generator/index.js';
14
+ export { mergePageOptions } from './macros/definePage.js';
15
+ export type * from './types/index.js';
16
+ export { findRoute } from './utils/findRoute.js';
17
+ export * from './utils/logger.js';
16
18
  /**
17
- * 类型定义生成结果
19
+ * 文件监听器事件类型
20
+ *
21
+ * 对应 chokidar 文件监听器的事件类型
18
22
  */
19
- interface WriteDtsResult {
20
- /** 类型定义内容 */
21
- content: string;
22
- /** 写入路径(绝对路径) */
23
- path: string;
24
- }
23
+ export type FileWatcherEvent = 'add' | 'addDir' | 'change' | 'unlink' | 'unlinkDir';
25
24
  /**
26
25
  * 文件路由管理器
27
- *
28
- * 封装文件路由的核心流程,提供统一的 API 供不同构建工具使用。
29
- *
30
- * @example
31
- * ```typescript
32
- * // 基本使用
33
- * const router = new FileRouter({
34
- * pagesDir: 'src/pages',
35
- * extensions: ['.tsx', '.ts']
36
- * })
37
- *
38
- * router.setRoot('/project/root')
39
- * router.scan()
40
- *
41
- * // 生成路由代码
42
- * const routes = await router.generateRoutes()
43
- *
44
- * // 生成类型定义
45
- * const dts = router.generateDts()
46
- * ```
47
- *
48
- * @example
49
- * ```typescript
50
- * // 在 Vite 插件中使用
51
- * const router = new FileRouter(options)
52
- *
53
- * return {
54
- * configResolved(config) {
55
- * router.setRoot(config.root)
56
- * router.scan()
57
- * },
58
- * async load(id) {
59
- * if (id === VIRTUAL_ID) {
60
- * return (await router.generateRoutes()).code
61
- * }
62
- * }
63
- * }
64
- * ```
65
26
  */
66
27
  export declare class FileRouter {
28
+ #private;
29
+ /**
30
+ * 配置项
31
+ */
67
32
  readonly config: ResolvedConfig;
68
- private _pages;
69
- private _routeTree;
70
- private _cachedRoutesPromise;
71
33
  /**
72
34
  * 创建文件路由管理器
73
35
  *
74
36
  * @param options - 配置选项
37
+ * @param [init = true] - 是否初始化加载
75
38
  */
76
- constructor(options?: FileRouterOptions);
39
+ constructor(options?: FileRouterOptions, init?: boolean);
77
40
  /**
78
41
  * 获取项目根目录
79
42
  */
80
43
  get root(): string;
44
+ /**
45
+ * 获取节点树
46
+ */
47
+ get nodeTree(): ScanNode[];
48
+ /**
49
+ * 获取文件映射表
50
+ *
51
+ * 键为文件或目录路径,值为对应的节点对象
52
+ */
53
+ get fileMap(): Map<string, ScanNode>;
54
+ /**
55
+ * 加载/重新加载文件路由管理器
56
+ *
57
+ * @returns {FileRouter} 文件路由管理器实例
58
+ */
59
+ reload(): this;
60
+ /**
61
+ * 构建路由数组
62
+ *
63
+ * @returns 扫描到的页面文件列表
64
+ */
65
+ protected scanPages(): ScanNode[];
81
66
  /**
82
67
  * 扫描页面目录
83
68
  *
84
- * 扫描所有配置的页面目录,解析页面文件并构建路由树。
85
- * 扫描后会清除路由代码缓存。
69
+ * @param page - 页面配置
70
+ * @param parent - 父路由
71
+ * @protected
86
72
  */
87
- scan(): void;
73
+ private scanPageDir;
88
74
  /**
89
- * 清除缓存
75
+ * 处理目录
90
76
  *
91
- * 清除路由代码缓存,下次调用 generateRoutes() 会重新生成。
77
+ * @param filePath - 目录路径
78
+ * @param fileName - 目录名
79
+ * @param page - 页面配置
80
+ * @param parent - 父节点
81
+ * @private
92
82
  */
93
- invalidate(): void;
83
+ private processDir;
94
84
  /**
95
- * 检查文件是否为页面文件
85
+ * 解析分组目录的自定义路径和选项
96
86
  *
97
- * @param file - 文件绝对路径
98
- * @returns {boolean} - 是否为页面文件
87
+ * 通过 groupParser 解析目录名,支持返回字符串路径或包含路径与选项的对象。
88
+ *
89
+ * @param fileName - 目录名
90
+ * @param filePath - 目录完整路径
91
+ * @returns 解析后的路径和选项
92
+ * @private
93
+ */
94
+ private parseGroupResult;
95
+ /**
96
+ * 处理文件
97
+ *
98
+ * 根据文件类型分发到对应的处理器。
99
+ *
100
+ * @param filePath - 文件路径
101
+ * @param page - 页面配置
102
+ * @param pageMapping - 同路径路由映射
103
+ * @param parent - 父节点
104
+ * @private
105
+ */
106
+ private processFile;
107
+ /**
108
+ * 解析文件信息与类型
109
+ *
110
+ * 统一入口,避免多处重复调用 extractFileInfo + getPageType。
111
+ *
112
+ * @param filePath - 文件路径
113
+ * @param page - 页面配置
114
+ * @returns 文件信息与类型
115
+ */
116
+ private resolveFile;
117
+ /**
118
+ * 处理分组配置文件
119
+ *
120
+ * 解析 definePage 宏并合并到父路由选项中。
121
+ *
122
+ * @param filePath - 文件路径
123
+ * @param parent - 父节点
99
124
  */
100
- isPageFile(file: string): boolean;
125
+ private processConfigFile;
101
126
  /**
102
- * 获取页面列表
127
+ * 处理分组布局文件
103
128
  *
104
- * @returns - 解析后的页面列表
129
+ * 将布局组件注册到父路由的 components 中。
130
+ *
131
+ * @param filePath - 文件路径
132
+ * @param fileInfo - 文件信息
133
+ * @param parent - 父节点
134
+ */
135
+ private processLayoutFile;
136
+ /**
137
+ * 处理页面文件
138
+ *
139
+ * 解析路由路径、视图命名和页面选项,创建或合并路由节点。
140
+ *
141
+ * @param filePath - 文件路径
142
+ * @param fileInfo - 文件信息
143
+ * @param page - 页面配置
144
+ * @param pageMapping - 同路径路由映射
145
+ * @param parent - 父节点
146
+ * @param [precomputedParsed] - 预计算的解析结果,避免重复调用 parsePageFile
147
+ * @returns 新创建的路由节点,或 null(合并到已有路由时)
148
+ */
149
+ private processPageFile;
150
+ /**
151
+ * 在已有路由树中查找同路径路由
152
+ *
153
+ * 用于 addPage 场景:新增文件时需要检查是否已存在同路径的路由节点,
154
+ * 以便将命名视图合并到已有路由而非创建重复路由。
155
+ *
156
+ * @param pathKey - 标准化后的路由路径
157
+ * @param prefix - 路径前缀
158
+ * @param parent - 父节点
159
+ * @returns 同路径的路由节点,未找到返回 null
105
160
  */
106
- getPages(): ParsedPage[];
161
+ private findSameRoute;
107
162
  /**
108
- * 获取路由树
163
+ * 应用路径策略
109
164
  *
110
- * @returns - 路由树结构
165
+ * @param path - 路径
166
+ * @protected
111
167
  */
112
- getRouteTree(): ParsedPage[];
168
+ private applyPathStrategy;
113
169
  /**
114
- * 生成路由代码
170
+ * 读取文件内容
115
171
  *
116
- * @returns - 路由生成结果
172
+ * @param file - 文件绝对路径
117
173
  */
118
- generateRoutes(): Promise<RoutesResult>;
174
+ private readFile;
119
175
  /**
120
- * 生成类型定义
176
+ * 获取文件类型
177
+ *
178
+ * @param file - 文件绝对路径
179
+ * @param rawName - 文件名(不含扩展名和 @视图命名)
180
+ * @param pages - 页面配置
181
+ * @returns 文件类型
182
+ */
183
+ private getPageType;
184
+ /**
185
+ * 检查文件是否为页面文件
186
+ *
187
+ * @param file - 文件绝对路径
188
+ * @param filter - 过滤配置,默认为 `config.pages`
189
+ * @returns {boolean} - 是否为页面文件
190
+ */
191
+ isPageFile(file: string, filter?: FilterOptions | readonly FilterOptions[]): boolean;
192
+ /**
193
+ * 获取文件的完整路由路径
194
+ *
195
+ * 判断文件是否为页面文件,如果是则计算其最终生成的路由 fullPath。
196
+ * 非页面文件(布局文件、配置文件等)返回 null。
121
197
  *
122
- * @returns - 类型定义内容
198
+ * @param filePath - 文件绝对路径
199
+ * @returns 完整路由路径,非页面文件返回 null
123
200
  */
124
- generateDts(): string;
201
+ getRouteFullPath(filePath: string): string | null;
125
202
  /**
126
203
  * 写入类型定义文件
204
+ */
205
+ private writeDts;
206
+ /**
207
+ * 生成路由
127
208
  *
128
- * @param dtsPath - 类型定义文件路径,可以是绝对路径或相对于项目根目录的相对路径
129
- * @returns 写入结果信息
209
+ * @returns {GenerateResult} 生成结果,包含routes、dts、code
210
+ */
211
+ generate(): GenerateResult;
212
+ /**
213
+ * 清空生成结果
130
214
  */
131
- writeDts(dtsPath: string): WriteDtsResult;
215
+ clearGenerateResult(): void;
132
216
  /**
133
217
  * 移除 definePage 宏
134
218
  *
135
- * 在构建模式下移除页面文件中的 definePage 宏调用。
136
- * definePage 作为全局宏使用,无需导入。
219
+ * 移除页面文件中的 definePage 宏调用。
220
+ * definePage 在客户端无法运行,必须移除。
137
221
  *
138
222
  * @param code - 源代码
139
223
  * @param filePath - 文件路径
140
224
  * @returns 转换后的代码,无需转换返回 null
141
225
  */
142
- removeDefinePage(code: string, filePath: string): import("@babel/generator").GeneratorResult | null;
226
+ removeDefinePage(code: string, filePath: string): GeneratorResult | null;
227
+ /**
228
+ * 添加页面文件
229
+ *
230
+ * 根据文件类型直接调用对应处理器,避免重复类型判断和文件解析。
231
+ *
232
+ * @param filePath - 文件路径
233
+ * @returns 是否创建了新的路由节点
234
+ */
235
+ addPage(filePath: string): boolean;
236
+ /**
237
+ * 移除指定的文件或目录
238
+ *
239
+ * 通常用于在开发模式下,文件内容改变时,移除旧的路由映射,重新生成
240
+ *
241
+ * 移除成功后续手动调用 `clearGenerateResult` 方法确保下一次获取新的生成结果!
242
+ *
243
+ * @param filePath - 文件/目录路径
244
+ * @returns {boolean} - 存在则移除并返回 true,不存在则返回 false
245
+ */
246
+ removePage(filePath: string): boolean;
247
+ /**
248
+ * 更新文件
249
+ *
250
+ * 如果文件未被扫描,则会自动添加。
251
+ *
252
+ * @param filePath - 文件路径
253
+ * @returns {boolean} - 是否更新了文件
254
+ */
255
+ updatePage(filePath: string): boolean;
256
+ /**
257
+ * 处理文件变化
258
+ *
259
+ * @param eventName - 文件变化事件名
260
+ * @param path - 文件路径
261
+ * @returns {boolean} - 是否影响了路由
262
+ * @example
263
+ * ```typescript
264
+ * router.handleChange('change', '/path/to/file.ts')
265
+ * // 在vite中使用
266
+ * server.watcher.on('all', (event,path) => {
267
+ * const mod = server.moduleGraph.getModuleById(RESOLVED_ROUTES_ID)
268
+ * if (mod) {
269
+ * const result = router.handleChange(event, path)
270
+ * // 如果 handleChange 返回 true,则表示路由受到影响,需要更新模块
271
+ * if(result) server.moduleGraph.invalidateModule(mod)
272
+ * }
273
+ * })
274
+ * ```
275
+ */
276
+ handleChange(eventName: FileWatcherEvent, path: string): boolean;
143
277
  }