vitarx-router 4.0.0-beta.8 → 4.0.0-beta.9

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.
@@ -106,6 +106,27 @@ export function buildRoutes(pages, extendRoute, parent) {
106
106
  }
107
107
  return routes;
108
108
  }
109
+ /**
110
+ * 解析组件导入表达式
111
+ *
112
+ * @param file - 组件文件路径
113
+ * @param importPath - JSON.stringify 后的文件路径
114
+ * @param mode - 解析后的导入模式('lazy' | 'sync' | 自定义表达式)
115
+ * @param importLines - 导入语句集合
116
+ * @returns 组件表达式代码
117
+ */
118
+ function resolveComponentExpr(file, importPath, mode, importLines) {
119
+ if (mode === 'sync') {
120
+ const expr = pathToUniqueName(file);
121
+ importLines.add(`import ${expr} from ${importPath}`);
122
+ return expr;
123
+ }
124
+ if (mode === 'lazy') {
125
+ importLines.add(`import { lazy } from 'vitarx'`);
126
+ return `lazy(() => import(${importPath}))`;
127
+ }
128
+ return mode;
129
+ }
109
130
  /**
110
131
  * 格式化组件表达式
111
132
  *
@@ -117,21 +138,14 @@ export function buildRoutes(pages, extendRoute, parent) {
117
138
  function formatComponent(component, importMode, importLines) {
118
139
  const entries = Object.entries(component).map(([name, file]) => {
119
140
  const importPath = JSON.stringify(file);
120
- let expr;
121
- if (importMode === 'sync') {
122
- expr = pathToUniqueName(file);
123
- importLines.add(`import ${expr} from ${importPath}`);
124
- }
125
- else if (importMode === 'lazy') {
126
- expr = `lazy(() => import(${importPath}))`;
127
- }
128
- else {
129
- expr = importMode({
141
+ const mode = typeof importMode === 'function'
142
+ ? importMode({
130
143
  importPath,
131
144
  filePath: file,
132
145
  addImport: statement => importLines.add(statement)
133
- });
134
- }
146
+ })
147
+ : importMode;
148
+ const expr = resolveComponentExpr(file, importPath, mode, importLines);
135
149
  return `${JSON.stringify(name)}: ${expr}`;
136
150
  });
137
151
  return `{ ${entries.join(', ')} }`;
@@ -221,9 +235,6 @@ function generateRouteCode(route, indent, isLast, importMode, importLines) {
221
235
  export function generateRoutesCode(routes, importMode = 'lazy', customImports, indent = ' ') {
222
236
  const importLines = new Set();
223
237
  const codeLines = [];
224
- if (importMode === 'lazy') {
225
- importLines.add(`import { lazy } from 'vitarx'`);
226
- }
227
238
  if (customImports && customImports.length > 0) {
228
239
  customImports.forEach(imp => importLines.add(imp));
229
240
  }
@@ -32,8 +32,9 @@ export declare class FileRouter {
32
32
  * 创建文件路由管理器
33
33
  *
34
34
  * @param options - 配置选项
35
+ * @param [init = true] - 是否初始化加载
35
36
  */
36
- constructor(options?: FileRouterOptions);
37
+ constructor(options?: FileRouterOptions, init?: boolean);
37
38
  /**
38
39
  * 获取项目根目录
39
40
  */
