@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,184 @@
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
+ * @description
10
+ * - 'timestamp': 时间戳格式,如 '20260203153000'
11
+ * - 'date': 日期格式,如 '2026.02.03'
12
+ * - 'datetime': 日期时间格式,如 '2026.02.03.153000'
13
+ * - 'semver': 语义化版本格式,如 '1.0.0'
14
+ * - 'hash': 随机哈希格式,如 'a1b2c3d4'
15
+ * - 'custom': 自定义格式,需要配合 customFormat 使用
16
+ */
17
+ type VersionFormat = 'timestamp' | 'date' | 'datetime' | 'semver' | 'hash' | 'custom';
18
+ /**
19
+ * 版本号输出类型
20
+ *
21
+ * @description
22
+ * - 'file': 输出到文件
23
+ * - 'define': 通过 Vite 的 define 注入到代码中
24
+ * - 'both': 同时输出到文件和注入代码
25
+ */
26
+ type OutputType = 'file' | 'define' | 'both';
27
+ /**
28
+ * 版本信息接口
29
+ *
30
+ * @description generateVersion 插件生成的版本信息对象类型,
31
+ * 通过 define 注入到代码中后可通过 `__APP_VERSION___INFO` 全局变量访问
32
+ */
33
+ interface VersionInfo {
34
+ /** 版本号字符串 */
35
+ version: string;
36
+ /** 构建时间(ISO 8601 格式) */
37
+ buildTime: string;
38
+ /** 构建时间戳(毫秒) */
39
+ timestamp: number;
40
+ /** 版本号格式 */
41
+ format: VersionFormat;
42
+ /** 通过 extra 选项附加的自定义字段 */
43
+ [key: string]: unknown;
44
+ }
45
+ /**
46
+ * 自动生成版本号插件的配置选项接口
47
+ *
48
+ * @interface GenerateVersionOptions
49
+ */
50
+ interface GenerateVersionOptions extends BasePluginOptions {
51
+ /**
52
+ * 版本号格式
53
+ *
54
+ * @default 'timestamp'
55
+ */
56
+ format?: VersionFormat;
57
+ /**
58
+ * 自定义格式模板,仅当 format 为 'custom' 时有效
59
+ *
60
+ * @description 支持以下占位符:
61
+ * - {YYYY}: 四位年份
62
+ * - {MM}: 两位月份
63
+ * - {DD}: 两位日期
64
+ * - {HH}: 两位小时
65
+ * - {mm}: 两位分钟
66
+ * - {ss}: 两位秒数
67
+ * - {timestamp}: 时间戳
68
+ * - {hash}: 随机哈希
69
+ * - {major}: 主版本号(需配合 semverBase)
70
+ * - {minor}: 次版本号(需配合 semverBase)
71
+ * - {patch}: 补丁版本号(需配合 semverBase)
72
+ *
73
+ * @example '{YYYY}.{MM}.{DD}-{hash}'
74
+ */
75
+ customFormat?: string;
76
+ /**
77
+ * 语义化版本基础值,用于 semver 格式
78
+ *
79
+ * @default '1.0.0'
80
+ */
81
+ semverBase?: string;
82
+ /**
83
+ * 输出类型
84
+ *
85
+ * @default 'file'
86
+ */
87
+ outputType?: OutputType;
88
+ /**
89
+ * 输出文件路径(相对于构建输出目录)
90
+ *
91
+ * @default 'version.json'
92
+ */
93
+ outputFile?: string;
94
+ /**
95
+ * 注入到代码中的全局变量名
96
+ *
97
+ * @default '__APP_VERSION__'
98
+ */
99
+ defineName?: string;
100
+ /**
101
+ * 哈希长度
102
+ *
103
+ * @default 8
104
+ */
105
+ hashLength?: number;
106
+ /**
107
+ * 版本号前缀
108
+ *
109
+ * @example 'v'
110
+ */
111
+ prefix?: string;
112
+ /**
113
+ * 版本号后缀
114
+ *
115
+ * @example '-beta'
116
+ */
117
+ suffix?: string;
118
+ /**
119
+ * 额外的版本信息,会包含在输出的 JSON 文件中
120
+ */
121
+ extra?: Record<string, any>;
122
+ }
123
+
124
+ /**
125
+ * 自动生成版本号插件
126
+ *
127
+ * @param {GenerateVersionOptions} options - 插件配置选项
128
+ * @returns {Plugin} 一个 Vite 插件实例
129
+ *
130
+ * @example
131
+ * ```typescript
132
+ * // 基本使用 - 时间戳格式
133
+ * generateVersion()
134
+ *
135
+ * // 日期格式
136
+ * generateVersion({
137
+ * format: 'date'
138
+ * })
139
+ *
140
+ * // 语义化版本格式
141
+ * generateVersion({
142
+ * format: 'semver',
143
+ * semverBase: '2.0.0',
144
+ * prefix: 'v'
145
+ * })
146
+ *
147
+ * // 自定义格式
148
+ * generateVersion({
149
+ * format: 'custom',
150
+ * customFormat: '{YYYY}.{MM}.{DD}-{hash}',
151
+ * hashLength: 6
152
+ * })
153
+ *
154
+ * // 注入到代码中
155
+ * generateVersion({
156
+ * outputType: 'define',
157
+ * defineName: '__VERSION__'
158
+ * })
159
+ *
160
+ * // 同时输出文件和注入代码
161
+ * generateVersion({
162
+ * outputType: 'both',
163
+ * outputFile: 'build-info.json',
164
+ * defineName: '__BUILD_VERSION__',
165
+ * extra: {
166
+ * environment: 'production',
167
+ * author: 'MengXi Studio'
168
+ * }
169
+ * })
170
+ * ```
171
+ *
172
+ * @remarks
173
+ * 该插件会在 Vite 构建过程中自动生成版本号,支持多种格式:
174
+ * - timestamp: 时间戳格式 (20260203153000)
175
+ * - date: 日期格式 (2026.02.03)
176
+ * - datetime: 日期时间格式 (2026.02.03.153000)
177
+ * - semver: 语义化版本格式 (1.0.0)
178
+ * - hash: 随机哈希格式 (a1b2c3d4)
179
+ * - custom: 自定义格式
180
+ */
181
+ declare const generateVersion: PluginFactory<GenerateVersionOptions, GenerateVersionOptions>;
182
+
183
+ export { generateVersion };
184
+ export type { GenerateVersionOptions, OutputType, VersionFormat, VersionInfo };
@@ -0,0 +1,184 @@
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
+ * @description
10
+ * - 'timestamp': 时间戳格式,如 '20260203153000'
11
+ * - 'date': 日期格式,如 '2026.02.03'
12
+ * - 'datetime': 日期时间格式,如 '2026.02.03.153000'
13
+ * - 'semver': 语义化版本格式,如 '1.0.0'
14
+ * - 'hash': 随机哈希格式,如 'a1b2c3d4'
15
+ * - 'custom': 自定义格式,需要配合 customFormat 使用
16
+ */
17
+ type VersionFormat = 'timestamp' | 'date' | 'datetime' | 'semver' | 'hash' | 'custom';
18
+ /**
19
+ * 版本号输出类型
20
+ *
21
+ * @description
22
+ * - 'file': 输出到文件
23
+ * - 'define': 通过 Vite 的 define 注入到代码中
24
+ * - 'both': 同时输出到文件和注入代码
25
+ */
26
+ type OutputType = 'file' | 'define' | 'both';
27
+ /**
28
+ * 版本信息接口
29
+ *
30
+ * @description generateVersion 插件生成的版本信息对象类型,
31
+ * 通过 define 注入到代码中后可通过 `__APP_VERSION___INFO` 全局变量访问
32
+ */
33
+ interface VersionInfo {
34
+ /** 版本号字符串 */
35
+ version: string;
36
+ /** 构建时间(ISO 8601 格式) */
37
+ buildTime: string;
38
+ /** 构建时间戳(毫秒) */
39
+ timestamp: number;
40
+ /** 版本号格式 */
41
+ format: VersionFormat;
42
+ /** 通过 extra 选项附加的自定义字段 */
43
+ [key: string]: unknown;
44
+ }
45
+ /**
46
+ * 自动生成版本号插件的配置选项接口
47
+ *
48
+ * @interface GenerateVersionOptions
49
+ */
50
+ interface GenerateVersionOptions extends BasePluginOptions {
51
+ /**
52
+ * 版本号格式
53
+ *
54
+ * @default 'timestamp'
55
+ */
56
+ format?: VersionFormat;
57
+ /**
58
+ * 自定义格式模板,仅当 format 为 'custom' 时有效
59
+ *
60
+ * @description 支持以下占位符:
61
+ * - {YYYY}: 四位年份
62
+ * - {MM}: 两位月份
63
+ * - {DD}: 两位日期
64
+ * - {HH}: 两位小时
65
+ * - {mm}: 两位分钟
66
+ * - {ss}: 两位秒数
67
+ * - {timestamp}: 时间戳
68
+ * - {hash}: 随机哈希
69
+ * - {major}: 主版本号(需配合 semverBase)
70
+ * - {minor}: 次版本号(需配合 semverBase)
71
+ * - {patch}: 补丁版本号(需配合 semverBase)
72
+ *
73
+ * @example '{YYYY}.{MM}.{DD}-{hash}'
74
+ */
75
+ customFormat?: string;
76
+ /**
77
+ * 语义化版本基础值,用于 semver 格式
78
+ *
79
+ * @default '1.0.0'
80
+ */
81
+ semverBase?: string;
82
+ /**
83
+ * 输出类型
84
+ *
85
+ * @default 'file'
86
+ */
87
+ outputType?: OutputType;
88
+ /**
89
+ * 输出文件路径(相对于构建输出目录)
90
+ *
91
+ * @default 'version.json'
92
+ */
93
+ outputFile?: string;
94
+ /**
95
+ * 注入到代码中的全局变量名
96
+ *
97
+ * @default '__APP_VERSION__'
98
+ */
99
+ defineName?: string;
100
+ /**
101
+ * 哈希长度
102
+ *
103
+ * @default 8
104
+ */
105
+ hashLength?: number;
106
+ /**
107
+ * 版本号前缀
108
+ *
109
+ * @example 'v'
110
+ */
111
+ prefix?: string;
112
+ /**
113
+ * 版本号后缀
114
+ *
115
+ * @example '-beta'
116
+ */
117
+ suffix?: string;
118
+ /**
119
+ * 额外的版本信息,会包含在输出的 JSON 文件中
120
+ */
121
+ extra?: Record<string, any>;
122
+ }
123
+
124
+ /**
125
+ * 自动生成版本号插件
126
+ *
127
+ * @param {GenerateVersionOptions} options - 插件配置选项
128
+ * @returns {Plugin} 一个 Vite 插件实例
129
+ *
130
+ * @example
131
+ * ```typescript
132
+ * // 基本使用 - 时间戳格式
133
+ * generateVersion()
134
+ *
135
+ * // 日期格式
136
+ * generateVersion({
137
+ * format: 'date'
138
+ * })
139
+ *
140
+ * // 语义化版本格式
141
+ * generateVersion({
142
+ * format: 'semver',
143
+ * semverBase: '2.0.0',
144
+ * prefix: 'v'
145
+ * })
146
+ *
147
+ * // 自定义格式
148
+ * generateVersion({
149
+ * format: 'custom',
150
+ * customFormat: '{YYYY}.{MM}.{DD}-{hash}',
151
+ * hashLength: 6
152
+ * })
153
+ *
154
+ * // 注入到代码中
155
+ * generateVersion({
156
+ * outputType: 'define',
157
+ * defineName: '__VERSION__'
158
+ * })
159
+ *
160
+ * // 同时输出文件和注入代码
161
+ * generateVersion({
162
+ * outputType: 'both',
163
+ * outputFile: 'build-info.json',
164
+ * defineName: '__BUILD_VERSION__',
165
+ * extra: {
166
+ * environment: 'production',
167
+ * author: 'MengXi Studio'
168
+ * }
169
+ * })
170
+ * ```
171
+ *
172
+ * @remarks
173
+ * 该插件会在 Vite 构建过程中自动生成版本号,支持多种格式:
174
+ * - timestamp: 时间戳格式 (20260203153000)
175
+ * - date: 日期格式 (2026.02.03)
176
+ * - datetime: 日期时间格式 (2026.02.03.153000)
177
+ * - semver: 语义化版本格式 (1.0.0)
178
+ * - hash: 随机哈希格式 (a1b2c3d4)
179
+ * - custom: 自定义格式
180
+ */
181
+ declare const generateVersion: PluginFactory<GenerateVersionOptions, GenerateVersionOptions>;
182
+
183
+ export { generateVersion };
184
+ export type { GenerateVersionOptions, OutputType, VersionFormat, VersionInfo };
@@ -0,0 +1,184 @@
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
+ * @description
10
+ * - 'timestamp': 时间戳格式,如 '20260203153000'
11
+ * - 'date': 日期格式,如 '2026.02.03'
12
+ * - 'datetime': 日期时间格式,如 '2026.02.03.153000'
13
+ * - 'semver': 语义化版本格式,如 '1.0.0'
14
+ * - 'hash': 随机哈希格式,如 'a1b2c3d4'
15
+ * - 'custom': 自定义格式,需要配合 customFormat 使用
16
+ */
17
+ type VersionFormat = 'timestamp' | 'date' | 'datetime' | 'semver' | 'hash' | 'custom';
18
+ /**
19
+ * 版本号输出类型
20
+ *
21
+ * @description
22
+ * - 'file': 输出到文件
23
+ * - 'define': 通过 Vite 的 define 注入到代码中
24
+ * - 'both': 同时输出到文件和注入代码
25
+ */
26
+ type OutputType = 'file' | 'define' | 'both';
27
+ /**
28
+ * 版本信息接口
29
+ *
30
+ * @description generateVersion 插件生成的版本信息对象类型,
31
+ * 通过 define 注入到代码中后可通过 `__APP_VERSION___INFO` 全局变量访问
32
+ */
33
+ interface VersionInfo {
34
+ /** 版本号字符串 */
35
+ version: string;
36
+ /** 构建时间(ISO 8601 格式) */
37
+ buildTime: string;
38
+ /** 构建时间戳(毫秒) */
39
+ timestamp: number;
40
+ /** 版本号格式 */
41
+ format: VersionFormat;
42
+ /** 通过 extra 选项附加的自定义字段 */
43
+ [key: string]: unknown;
44
+ }
45
+ /**
46
+ * 自动生成版本号插件的配置选项接口
47
+ *
48
+ * @interface GenerateVersionOptions
49
+ */
50
+ interface GenerateVersionOptions extends BasePluginOptions {
51
+ /**
52
+ * 版本号格式
53
+ *
54
+ * @default 'timestamp'
55
+ */
56
+ format?: VersionFormat;
57
+ /**
58
+ * 自定义格式模板,仅当 format 为 'custom' 时有效
59
+ *
60
+ * @description 支持以下占位符:
61
+ * - {YYYY}: 四位年份
62
+ * - {MM}: 两位月份
63
+ * - {DD}: 两位日期
64
+ * - {HH}: 两位小时
65
+ * - {mm}: 两位分钟
66
+ * - {ss}: 两位秒数
67
+ * - {timestamp}: 时间戳
68
+ * - {hash}: 随机哈希
69
+ * - {major}: 主版本号(需配合 semverBase)
70
+ * - {minor}: 次版本号(需配合 semverBase)
71
+ * - {patch}: 补丁版本号(需配合 semverBase)
72
+ *
73
+ * @example '{YYYY}.{MM}.{DD}-{hash}'
74
+ */
75
+ customFormat?: string;
76
+ /**
77
+ * 语义化版本基础值,用于 semver 格式
78
+ *
79
+ * @default '1.0.0'
80
+ */
81
+ semverBase?: string;
82
+ /**
83
+ * 输出类型
84
+ *
85
+ * @default 'file'
86
+ */
87
+ outputType?: OutputType;
88
+ /**
89
+ * 输出文件路径(相对于构建输出目录)
90
+ *
91
+ * @default 'version.json'
92
+ */
93
+ outputFile?: string;
94
+ /**
95
+ * 注入到代码中的全局变量名
96
+ *
97
+ * @default '__APP_VERSION__'
98
+ */
99
+ defineName?: string;
100
+ /**
101
+ * 哈希长度
102
+ *
103
+ * @default 8
104
+ */
105
+ hashLength?: number;
106
+ /**
107
+ * 版本号前缀
108
+ *
109
+ * @example 'v'
110
+ */
111
+ prefix?: string;
112
+ /**
113
+ * 版本号后缀
114
+ *
115
+ * @example '-beta'
116
+ */
117
+ suffix?: string;
118
+ /**
119
+ * 额外的版本信息,会包含在输出的 JSON 文件中
120
+ */
121
+ extra?: Record<string, any>;
122
+ }
123
+
124
+ /**
125
+ * 自动生成版本号插件
126
+ *
127
+ * @param {GenerateVersionOptions} options - 插件配置选项
128
+ * @returns {Plugin} 一个 Vite 插件实例
129
+ *
130
+ * @example
131
+ * ```typescript
132
+ * // 基本使用 - 时间戳格式
133
+ * generateVersion()
134
+ *
135
+ * // 日期格式
136
+ * generateVersion({
137
+ * format: 'date'
138
+ * })
139
+ *
140
+ * // 语义化版本格式
141
+ * generateVersion({
142
+ * format: 'semver',
143
+ * semverBase: '2.0.0',
144
+ * prefix: 'v'
145
+ * })
146
+ *
147
+ * // 自定义格式
148
+ * generateVersion({
149
+ * format: 'custom',
150
+ * customFormat: '{YYYY}.{MM}.{DD}-{hash}',
151
+ * hashLength: 6
152
+ * })
153
+ *
154
+ * // 注入到代码中
155
+ * generateVersion({
156
+ * outputType: 'define',
157
+ * defineName: '__VERSION__'
158
+ * })
159
+ *
160
+ * // 同时输出文件和注入代码
161
+ * generateVersion({
162
+ * outputType: 'both',
163
+ * outputFile: 'build-info.json',
164
+ * defineName: '__BUILD_VERSION__',
165
+ * extra: {
166
+ * environment: 'production',
167
+ * author: 'MengXi Studio'
168
+ * }
169
+ * })
170
+ * ```
171
+ *
172
+ * @remarks
173
+ * 该插件会在 Vite 构建过程中自动生成版本号,支持多种格式:
174
+ * - timestamp: 时间戳格式 (20260203153000)
175
+ * - date: 日期格式 (2026.02.03)
176
+ * - datetime: 日期时间格式 (2026.02.03.153000)
177
+ * - semver: 语义化版本格式 (1.0.0)
178
+ * - hash: 随机哈希格式 (a1b2c3d4)
179
+ * - custom: 自定义格式
180
+ */
181
+ declare const generateVersion: PluginFactory<GenerateVersionOptions, GenerateVersionOptions>;
182
+
183
+ export { generateVersion };
184
+ export type { GenerateVersionOptions, OutputType, VersionFormat, VersionInfo };
@@ -0,0 +1 @@
1
+ import{createPluginFactory as n,BasePlugin as r}from"../../factory/index.mjs";import{getDateFormatParams as a,generateRandomHash as u,parseTemplate as m}from"../../common/format/index.mjs";import{writeFileContent as h}from"../../common/fs/index.mjs";import{join as p}from"path";import"../../logger/index.mjs";import"../../common/object/index.mjs";import"../../shared/vite-plugin.DcExl6jd.mjs";import"crypto";import"fs";class f extends r{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 t=a(this.buildTime),e=u(this.options.hashLength);let i;switch(this.options.format){case"timestamp":i=`${t.YYYY}${t.MM}${t.DD}${t.HH}${t.mm}${t.ss}`;break;case"date":i=`${t.YYYY}.${t.MM}.${t.DD}`;break;case"datetime":i=`${t.YYYY}.${t.MM}.${t.DD}.${t.HH}${t.mm}${t.ss}`;break;case"semver":i=this.options.semverBase||"1.0.0";break;case"hash":i=e;break;case"custom":i=this.parseCustomFormat({...t,hash:e});break;default:i=t.timestamp}const s=this.options.prefix||"",o=this.options.suffix||"";return`${s}${i}${o}`}parseCustomFormat(t){const e={...t};if(this.options.semverBase){const[i,s,o]=this.options.semverBase.split(".");e.major=i||"1",e.minor=s||"0",e.patch=o||"0"}return m(this.options.customFormat||"",e)}generateVersionInfo(){return{version:this.version,buildTime:this.buildTime.toISOString(),timestamp:this.buildTime.getTime(),format:this.options.format,...this.options.extra}}async writeVersionFile(t){const e=p(t,this.options.outputFile||"version.json"),i=this.generateVersionInfo();await h(e,JSON.stringify(i,null,2)),this.logger.success(`\u7248\u672C\u6587\u4EF6\u5DF2\u751F\u6210: ${e}`)}addPluginHooks(t){t.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")&&(t.config=()=>{this.version||(this.buildTime=new Date,this.version=this.generateVersion());const e=this.options.defineName||"__APP_VERSION__";return this.logger.info(`\u6CE8\u5165\u5168\u5C40\u53D8\u91CF: ${e} = "${this.version}"`),{define:{[e]:JSON.stringify(this.version),[`${e}_INFO`]:JSON.stringify(this.generateVersionInfo())}}}),(this.options.outputType==="file"||this.options.outputType==="both")&&(t.writeBundle=async()=>{this.viteConfig&&await this.safeExecute(async()=>{const e=this.viteConfig.build.outDir;await this.writeVersionFile(e)},"\u5199\u5165\u7248\u672C\u6587\u4EF6")})}}const l=n(f);export{l as generateVersion};
@@ -0,0 +1,7 @@
1
+ "use strict";const factory_index=require("../../factory/index.cjs"),common_script_index=require("../../common/script/index.cjs"),r=require("node:path");require("../../logger/index.cjs"),require("../../common/object/index.cjs"),require("../../shared/vite-plugin.Bcg6RW2N.cjs");function _interopDefaultCompat(e){return e&&typeof e=="object"&&"default"in e?e.default:e}const r__default=_interopDefaultCompat(r),g$1=["script","iframe","object","embed","applet","form","input","textarea","select","button"],b=["onclick","ondblclick","onmouseover","onmouseout","onmousemove","onmousedown","onmouseup","onkeydown","onkeyup","onkeypress","onload","onerror","onfocus","onblur","onsubmit","onchange","oninput","oncontextmenu","ondrag","ondrop","onanimationend","ontransitionend"];function validateRules(e){if(!e.rules||!Array.isArray(e.rules))throw new Error("rules \u5FC5\u987B\u662F\u975E\u7A7A\u6570\u7EC4");if(e.rules.length===0)throw new Error("rules \u4E0D\u80FD\u4E3A\u7A7A\u6570\u7EC4");for(let t=0;t<e.rules.length;t++)m(e.rules[t],t)}function m(e,t){if(!e.content||typeof e.content!="string")throw new Error(`rules[${t}].content \u5FC5\u987B\u662F\u975E\u7A7A\u5B57\u7B26\u4E32`);if(!e.position||typeof e.position!="string")throw new Error(`rules[${t}].position \u5FC5\u987B\u662F\u6709\u6548\u7684\u6CE8\u5165\u4F4D\u7F6E`);const n=["head-start","head-end","body-start","body-end","before-selector","after-selector","replace-selector"];if(!n.includes(e.position))throw new Error(`rules[${t}].position \u5FC5\u987B\u662F ${n.join(", ")} \u4E4B\u4E00`);if(e.position.includes("selector")&&!e.selector)throw new Error(`rules[${t}].position \u4E3A "${e.position}" \u65F6\uFF0Cselector \u4E3A\u5FC5\u586B\u9879`);if(e.priority!==void 0&&(typeof e.priority!="number"||e.priority<0))throw new Error(`rules[${t}].priority \u5FC5\u987B\u662F\u975E\u8D1F\u6570`);e.condition&&y(e.condition,t)}function y(e,t){if(!e)return;const n=["env","file-contains","custom"];if(!n.includes(e.type))throw new Error(`rules[${t}].condition.type \u5FC5\u987B\u662F ${n.join(", ")} \u4E4B\u4E00`);if(e.type==="custom"&&typeof e.value!="function")throw new Error(`rules[${t}].condition.type \u4E3A "custom" \u65F6\uFF0Cvalue \u5FC5\u987B\u662F\u51FD\u6570`);if(e.type!=="custom"&&typeof e.value!="string")throw new Error(`rules[${t}].condition.type \u4E3A "${e.type}" \u65F6\uFF0Cvalue \u5FC5\u987B\u662F\u5B57\u7B26\u4E32`)}function validateSecurityConfig(e){if(e){if(e.blockedTags&&!Array.isArray(e.blockedTags))throw new Error("security.blockedTags \u5FC5\u987B\u662F\u5B57\u7B26\u4E32\u6570\u7EC4");if(e.allowedTags&&!Array.isArray(e.allowedTags))throw new Error("security.allowedTags \u5FC5\u987B\u662F\u5B57\u7B26\u4E32\u6570\u7EC4");if(e.blockedAttributes&&!Array.isArray(e.blockedAttributes))throw new Error("security.blockedAttributes \u5FC5\u987B\u662F\u5B57\u7B26\u4E32\u6570\u7EC4")}}function validateAll(e){validateRules(e),validateSecurityConfig(e.security)}function sanitizeContent(e,t,n,i){const c=n?.blockDangerousTags!==!1,o=n?.blockDangerousAttributes!==!1,s=n?.blockedTags||g$1,f=n?.allowedTags,a=n?.blockedAttributes||b;let l=e;if(c){const E=f?s.filter(d=>!f.includes(d)):s;if(common_script_index.containsScriptTag(e))if(t.allowScriptInjection)i?.warn(`[\u5B89\u5168\u8B66\u544A] \u89C4\u5219 "${t.id||"unnamed"}" \u5DF2\u542F\u7528\u811A\u672C\u6CE8\u5165(allowScriptInjection=true)\uFF0C\u8BF7\u786E\u4FDD\u6CE8\u5165\u5185\u5BB9\u6765\u6E90\u53EF\u4FE1\u3002\u6CE8\u5165\u811A\u672C\u53EF\u80FD\u5E26\u6765 XSS \u653B\u51FB\u98CE\u9669\u3002`);else throw new Error(`\u89C4\u5219 "${t.id||"unnamed"}" \u7684\u5185\u5BB9\u5305\u542B <script> \u6807\u7B7E\uFF0C\u9ED8\u8BA4\u88AB\u963B\u6B62\u3002\u5982\u9700\u6CE8\u5165\u811A\u672C\uFF0C\u8BF7\u8BBE\u7F6E allowScriptInjection: true`);for(const d of E){if(d==="script")continue;const F=new RegExp(`<${d}\\b[^>]*>[\\s\\S]*?<\\/${d}>`,"gi"),h=new RegExp(`<${d}\\b[^>]*/?>`,"gi");if(F.test(l)||h.test(l))if(t.allowScriptInjection){i?.warn(`[\u5B89\u5168\u8B66\u544A] \u89C4\u5219 "${t.id||"unnamed"}" \u5305\u542B\u88AB\u963B\u6B62\u7684\u6807\u7B7E <${d}>\uFF0C\u5DF2\u901A\u8FC7 allowScriptInjection \u8DF3\u8FC7\u5B89\u5168\u68C0\u67E5\uFF0C\u8BF7\u786E\u4FDD\u5185\u5BB9\u53EF\u4FE1\u3002`);continue}else throw new Error(`\u89C4\u5219 "${t.id||"unnamed"}" \u7684\u5185\u5BB9\u5305\u542B\u88AB\u963B\u6B62\u7684\u6807\u7B7E <${d}>\u3002\u5982\u9700\u6CE8\u5165\u6B64\u6807\u7B7E\uFF0C\u8BF7\u8BBE\u7F6E allowScriptInjection: true \u6216\u5728 security.allowedTags \u4E2D\u6DFB\u52A0 "${d}"`);l=l.replace(F,""),l=l.replace(h,"")}}if(o)for(const E of a){const d=new RegExp(`\\s${E}\\s*=\\s*["'][^"']*["']`,"gi");if(d.test(l))if(t.allowScriptInjection){i?.warn(`[\u5B89\u5168\u8B66\u544A] \u89C4\u5219 "${t.id||"unnamed"}" \u5305\u542B\u5371\u9669\u5C5E\u6027 ${E}\uFF0C\u5DF2\u901A\u8FC7 allowScriptInjection \u8DF3\u8FC7\u5B89\u5168\u68C0\u67E5\uFF0C\u8BF7\u786E\u4FDD\u5185\u5BB9\u53EF\u4FE1\u3002`);continue}else throw new Error(`\u89C4\u5219 "${t.id||"unnamed"}" \u7684\u5185\u5BB9\u5305\u542B\u5371\u9669\u5C5E\u6027 ${E}\u3002\u5982\u9700\u6CE8\u5165\u6B64\u5C5E\u6027\uFF0C\u8BF7\u8BBE\u7F6E allowScriptInjection: true`);l=l.replace(d,"")}return l}function g(e,t,n){if(n==="regex"){try{const c=new RegExp(t),o=e.match(c);if(o&&o.index!==void 0)return{index:o.index,length:o[0].length}}catch{return null}return null}const i=e.indexOf(t);return i===-1?null:{index:i,length:t.length}}function applyTemplateVars(e,t,n){let i=e;const c={...n,...t};for(const[o,s]of Object.entries(c)){const f=o.replace(/[.*+?^${}()|[\]\\]/g,"\\$&"),a=s.replace(/\$/g,"$$$$");i=i.replace(new RegExp(`\\{\\{${f}\\}\\}`,"g"),a)}return i}function evaluateCondition(e,t){let n=!1;switch(e.type){case"env":{const i=e.value,c=process.env[i];n=!!c&&c!=="false"&&c!=="0";break}case"file-contains":{const i=e.value;n=t.includes(i);break}case"custom":{const i=e.value;try{n=!!i()}catch{n=!1}break}}return e.negate?!n:n}function sortRulesByPriority(e){return[...e].sort((t,n)=>(t.priority??100)-(n.priority??100))}function injectAtPosition(e,t,n,i,c){switch(n){case"head-start":{const o=e.match(/<head\b[^>]*>/i);if(!o)return{html:e,injected:!1,reason:"\u672A\u627E\u5230 <head> \u6807\u7B7E"};const s=(o.index??0)+o[0].length;return{html:e.slice(0,s)+`
2
+ `+t+e.slice(s),injected:!0}}case"head-end":{const o=e.match(/<\/head\s*>/i);if(!o)return{html:e,injected:!1,reason:"\u672A\u627E\u5230 </head> \u6807\u7B7E"};const s=o.index;return{html:e.slice(0,s)+t+`
3
+ `+e.slice(s),injected:!0}}case"body-start":{const o=e.match(/<body\b[^>]*>/i);if(!o)return{html:e,injected:!1,reason:"\u672A\u627E\u5230 <body> \u6807\u7B7E"};const s=(o.index??0)+o[0].length;return{html:e.slice(0,s)+`
4
+ `+t+e.slice(s),injected:!0}}case"body-end":{const o=e.match(/<\/body\s*>/i);if(!o)return{html:e,injected:!1,reason:"\u672A\u627E\u5230 </body> \u6807\u7B7E"};const s=o.index;return{html:e.slice(0,s)+t+`
5
+ `+e.slice(s),injected:!0}}case"before-selector":{if(!i)return{html:e,injected:!1,reason:"before-selector \u9700\u8981 selector \u53C2\u6570"};const o=g(e,i,c);return o?{html:e.slice(0,o.index)+t+`
6
+ `+e.slice(o.index),injected:!0}:{html:e,injected:!1,reason:`\u672A\u627E\u5230\u9009\u62E9\u5668 "${i}"`}}case"after-selector":{if(!i)return{html:e,injected:!1,reason:"after-selector \u9700\u8981 selector \u53C2\u6570"};const o=g(e,i,c);if(!o)return{html:e,injected:!1,reason:`\u672A\u627E\u5230\u9009\u62E9\u5668 "${i}"`};const s=o.index+o.length;return{html:e.slice(0,s)+`
7
+ `+t+e.slice(s),injected:!0}}case"replace-selector":{if(!i)return{html:e,injected:!1,reason:"replace-selector \u9700\u8981 selector \u53C2\u6570"};const o=g(e,i,c);if(!o)return{html:e,injected:!1,reason:`\u672A\u627E\u5230\u9009\u62E9\u5668 "${i}"`};const s=o.index+o.length;return{html:e.slice(0,o.index)+t+e.slice(s),injected:!0}}default:return{html:e,injected:!1,reason:`\u4E0D\u652F\u6301\u7684\u6CE8\u5165\u4F4D\u7F6E: ${n}`}}}function processRules(e,t,n,i,c){let o=e;const s=[],f=sortRulesByPriority(t);for(const a of f){const l={ruleId:a.id||"unnamed",position:a.position,selector:a.selector,injected:!1,timestamp:Date.now()};if(a.condition&&!evaluateCondition(a.condition,o)){l.injected=!1,l.reason="\u6761\u4EF6\u4E0D\u6EE1\u8DB3\uFF0C\u8DF3\u8FC7\u6CE8\u5165",s.push(l);continue}let E=applyTemplateVars(a.content,a.templateVars,n);try{E=sanitizeContent(E,a,i,c)}catch(F){l.injected=!1,l.reason=F.message,s.push(l),c?.warn(`\u89C4\u5219 "${a.id||"unnamed"}" \u5B89\u5168\u68C0\u67E5\u5931\u8D25: ${F.message}`);continue}const d=injectAtPosition(o,E,a.position,a.selector,a.selectorMatch);o=d.html,l.injected=d.injected,l.reason=d.reason,s.push(l)}return{html:o,logs:s}}class u extends factory_index.BasePlugin{injectionLogs=[];getDefaultOptions(){return{targetFile:"index.html",logInjection:!0,security:{blockDangerousTags:!0,blockDangerousAttributes:!0}}}validateOptions(){this.validator.field("targetFile").string().field("rules").required().field("logInjection").boolean().validate(),validateAll(this.options)}getPluginName(){return"html-inject"}isTargetFile(t,n){if(n==="index.html")return t.endsWith("index.html")||t.endsWith("index.htm");const i=n.replace(/\\/g,"/"),c=t.replace(/\\/g,"/");return c.endsWith(i)?!0:r__default.basename(c)===r__default.basename(i)}addPluginHooks(t){t.transformIndexHtml={order:"post",handler:(n,i)=>{const c=i.filename||"",o=this.options.targetFile||"index.html";if(!this.isTargetFile(c,o))return n;const s={warn:a=>this.logger.warn(a)},f=processRules(n,this.options.rules,this.options.templateVars,this.options.security,s);return this.injectionLogs=f.logs,this.options.logInjection&&this.logInjectionResults(f.logs),f.html}},t.buildEnd=()=>{this.options.logInjection&&this.injectionLogs.length>0&&this.logger.info(`\u6CE8\u5165\u5B8C\u6210\uFF0C\u5171\u5904\u7406 ${this.injectionLogs.length} \u6761\u89C4\u5219`)}}logInjectionResults(t){for(const n of t)n.injected?this.logger.success(`\u89C4\u5219 "${n.ruleId}" \u6CE8\u5165\u6210\u529F (\u4F4D\u7F6E: ${n.position}${n.selector?`, \u9009\u62E9\u5668: ${n.selector}`:""})`):this.logger.warn(`\u89C4\u5219 "${n.ruleId}" \u6CE8\u5165\u5931\u8D25: ${n.reason||"\u672A\u77E5\u539F\u56E0"}`)}getInjectionLogs(){return[...this.injectionLogs]}}const htmlInject=factory_index.createPluginFactory(u);exports.htmlInject=htmlInject;