@meng-xi/vite-plugin 0.1.0 → 0.1.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.
Files changed (105) hide show
  1. package/README-en.md +170 -49
  2. package/README.md +149 -29
  3. package/dist/common/format/index.cjs +1 -0
  4. package/dist/common/format/index.d.cts +156 -0
  5. package/dist/common/format/index.d.mts +156 -0
  6. package/dist/common/format/index.d.ts +156 -0
  7. package/dist/common/format/index.mjs +1 -0
  8. package/dist/common/fs/index.cjs +1 -0
  9. package/dist/common/fs/index.d.cts +150 -0
  10. package/dist/common/fs/index.d.mts +150 -0
  11. package/dist/common/fs/index.d.ts +150 -0
  12. package/dist/common/fs/index.mjs +1 -0
  13. package/dist/common/html/index.cjs +2 -0
  14. package/dist/common/html/index.d.cts +109 -0
  15. package/dist/common/html/index.d.mts +109 -0
  16. package/dist/common/html/index.d.ts +109 -0
  17. package/dist/common/html/index.mjs +2 -0
  18. package/dist/common/index.cjs +1 -1
  19. package/dist/common/index.d.cts +7 -417
  20. package/dist/common/index.d.mts +7 -417
  21. package/dist/common/index.d.ts +7 -417
  22. package/dist/common/index.mjs +1 -1
  23. package/dist/common/object/index.cjs +1 -0
  24. package/dist/common/object/index.d.cts +30 -0
  25. package/dist/common/object/index.d.mts +30 -0
  26. package/dist/common/object/index.d.ts +30 -0
  27. package/dist/common/object/index.mjs +1 -0
  28. package/dist/common/script/index.cjs +1 -0
  29. package/dist/common/script/index.d.cts +54 -0
  30. package/dist/common/script/index.d.mts +54 -0
  31. package/dist/common/script/index.d.ts +54 -0
  32. package/dist/common/script/index.mjs +1 -0
  33. package/dist/common/validation/index.cjs +1 -0
  34. package/dist/common/validation/index.d.cts +93 -0
  35. package/dist/common/validation/index.d.mts +93 -0
  36. package/dist/common/validation/index.d.ts +93 -0
  37. package/dist/common/validation/index.mjs +1 -0
  38. package/dist/factory/index.cjs +1 -1
  39. package/dist/factory/index.d.cts +1 -1
  40. package/dist/factory/index.d.mts +1 -1
  41. package/dist/factory/index.d.ts +1 -1
  42. package/dist/factory/index.mjs +1 -1
  43. package/dist/index.cjs +1 -1
  44. package/dist/index.d.cts +15 -3
  45. package/dist/index.d.mts +15 -3
  46. package/dist/index.d.ts +15 -3
  47. package/dist/index.mjs +1 -1
  48. package/dist/plugins/buildProgress/index.cjs +2 -0
  49. package/dist/plugins/buildProgress/index.d.cts +187 -0
  50. package/dist/plugins/buildProgress/index.d.mts +187 -0
  51. package/dist/plugins/buildProgress/index.d.ts +187 -0
  52. package/dist/plugins/buildProgress/index.mjs +2 -0
  53. package/dist/plugins/copyFile/index.cjs +1 -0
  54. package/dist/plugins/copyFile/index.d.cts +78 -0
  55. package/dist/plugins/copyFile/index.d.mts +78 -0
  56. package/dist/plugins/copyFile/index.d.ts +78 -0
  57. package/dist/plugins/copyFile/index.mjs +1 -0
  58. package/dist/plugins/faviconManager/index.cjs +1 -0
  59. package/dist/plugins/faviconManager/index.d.cts +143 -0
  60. package/dist/plugins/faviconManager/index.d.mts +143 -0
  61. package/dist/plugins/faviconManager/index.d.ts +143 -0
  62. package/dist/plugins/faviconManager/index.mjs +1 -0
  63. package/dist/plugins/generateRouter/index.cjs +35 -0
  64. package/dist/plugins/generateRouter/index.d.cts +215 -0
  65. package/dist/plugins/generateRouter/index.d.mts +215 -0
  66. package/dist/plugins/generateRouter/index.d.ts +215 -0
  67. package/dist/plugins/generateRouter/index.mjs +35 -0
  68. package/dist/plugins/generateVersion/index.cjs +1 -0
  69. package/dist/plugins/generateVersion/index.d.cts +184 -0
  70. package/dist/plugins/generateVersion/index.d.mts +184 -0
  71. package/dist/plugins/generateVersion/index.d.ts +184 -0
  72. package/dist/plugins/generateVersion/index.mjs +1 -0
  73. package/dist/plugins/htmlInject/index.cjs +7 -0
  74. package/dist/plugins/htmlInject/index.d.cts +278 -0
  75. package/dist/plugins/htmlInject/index.d.mts +278 -0
  76. package/dist/plugins/htmlInject/index.d.ts +278 -0
  77. package/dist/plugins/htmlInject/index.mjs +7 -0
  78. package/dist/plugins/index.cjs +1 -1
  79. package/dist/plugins/index.d.cts +10 -1714
  80. package/dist/plugins/index.d.mts +10 -1714
  81. package/dist/plugins/index.d.ts +10 -1714
  82. package/dist/plugins/index.mjs +1 -1
  83. package/dist/plugins/loadingManager/index.cjs +487 -0
  84. package/dist/plugins/loadingManager/index.d.cts +769 -0
  85. package/dist/plugins/loadingManager/index.d.mts +769 -0
  86. package/dist/plugins/loadingManager/index.d.ts +769 -0
  87. package/dist/plugins/loadingManager/index.mjs +487 -0
  88. package/dist/plugins/versionUpdateChecker/index.cjs +185 -0
  89. package/dist/plugins/versionUpdateChecker/index.d.cts +200 -0
  90. package/dist/plugins/versionUpdateChecker/index.d.mts +200 -0
  91. package/dist/plugins/versionUpdateChecker/index.d.ts +200 -0
  92. package/dist/plugins/versionUpdateChecker/index.mjs +185 -0
  93. package/dist/shared/vite-plugin.Bcg6RW2N.cjs +3 -0
  94. package/dist/shared/{vite-plugin.CiHfwMiN.d.ts → vite-plugin.DRRlWY8P.d.cts} +50 -0
  95. package/dist/shared/{vite-plugin.CiHfwMiN.d.cts → vite-plugin.DRRlWY8P.d.mts} +50 -0
  96. package/dist/shared/{vite-plugin.CiHfwMiN.d.mts → vite-plugin.DRRlWY8P.d.ts} +50 -0
  97. package/dist/shared/{vite-plugin.B88RyRN8.mjs → vite-plugin.DcExl6jd.mjs} +2 -2
  98. package/package.json +72 -2
  99. package/dist/shared/vite-plugin.CawoITTT.cjs +0 -1
  100. package/dist/shared/vite-plugin.D6Law9Ke.mjs +0 -706
  101. package/dist/shared/vite-plugin.D8L9KzuW.cjs +0 -706
  102. package/dist/shared/vite-plugin.DFjf9wFM.mjs +0 -2
  103. package/dist/shared/vite-plugin.DSb6XzBn.mjs +0 -1
  104. package/dist/shared/vite-plugin.IGZeStMa.cjs +0 -3
  105. package/dist/shared/vite-plugin.Tab4qcIM.cjs +0 -2