@@ -48,6 +49,12 @@ export declare class FileRouter {
48
49
  * 键为文件或目录路径,值为对应的节点对象
49
50
  */
50
51
  get fileMap(): Map<string, ParsedNode>;
52
+ /**
53
+ * 加载/重新加载文件路由管理器
54
+ *
55
+ * @returns {FileRouter} 文件路由管理器实例
56
+ */
57
+ reload(): this;
51
58
  /**
52
59
  * 构建路由数组
53
60
  *
@@ -125,10 +132,6 @@ export declare class FileRouter {
125
132
  * 清空生成结果
126
133
  */
127
134
  clearGenerateResult(): void;
128
- /**
129
- * 重新加载
130
- */
131
- reload(): void;
132
135
  /**
133
136
  * 移除 definePage 宏
134
137
  *
@@ -29,8 +29,9 @@ export class FileRouter {
29
29
  * 创建文件路由管理器
30
30
  *
31
31
  * @param options - 配置选项
32
+ * @param [init = true] - 是否初始化加载
32
33
  */
33
- constructor(options = {}) {
34
+ constructor(options = {}, init = true) {
34
35
  /**
35
36
  * 配置项
36
37
  */
@@ -65,11 +66,12 @@ export class FileRouter {
65
66
  * 创建文件路由管理器
66
67
  *
67
68
  * @param options - 配置选项
69
+ * @param [init = true] - 是否初始化加载
68
70
  */
69
71
  );
70
72
  validateOptions(options);
71
73
  this.config = resolveConfig(options);
72
- __classPrivateFieldSet(this, _FileRouter_nodeTree, this.scanPages(), "f");
74
+ __classPrivateFieldSet(this, _FileRouter_nodeTree, init ? this.scanPages() : [], "f");
73
75
  }
74
76
  /**
75
77
  * 获取项目根目录
@@ -91,6 +93,17 @@ export class FileRouter {
91
93
  get fileMap() {
92
94
  return __classPrivateFieldGet(this, _FileRouter_fileMap, "f");
93
95
  }
96
+ /**
97
+ * 加载/重新加载文件路由管理器
98
+ *
99
+ * @returns {FileRouter} 文件路由管理器实例
100
+ */
101
+ reload() {
102
+ this.clearGenerateResult();
103
+ __classPrivateFieldGet(this, _FileRouter_fileMap, "f").clear();
104
+ __classPrivateFieldSet(this, _FileRouter_nodeTree, this.scanPages(), "f");
105
+ return this;
106
+ }
94
107
  /**
95
108
  * 构建路由数组
96
109
  *
@@ -345,14 +358,6 @@ export class FileRouter {
345
358
  clearGenerateResult() {
346
359
  __classPrivateFieldSet(this, _FileRouter_generateResult, null, "f");
347
360
  }
348
- /**
349
- * 重新加载
350
- */
351
- reload() {
352
- this.clearGenerateResult();
353
- __classPrivateFieldGet(this, _FileRouter_fileMap, "f").clear();
354
- __classPrivateFieldSet(this, _FileRouter_nodeTree, this.scanPages(), "f");
355
- }
356
361
  /**
357
362
  * 移除 definePage 宏
358
363
  *
@@ -21,18 +21,22 @@ export interface ImportModeContext {
21
21
  * 自定义导入模式函数
22
22
  *
23
23
  * @param context - 导入上下文
24
- * @returns 组件表达式代码
24
+ * @returns 'lazy' | 'sync' | 表达式字符串
25
25
  *
26
26
  * @example
27
27
  * ```ts
28
- * // 自定义导入模式:使用 React.lazy
28
+ * // 返回预设模式
29
+ * (context) => 'lazy'
30
+ * (context) => 'sync'
31
+ *
32
+ * // 返回自定义表达式
29
33
  * (context) => {
30
- * context.addImport(`import { lazy } from 'react'`)
34
+ * context.addImport(`import { lazy } from 'vitarx'`)
31
35
  * return `lazy(() => import(${context.importPath}))`
32
36
  * }
33
37
  * ```
34
38
  */
35
- export type ImportModeFunction = (context: ImportModeContext) => string;
39
+ export type ImportModeFunction = (context: ImportModeContext) => 'lazy' | 'sync' | string;
36
40
  /**
37
41
  * 组件导入模式。
38
42
  *
@@ -134,7 +138,7 @@ export interface FileRouterOptions {
134
138
  *
135
139
  * // 使用自定义函数
136
140
  * importMode: (context) => {
137
- * context.addImport(`import { lazy } from 'react'`)
141
+ * context.addImport(`import { lazy } from 'vitarx'`)
138
142
  * return `lazy(() => import(${context.importPath}))`
139
143
  * }
140
144
  * ```
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "vitarx-router",
3
- "version": "4.0.0-beta.8",
3
+ "version": "4.0.0-beta.9",
4
4
  "description": "Official routing solution for Vitarx framework with declarative routing, navigation guards, dynamic routes, file-based routing with HMR, and full TypeScript support.",
5
5
  "author": "ZhuChonglin <8210856@qq.com>",
6
6
  "license": "MIT",