@@ -0,0 +1,215 @@
1
+ import { BasePluginOptions, PluginFactory } from '../../factory/index.cjs';
2
+ import 'vite';
3
+ import '../../shared/vite-plugin.CLr0ttuO.cjs';
4
+ import '../../shared/vite-plugin.DRRlWY8P.cjs';
5
+
6
+ /**
7
+ * 路由元信息
8
+ */
9
+ interface RouteMeta {
10
+ /** 页面标题 */
11
+ title?: string;
12
+ /** 是否为TabBar页面 */
13
+ isTab?: boolean;
14
+ /** 是否需要登录 */
15
+ requireAuth?: boolean;
16
+ /** 自定义扩展字段 */
17
+ [key: string]: unknown;
18
+ }
19
+ /**
20
+ * 路由配置项
21
+ */
22
+ interface RouteConfig {
23
+ /** 路由路径 */
24
+ path: string;
25
+ /** 路由名称(用于命名路由导航) */
26
+ name?: string;
27
+ /** 路由元信息 */
28
+ meta?: RouteMeta;
29
+ }
30
+ /**
31
+ * uni-app pages.json 中的页面配置项
32
+ */
33
+ interface UniAppPageConfig {
34
+ /** 页面路径 */
35
+ path: string;
36
+ /** 页面样式 */
37
+ style?: {
38
+ /** 导航栏标题 */
39
+ navigationBarTitleText?: string;
40
+ /** 是否需要登录 */
41
+ requireAuth?: boolean;
42
+ /** 其他自定义属性 */
43
+ [key: string]: unknown;
44
+ };
45
+ /** 其他属性 */
46
+ [key: string]: unknown;
47
+ }
48
+ /**
49
+ * uni-app pages.json 中的 tabBar 配置
50
+ */
51
+ interface UniAppTabBarConfig {
52
+ /** tabBar 列表 */
53
+ list?: Array<{
54
+ /** 页面路径 */
55
+ pagePath: string;
56
+ /** 文字 */
57
+ text?: string;
58
+ /** 图标路径 */
59
+ iconPath?: string;
60
+ /** 选中图标路径 */
61
+ selectedIconPath?: string;
62
+ }>;
63
+ /** 其他属性 */
64
+ [key: string]: unknown;
65
+ }
66
+ /**
67
+ * uni-app pages.json 结构
68
+ */
69
+ interface UniAppPagesJson {
70
+ /** 页面列表 */
71
+ pages: UniAppPageConfig[];
72
+ /** 子包 */
73
+ subPackages?: Array<{
74
+ root: string;
75
+ pages: UniAppPageConfig[];
76
+ }>;
77
+ /** tabBar 配置 */
78
+ tabBar?: UniAppTabBarConfig;
79
+ /** 全局样式 */
80
+ globalStyle?: Record<string, unknown>;
81
+ /** 其他属性 */
82
+ [key: string]: unknown;
83
+ }
84
+ /**
85
+ * 输出文件格式类型
86
+ */
87
+ type OutputFormat = 'ts' | 'js';
88
+ /**
89
+ * 路由名称生成策略
90
+ */
91
+ type NameStrategy = 'path' | 'camelCase' | 'pascalCase' | 'custom';
92
+ /**
93
+ * 生成路由配置插件选项
94
+ */
95
+ interface GenerateRouterOptions extends BasePluginOptions {
96
+ /**
97
+ * pages.json 文件路径(相对于项目根目录)
98
+ *
99
+ * @default 'src/pages.json'
100
+ */
101
+ pagesJsonPath?: string;
102
+ /**
103
+ * 输出文件路径(相对于项目根目录)
104
+ *
105
+ * @default 'src/router.config.ts'
106
+ */
107
+ outputPath?: string;
108
+ /**
109
+ * 输出文件格式
110
+ *
111
+ * @default 'ts'
112
+ */
113
+ outputFormat?: OutputFormat;
114
+ /**
115
+ * 路由名称生成策略
116
+ *
117
+ * @default 'camelCase'
118
+ */
119
+ nameStrategy?: NameStrategy;
120
+ /**
121
+ * 自定义路由名称生成函数
122
+ *
123
+ * @param path - 页面路径
124
+ * @returns 路由名称
125
+ */
126
+ customNameGenerator?: (path: string) => string;
127
+ /**
128
+ * 是否包含子包路由
129
+ *
130
+ * @default true
131
+ */
132
+ includeSubPackages?: boolean;
133
+ /**
134
+ * 是否监听 pages.json 变化并自动重新生成
135
+ *
136
+ * @default true
137
+ */
138
+ watch?: boolean;
139
+ /**
140
+ * 额外的元信息字段映射
141
+ *
142
+ * @description 将 pages.json 中 style 的字段映射到 meta 中
143
+ * @example { 'navigationBarTitleText': 'title', 'requireAuth': 'requireAuth' }
144
+ */
145
+ metaMapping?: Record<string, string>;
146
+ /**
147
+ * 是否导出类型定义
148
+ *
149
+ * @default true
150
+ */
151
+ exportTypes?: boolean;
152
+ /**
153
+ * 是否保留用户对 routes 配置的修改
154
+ *
155
+ * @description 开启后,用户在 routes 数组中修改的字段将被保留
156
+ * @default true
157
+ */
158
+ preserveRouteChanges?: boolean;
159
+ }
160
+
161
+ /**
162
+ * 生成路由配置插件
163
+ *
164
+ * @param {GenerateRouterOptions} options - 插件配置选项
165
+ * @returns {Plugin} 一个 Vite 插件实例
166
+ *
167
+ * @example
168
+ * ```typescript
169
+ * // 基本使用 - 使用默认配置
170
+ * generateRouter()
171
+ *
172
+ * // 自定义 pages.json 路径
173
+ * generateRouter({
174
+ * pagesJsonPath: 'pages.json'
175
+ * })
176
+ *
177
+ * // 输出 JavaScript 文件
178
+ * generateRouter({
179
+ * outputFormat: 'js',
180
+ * outputPath: 'src/router.config.js'
181
+ * })
182
+ *
183
+ * // 使用帕斯卡命名策略
184
+ * generateRouter({
185
+ * nameStrategy: 'pascalCase'
186
+ * })
187
+ *
188
+ * // 自定义路由名称生成
189
+ * generateRouter({
190
+ * nameStrategy: 'custom',
191
+ * customNameGenerator: (path) => `route_${path.replace(/\//g, '_')}`
192
+ * })
193
+ *
194
+ * // 自定义元信息映射
195
+ * generateRouter({
196
+ * metaMapping: {
197
+ * navigationBarTitleText: 'title',
198
+ * requireAuth: 'requireAuth',
199
+ * customField: 'custom'
200
+ * }
201
+ * })
202
+ * ```
203
+ *
204
+ * @remarks
205
+ * 该插件会读取 uni-app 项目的 pages.json 文件,自动生成路由配置文件:
206
+ * - 支持主包和子包页面
207
+ * - 自动识别 tabBar 页面
208
+ * - 支持多种路由名称生成策略
209
+ * - 支持自定义元信息字段映射
210
+ * - 开发模式下自动监听 pages.json 变化并重新生成
211
+ */
212
+ declare const generateRouter: PluginFactory<GenerateRouterOptions, GenerateRouterOptions>;
213
+
214
+ export { generateRouter };
215
+ export type { GenerateRouterOptions, NameStrategy, OutputFormat, RouteConfig, RouteMeta, UniAppPageConfig, UniAppPagesJson, UniAppTabBarConfig };
@@ -0,0 +1,215 @@
1
+ import { BasePluginOptions, PluginFactory } from '../../factory/index.mjs';
2
+ import 'vite';
3
+ import '../../shared/vite-plugin.CLr0ttuO.mjs';
4
+ import '../../shared/vite-plugin.DRRlWY8P.mjs';
5
+
6
+ /**
7
+ * 路由元信息
8
+ */
9
+ interface RouteMeta {
10
+ /** 页面标题 */
11
+ title?: string;
12
+ /** 是否为TabBar页面 */
13
+ isTab?: boolean;
14
+ /** 是否需要登录 */
15
+ requireAuth?: boolean;
16
+ /** 自定义扩展字段 */
17
+ [key: string]: unknown;
18
+ }
19
+ /**
20
+ * 路由配置项
21
+ */
22
+ interface RouteConfig {
23
+ /** 路由路径 */
24
+ path: string;
25
+ /** 路由名称(用于命名路由导航) */
26
+ name?: string;
27
+ /** 路由元信息 */
28
+ meta?: RouteMeta;
29
+ }
30
+ /**
31
+ * uni-app pages.json 中的页面配置项
32
+ */
33
+ interface UniAppPageConfig {
34
+ /** 页面路径 */
35
+ path: string;
36
+ /** 页面样式 */
37
+ style?: {
38
+ /** 导航栏标题 */
39
+ navigationBarTitleText?: string;
40
+ /** 是否需要登录 */
41
+ requireAuth?: boolean;
42
+ /** 其他自定义属性 */
43
+ [key: string]: unknown;
44
+ };
45
+ /** 其他属性 */
46
+ [key: string]: unknown;
47
+ }
48
+ /**
49
+ * uni-app pages.json 中的 tabBar 配置
50
+ */
51
+ interface UniAppTabBarConfig {
52
+ /** tabBar 列表 */
53
+ list?: Array<{
54
+ /** 页面路径 */
55
+ pagePath: string;
56
+ /** 文字 */
57
+ text?: string;
58
+ /** 图标路径 */
59
+ iconPath?: string;
60
+ /** 选中图标路径 */
61
+ selectedIconPath?: string;
62
+ }>;
63
+ /** 其他属性 */
64
+ [key: string]: unknown;
65
+ }
66
+ /**
67
+ * uni-app pages.json 结构
68
+ */
69
+ interface UniAppPagesJson {
70
+ /** 页面列表 */
71
+ pages: UniAppPageConfig[];
72
+ /** 子包 */
73
+ subPackages?: Array<{
74
+ root: string;
75
+ pages: UniAppPageConfig[];
76
+ }>;
77
+ /** tabBar 配置 */
78
+ tabBar?: UniAppTabBarConfig;
79
+ /** 全局样式 */
80
+ globalStyle?: Record<string, unknown>;
81
+ /** 其他属性 */
82
+ [key: string]: unknown;
83
+ }
84
+ /**
85
+ * 输出文件格式类型
86
+ */
87
+ type OutputFormat = 'ts' | 'js';
88
+ /**
89
+ * 路由名称生成策略
90
+ */
91
+ type NameStrategy = 'path' | 'camelCase' | 'pascalCase' | 'custom';
92
+ /**
93
+ * 生成路由配置插件选项
94
+ */
95
+ interface GenerateRouterOptions extends BasePluginOptions {
96
+ /**
97
+ * pages.json 文件路径(相对于项目根目录)
98
+ *
99
+ * @default 'src/pages.json'
100
+ */
101
+ pagesJsonPath?: string;
102
+ /**
103
+ * 输出文件路径(相对于项目根目录)
104
+ *
105
+ * @default 'src/router.config.ts'
106
+ */
107
+ outputPath?: string;
108
+ /**
109
+ * 输出文件格式
110
+ *
111
+ * @default 'ts'
112
+ */
113
+ outputFormat?: OutputFormat;
114
+ /**
115
+ * 路由名称生成策略
116
+ *
117
+ * @default 'camelCase'
118
+ */
119
+ nameStrategy?: NameStrategy;
120
+ /**
121
+ * 自定义路由名称生成函数
122
+ *
123
+ * @param path - 页面路径
124
+ * @returns 路由名称
125
+ */
126
+ customNameGenerator?: (path: string) => string;
127
+ /**
128
+ * 是否包含子包路由
129
+ *
130
+ * @default true
131
+ */
132
+ includeSubPackages?: boolean;
133
+ /**
134
+ * 是否监听 pages.json 变化并自动重新生成
135
+ *
136
+ * @default true
137
+ */
138
+ watch?: boolean;
139
+ /**
140
+ * 额外的元信息字段映射
141
+ *
142
+ * @description 将 pages.json 中 style 的字段映射到 meta 中
143
+ * @example { 'navigationBarTitleText': 'title', 'requireAuth': 'requireAuth' }
144
+ */
145
+ metaMapping?: Record<string, string>;
146
+ /**
147
+ * 是否导出类型定义
148
+ *
149
+ * @default true
150
+ */
151
+ exportTypes?: boolean;
152
+ /**
153
+ * 是否保留用户对 routes 配置的修改
154
+ *
155
+ * @description 开启后,用户在 routes 数组中修改的字段将被保留
156
+ * @default true
157
+ */
158
+ preserveRouteChanges?: boolean;
159
+ }
160
+
161
+ /**
162
+ * 生成路由配置插件
163
+ *
164
+ * @param {GenerateRouterOptions} options - 插件配置选项
165
+ * @returns {Plugin} 一个 Vite 插件实例
166
+ *
167
+ * @example
168
+ * ```typescript
169
+ * // 基本使用 - 使用默认配置
170
+ * generateRouter()
171
+ *
172
+ * // 自定义 pages.json 路径
173
+ * generateRouter({
174
+ * pagesJsonPath: 'pages.json'
175
+ * })
176
+ *
177
+ * // 输出 JavaScript 文件
178
+ * generateRouter({
179
+ * outputFormat: 'js',
180
+ * outputPath: 'src/router.config.js'
181
+ * })
182
+ *
183
+ * // 使用帕斯卡命名策略
184
+ * generateRouter({
185
+ * nameStrategy: 'pascalCase'
186
+ * })
187
+ *
188
+ * // 自定义路由名称生成
189
+ * generateRouter({
190
+ * nameStrategy: 'custom',
191
+ * customNameGenerator: (path) => `route_${path.replace(/\//g, '_')}`
192
+ * })
193
+ *
194
+ * // 自定义元信息映射
195
+ * generateRouter({
196
+ * metaMapping: {
197
+ * navigationBarTitleText: 'title',
198
+ * requireAuth: 'requireAuth',
199
+ * customField: 'custom'
200
+ * }
201
+ * })
202
+ * ```
203
+ *
204
+ * @remarks
205
+ * 该插件会读取 uni-app 项目的 pages.json 文件,自动生成路由配置文件:
206
+ * - 支持主包和子包页面
207
+ * - 自动识别 tabBar 页面
208
+ * - 支持多种路由名称生成策略
209
+ * - 支持自定义元信息字段映射
210
+ * - 开发模式下自动监听 pages.json 变化并重新生成
211
+ */
212
+ declare const generateRouter: PluginFactory<GenerateRouterOptions, GenerateRouterOptions>;
213
+
214
+ export { generateRouter };
215
+ export type { GenerateRouterOptions, NameStrategy, OutputFormat, RouteConfig, RouteMeta, UniAppPageConfig, UniAppPagesJson, UniAppTabBarConfig };
@@ -0,0 +1,215 @@
1
+ import { BasePluginOptions, PluginFactory } from '../../factory/index.js';
2
+ import 'vite';
3
+ import '../../shared/vite-plugin.CLr0ttuO.js';
4
+ import '../../shared/vite-plugin.DRRlWY8P.js';
5
+
6
+ /**
7
+ * 路由元信息
8
+ */
9
+ interface RouteMeta {
10
+ /** 页面标题 */
11
+ title?: string;
12
+ /** 是否为TabBar页面 */
13
+ isTab?: boolean;
14
+ /** 是否需要登录 */
15
+ requireAuth?: boolean;
16
+ /** 自定义扩展字段 */
17
+ [key: string]: unknown;
18
+ }
19
+ /**
20
+ * 路由配置项
21
+ */
22
+ interface RouteConfig {
23
+ /** 路由路径 */
24
+ path: string;
25
+ /** 路由名称(用于命名路由导航) */
26
+ name?: string;
27
+ /** 路由元信息 */
28
+ meta?: RouteMeta;
29
+ }
30
+ /**
31
+ * uni-app pages.json 中的页面配置项
32
+ */
33
+ interface UniAppPageConfig {
34
+ /** 页面路径 */
35
+ path: string;
36
+ /** 页面样式 */
37
+ style?: {
38
+ /** 导航栏标题 */
39
+ navigationBarTitleText?: string;
40
+ /** 是否需要登录 */
41
+ requireAuth?: boolean;
42
+ /** 其他自定义属性 */
43
+ [key: string]: unknown;
44
+ };
45
+ /** 其他属性 */
46
+ [key: string]: unknown;
47
+ }
48
+ /**
49
+ * uni-app pages.json 中的 tabBar 配置
50
+ */
51
+ interface UniAppTabBarConfig {
52
+ /** tabBar 列表 */
53
+ list?: Array<{
54
+ /** 页面路径 */
55
+ pagePath: string;
56
+ /** 文字 */
57
+ text?: string;
58
+ /** 图标路径 */
59
+ iconPath?: string;
60
+ /** 选中图标路径 */
61
+ selectedIconPath?: string;
62
+ }>;
63
+ /** 其他属性 */
64
+ [key: string]: unknown;
65
+ }
66
+ /**
67
+ * uni-app pages.json 结构
68
+ */
69
+ interface UniAppPagesJson {
70
+ /** 页面列表 */
71
+ pages: UniAppPageConfig[];
72
+ /** 子包 */
73
+ subPackages?: Array<{
74
+ root: string;
75
+ pages: UniAppPageConfig[];
76
+ }>;
77
+ /** tabBar 配置 */
78
+ tabBar?: UniAppTabBarConfig;
79
+ /** 全局样式 */
80
+ globalStyle?: Record<string, unknown>;
81
+ /** 其他属性 */
82
+ [key: string]: unknown;
83
+ }
84
+ /**
85
+ * 输出文件格式类型
86
+ */
87
+ type OutputFormat = 'ts' | 'js';
88
+ /**
89
+ * 路由名称生成策略
90
+ */
91
+ type NameStrategy = 'path' | 'camelCase' | 'pascalCase' | 'custom';
92
+ /**
93
+ * 生成路由配置插件选项
94
+ */
95
+ interface GenerateRouterOptions extends BasePluginOptions {
96
+ /**
97
+ * pages.json 文件路径(相对于项目根目录)
98
+ *
99
+ * @default 'src/pages.json'
100
+ */
101
+ pagesJsonPath?: string;
102
+ /**
103
+ * 输出文件路径(相对于项目根目录)
104
+ *
105
+ * @default 'src/router.config.ts'
106
+ */
107
+ outputPath?: string;
108
+ /**
109
+ * 输出文件格式
110
+ *
111
+ * @default 'ts'
112
+ */
113
+ outputFormat?: OutputFormat;
114
+ /**
115
+ * 路由名称生成策略
116
+ *
117
+ * @default 'camelCase'
118
+ */
119
+ nameStrategy?: NameStrategy;
120
+ /**
121
+ * 自定义路由名称生成函数
122
+ *
123
+ * @param path - 页面路径
124
+ * @returns 路由名称
125
+ */
126
+ customNameGenerator?: (path: string) => string;
127
+ /**
128
+ * 是否包含子包路由
129
+ *
130
+ * @default true
131
+ */
132
+ includeSubPackages?: boolean;
133
+ /**
134
+ * 是否监听 pages.json 变化并自动重新生成
135
+ *
136
+ * @default true
137
+ */
138
+ watch?: boolean;
139
+ /**
140
+ * 额外的元信息字段映射
141
+ *
142
+ * @description 将 pages.json 中 style 的字段映射到 meta 中
143
+ * @example { 'navigationBarTitleText': 'title', 'requireAuth': 'requireAuth' }
144
+ */
145
+ metaMapping?: Record<string, string>;
146
+ /**
147
+ * 是否导出类型定义
148
+ *
149
+ * @default true
150
+ */
151
+ exportTypes?: boolean;
152
+ /**
153
+ * 是否保留用户对 routes 配置的修改
154
+ *
155
+ * @description 开启后,用户在 routes 数组中修改的字段将被保留
156
+ * @default true
157
+ */
158
+ preserveRouteChanges?: boolean;
159
+ }
160
+
161
+ /**
162
+ * 生成路由配置插件
163
+ *
164
+ * @param {GenerateRouterOptions} options - 插件配置选项
165
+ * @returns {Plugin} 一个 Vite 插件实例
166
+ *
167
+ * @example
168
+ * ```typescript
169
+ * // 基本使用 - 使用默认配置
170
+ * generateRouter()
171
+ *
172
+ * // 自定义 pages.json 路径
173
+ * generateRouter({
174
+ * pagesJsonPath: 'pages.json'
175
+ * })
176
+ *
177
+ * // 输出 JavaScript 文件
178
+ * generateRouter({
179
+ * outputFormat: 'js',
180
+ * outputPath: 'src/router.config.js'
181
+ * })
182
+ *
183
+ * // 使用帕斯卡命名策略
184
+ * generateRouter({
185
+ * nameStrategy: 'pascalCase'
186
+ * })
187
+ *
188
+ * // 自定义路由名称生成
189
+ * generateRouter({
190
+ * nameStrategy: 'custom',
191
+ * customNameGenerator: (path) => `route_${path.replace(/\//g, '_')}`
192
+ * })
193
+ *
194
+ * // 自定义元信息映射
195
+ * generateRouter({
196
+ * metaMapping: {
197
+ * navigationBarTitleText: 'title',
198
+ * requireAuth: 'requireAuth',
199
+ * customField: 'custom'
200
+ * }
201
+ * })
202
+ * ```
203
+ *
204
+ * @remarks
205
+ * 该插件会读取 uni-app 项目的 pages.json 文件,自动生成路由配置文件:
206
+ * - 支持主包和子包页面
207
+ * - 自动识别 tabBar 页面
208
+ * - 支持多种路由名称生成策略
209
+ * - 支持自定义元信息字段映射
210
+ * - 开发模式下自动监听 pages.json 变化并重新生成
211
+ */
212
+ declare const generateRouter: PluginFactory<GenerateRouterOptions, GenerateRouterOptions>;
213
+
214
+ export { generateRouter };
215
+ export type { GenerateRouterOptions, NameStrategy, OutputFormat, RouteConfig, RouteMeta, UniAppPageConfig, UniAppPagesJson, UniAppTabBarConfig };
@@ -0,0 +1,35 @@
1
+ import{createPluginFactory as p,BasePlugin as h}from"../../factory/index.mjs";import{readFileContent as i,writeFileContent as l}from"../../common/fs/index.mjs";import{toCamelCase as g,toPascalCase as F,stripJsonComments as m}from"../../common/format/index.mjs";import{resolve as r}from"path";import{existsSync as n,watch as f}from"fs";import"../../logger/index.mjs";import"../../common/object/index.mjs";import"../../shared/vite-plugin.DcExl6jd.mjs";import"crypto";class E extends h{projectRoot=process.cwd();tabBarPages=new Set;watcher=null;getDefaultOptions(){return{pagesJsonPath:"src/pages.json",outputPath:"src/router.config.ts",outputFormat:"ts",nameStrategy:"camelCase",includeSubPackages:!0,watch:!0,exportTypes:!0,preserveRouteChanges:!0,metaMapping:{navigationBarTitleText:"title",requireAuth:"requireAuth"}}}validateOptions(){if(this.validator.field("pagesJsonPath").string().field("outputPath").string().field("outputFormat").enum(["ts","js"]).field("nameStrategy").enum(["path","camelCase","pascalCase","custom"]).validate(),this.options.nameStrategy==="custom"&&!this.options.customNameGenerator)throw new Error("\u5F53 nameStrategy \u4E3A custom \u65F6\uFF0C\u5FC5\u987B\u63D0\u4F9B customNameGenerator")}getPluginName(){return"generate-router"}generateRouteName(t){switch(this.options.nameStrategy){case"path":return t.replace(/\//g,"_").replace(/^_/,"");case"camelCase":return g(t);case"pascalCase":return F(t);case"custom":return this.options.customNameGenerator(t);default:return g(t)}}extractMeta(t,u){const e={},s=t.style||{},a=this.options.metaMapping||{};for(const[o,c]of Object.entries(a))s[o]!==void 0&&(e[c]=s[o]);return this.tabBarPages.has(u)&&(e.isTab=!0),e}parsePageToRoute(t,u=""){const e=u?`/${u}/${t.path}`:`/${t.path}`,s=this.generateRouteName(e),a=this.extractMeta(t,e.replace(/^\//,"")),o={path:e,name:s};return Object.keys(a).length>0&&(o.meta=a),o}parsePagesJson(t){const u=[];if(!t.pages||!Array.isArray(t.pages)||t.pages.length===0)return this.logger.warn("pages.json \u4E2D\u6CA1\u6709\u6709\u6548\u7684\u9875\u9762\u914D\u7F6E"),u;if(this.tabBarPages.clear(),t.tabBar?.list)for(const e of t.tabBar.list)this.tabBarPages.add(e.pagePath);for(const e of t.pages)u.push(this.parsePageToRoute(e));if(this.options.includeSubPackages&&t.subPackages){for(const e of t.subPackages)if(e.pages&&Array.isArray(e.pages))for(const s of e.pages)u.push(this.parsePageToRoute(s,e.root))}return u}generateTypeDefinitions(){return!this.options.exportTypes||this.options.outputFormat==="js"?"":`
2
+ /**
3
+ * \u8DEF\u7531\u5143\u4FE1\u606F
4
+ */
5
+ export interface RouteMeta {
6
+ /** \u9875\u9762\u6807\u9898 */
7
+ title?: string
8
+ /** \u662F\u5426\u4E3ATabBar\u9875\u9762 */
9
+ isTab?: boolean
10
+ /** \u662F\u5426\u9700\u8981\u767B\u5F55 */
11
+ requireAuth?: boolean
12
+ /** \u81EA\u5B9A\u4E49\u6269\u5C55\u5B57\u6BB5 */
13
+ [key: string]: unknown
14
+ }
15
+
16
+ /**
17
+ * \u8DEF\u7531\u914D\u7F6E\u9879
18
+ */
19
+ export interface RouteConfig {
20
+ /** \u8DEF\u7531\u8DEF\u5F84 */
21
+ path: string
22
+ /** \u8DEF\u7531\u540D\u79F0\uFF08\u7528\u4E8E\u547D\u540D\u8DEF\u7531\u5BFC\u822A\uFF09 */
23
+ name?: string
24
+ /** \u8DEF\u7531\u5143\u4FE1\u606F */
25
+ meta?: RouteMeta
26
+ }
27
+ `}generateFileContent(t){const u=this.generateTypeDefinitions(),e=this.options.outputFormat==="ts",s=JSON.stringify(t,null," ").replace(/"(\w+)":/g,"$1:").replace(/: "([^"]+)"/g,": '$1'");return`${u}
28
+ /**
29
+ * \u8DEF\u7531\u914D\u7F6E\u5217\u8868
30
+ * @description \u7531 pages.json \u81EA\u52A8\u751F\u6210
31
+ */
32
+ export const routes${e?": RouteConfig[]":""} = ${s}
33
+
34
+ export default routes
35
+ `}async readPagesJson(){const t=r(this.projectRoot,this.options.pagesJsonPath);if(!n(t))return this.logger.warn(`pages.json \u6587\u4EF6\u4E0D\u5B58\u5728: ${t}`),null;try{const u=await i(t),e=m(u);return JSON.parse(e)}catch(u){return this.logger.error(`\u89E3\u6790 pages.json \u5931\u8D25: ${u.message}`),null}}extractExistingRoutes(t){const u=new Map,e=t.match(/export const routes[^=]*=\s*(\[[\s\S]*?\](?=\s*\n|\s*$|\s*\/\/))/);if(!e)return u;try{let s=e[1].replace(/(\w+)(?=\s*:)/g,'"$1"').replace(/'([^']*)'/g,'"$1"').replace(/,\s*([\]\}])/g,"$1");const a=JSON.parse(s);for(const o of a)o.path&&u.set(o.path,o)}catch{this.logger.warn("\u89E3\u6790\u73B0\u6709 routes \u914D\u7F6E\u5931\u8D25\uFF0C\u5C06\u5B8C\u5168\u91CD\u65B0\u751F\u6210")}return u}mergeRoutes(t,u){return t.map(e=>{const s=u.get(e.path);if(!s)return e;const a={};return e.meta&&Object.assign(a,e.meta),s.meta&&Object.assign(a,s.meta),{...s,path:e.path,meta:Object.keys(a).length>0?a:void 0}})}async generateRouterConfig(){const t=await this.readPagesJson();if(!t)return;let u=this.parsePagesJson(t);const e=r(this.projectRoot,this.options.outputPath);if(this.options.preserveRouteChanges&&n(e))try{const a=await i(e),o=this.extractExistingRoutes(a);o.size>0&&(u=this.mergeRoutes(u,o),this.logger.info("\u5DF2\u5408\u5E76\u7528\u6237\u5BF9\u8DEF\u7531\u914D\u7F6E\u7684\u4FEE\u6539"))}catch{}const s=this.generateFileContent(u);await l(e,s),this.logger.success(`\u8DEF\u7531\u914D\u7F6E\u6587\u4EF6\u5DF2\u751F\u6210: ${e}`),this.logger.info(`\u5171\u751F\u6210 ${u.length} \u6761\u8DEF\u7531\u914D\u7F6E`)}startWatching(){if(!this.options.watch)return;const t=r(this.projectRoot,this.options.pagesJsonPath);n(t)&&(this.watcher=f(t,async u=>{u==="change"&&(this.logger.info("\u68C0\u6D4B\u5230 pages.json \u53D8\u5316\uFF0C\u91CD\u65B0\u751F\u6210\u8DEF\u7531\u914D\u7F6E..."),await this.safeExecute(()=>this.generateRouterConfig(),"\u91CD\u65B0\u751F\u6210\u8DEF\u7531\u914D\u7F6E"))}),this.logger.info(`\u6B63\u5728\u76D1\u542C pages.json \u53D8\u5316: ${t}`))}stopWatching(){this.watcher&&(this.watcher.close(),this.watcher=null)}addPluginHooks(t){t.configResolved=async u=>{this.projectRoot=u.root,await this.safeExecute(()=>this.generateRouterConfig(),"\u751F\u6210\u8DEF\u7531\u914D\u7F6E"),u.command==="serve"&&this.startWatching()}}destroy(){super.destroy(),this.stopWatching()}}const D=p(E);export{D as generateRouter};
@@ -0,0 +1 @@
1
+ "use strict";const factory_index=require("../../factory/index.cjs"),common_format_index=require("../../common/format/index.cjs"),common_fs_index=require("../../common/fs/index.cjs"),u=require("path");require("../../logger/index.cjs"),require("../../common/object/index.cjs"),require("../../shared/vite-plugin.Bcg6RW2N.cjs"),require("crypto"),require("fs");class f extends factory_index.BasePlugin{version="";buildTime=new Date;getDefaultOptions(){return{format:"timestamp",semverBase:"1.0.0",outputType:"file",outputFile:"version.json",defineName:"__APP_VERSION__",hashLength:8,prefix:"",suffix:""}}validateOptions(){if(this.validator.field("format").enum(["timestamp","date","datetime","semver","hash","custom"]).field("outputType").enum(["file","define","both"]).field("hashLength").number().minValue(1).maxValue(32).validate(),this.options.format==="custom"&&!this.options.customFormat)throw new Error("\u5F53 format \u4E3A custom \u65F6\uFF0C\u5FC5\u987B\u63D0\u4F9B customFormat")}getPluginName(){return"generate-version"}generateVersion(){const e=common_format_index.getDateFormatParams(this.buildTime),t=common_format_index.generateRandomHash(this.options.hashLength);let i;switch(this.options.format){case"timestamp":i=`${e.YYYY}${e.MM}${e.DD}${e.HH}${e.mm}${e.ss}`;break;case"date":i=`${e.YYYY}.${e.MM}.${e.DD}`;break;case"datetime":i=`${e.YYYY}.${e.MM}.${e.DD}.${e.HH}${e.mm}${e.ss}`;break;case"semver":i=this.options.semverBase||"1.0.0";break;case"hash":i=t;break;case"custom":i=this.parseCustomFormat({...e,hash:t});break;default:i=e.timestamp}const s=this.options.prefix||"",o=this.options.suffix||"";return`${s}${i}${o}`}parseCustomFormat(e){const t={...e};if(this.options.semverBase){const[i,s,o]=this.options.semverBase.split(".");t.major=i||"1",t.minor=s||"0",t.patch=o||"0"}return common_format_index.parseTemplate(this.options.customFormat||"",t)}generateVersionInfo(){return{version:this.version,buildTime:this.buildTime.toISOString(),timestamp:this.buildTime.getTime(),format:this.options.format,...this.options.extra}}async writeVersionFile(e){const t=u.join(e,this.options.outputFile||"version.json"),i=this.generateVersionInfo();await common_fs_index.writeFileContent(t,JSON.stringify(i,null,2)),this.logger.success(`\u7248\u672C\u6587\u4EF6\u5DF2\u751F\u6210: ${t}`)}addPluginHooks(e){e.configResolved=()=>{this.buildTime=new Date,this.version=this.generateVersion(),this.logger.info(`\u751F\u6210\u7248\u672C\u53F7: ${this.version}`)},(this.options.outputType==="define"||this.options.outputType==="both")&&(e.config=()=>{this.version||(this.buildTime=new Date,this.version=this.generateVersion());const t=this.options.defineName||"__APP_VERSION__";return this.logger.info(`\u6CE8\u5165\u5168\u5C40\u53D8\u91CF: ${t} = "${this.version}"`),{define:{[t]:JSON.stringify(this.version),[`${t}_INFO`]:JSON.stringify(this.generateVersionInfo())}}}),(this.options.outputType==="file"||this.options.outputType==="both")&&(e.writeBundle=async()=>{this.viteConfig&&await this.safeExecute(async()=>{const t=this.viteConfig.build.outDir;await this.writeVersionFile(t)},"\u5199\u5165\u7248\u672C\u6587\u4EF6")})}}const generateVersion=factory_index.createPluginFactory(f);exports.generateVersion=generateVersion;