@hzab/vite-config 0.0.2-alpha.0

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 (151) hide show
  1. package/CHANGELOG.md +83 -0
  2. package/LICENSE +21 -0
  3. package/README.md +173 -0
  4. package/dist/create-app-vite-config.cjs +426 -0
  5. package/dist/create-app-vite-config.cjs.map +1 -0
  6. package/dist/create-app-vite-config.d.cts +19 -0
  7. package/dist/create-app-vite-config.d.ts +19 -0
  8. package/dist/create-app-vite-config.js +415 -0
  9. package/dist/create-app-vite-config.js.map +1 -0
  10. package/dist/create-library-vite-config.cjs +281 -0
  11. package/dist/create-library-vite-config.cjs.map +1 -0
  12. package/dist/create-library-vite-config.d.cts +31 -0
  13. package/dist/create-library-vite-config.d.ts +31 -0
  14. package/dist/create-library-vite-config.js +268 -0
  15. package/dist/create-library-vite-config.js.map +1 -0
  16. package/dist/create-package-vitest-config.cjs +73 -0
  17. package/dist/create-package-vitest-config.cjs.map +1 -0
  18. package/dist/create-package-vitest-config.d.cts +12 -0
  19. package/dist/create-package-vitest-config.d.ts +12 -0
  20. package/dist/create-package-vitest-config.js +67 -0
  21. package/dist/create-package-vitest-config.js.map +1 -0
  22. package/dist/create-vite-config.cjs +438 -0
  23. package/dist/create-vite-config.cjs.map +1 -0
  24. package/dist/create-vite-config.d.cts +17 -0
  25. package/dist/create-vite-config.d.ts +17 -0
  26. package/dist/create-vite-config.js +427 -0
  27. package/dist/create-vite-config.js.map +1 -0
  28. package/dist/index.cjs +631 -0
  29. package/dist/index.cjs.map +1 -0
  30. package/dist/index.d.cts +16 -0
  31. package/dist/index.d.ts +16 -0
  32. package/dist/index.js +603 -0
  33. package/dist/index.js.map +1 -0
  34. package/dist/plugins/copy.cjs +16 -0
  35. package/dist/plugins/copy.cjs.map +1 -0
  36. package/dist/plugins/copy.d.cts +18 -0
  37. package/dist/plugins/copy.d.ts +18 -0
  38. package/dist/plugins/copy.js +13 -0
  39. package/dist/plugins/copy.js.map +1 -0
  40. package/dist/plugins/hash-deploy.cjs +128 -0
  41. package/dist/plugins/hash-deploy.cjs.map +1 -0
  42. package/dist/plugins/hash-deploy.d.cts +56 -0
  43. package/dist/plugins/hash-deploy.d.ts +56 -0
  44. package/dist/plugins/hash-deploy.js +119 -0
  45. package/dist/plugins/hash-deploy.js.map +1 -0
  46. package/dist/plugins/less-tilde.cjs +32 -0
  47. package/dist/plugins/less-tilde.cjs.map +1 -0
  48. package/dist/plugins/less-tilde.d.cts +36 -0
  49. package/dist/plugins/less-tilde.d.ts +36 -0
  50. package/dist/plugins/less-tilde.js +29 -0
  51. package/dist/plugins/less-tilde.js.map +1 -0
  52. package/dist/plugins/lifecycle.cjs +28 -0
  53. package/dist/plugins/lifecycle.cjs.map +1 -0
  54. package/dist/plugins/lifecycle.d.cts +20 -0
  55. package/dist/plugins/lifecycle.d.ts +20 -0
  56. package/dist/plugins/lifecycle.js +26 -0
  57. package/dist/plugins/lifecycle.js.map +1 -0
  58. package/dist/plugins/public-assets.cjs +28 -0
  59. package/dist/plugins/public-assets.cjs.map +1 -0
  60. package/dist/plugins/public-assets.d.cts +17 -0
  61. package/dist/plugins/public-assets.d.ts +17 -0
  62. package/dist/plugins/public-assets.js +26 -0
  63. package/dist/plugins/public-assets.js.map +1 -0
  64. package/dist/plugins/public-config.cjs +31 -0
  65. package/dist/plugins/public-config.cjs.map +1 -0
  66. package/dist/plugins/public-config.d.cts +20 -0
  67. package/dist/plugins/public-config.d.ts +20 -0
  68. package/dist/plugins/public-config.js +29 -0
  69. package/dist/plugins/public-config.js.map +1 -0
  70. package/dist/plugins/react-compiler.cjs +22 -0
  71. package/dist/plugins/react-compiler.cjs.map +1 -0
  72. package/dist/plugins/react-compiler.d.cts +24 -0
  73. package/dist/plugins/react-compiler.d.ts +24 -0
  74. package/dist/plugins/react-compiler.js +16 -0
  75. package/dist/plugins/react-compiler.js.map +1 -0
  76. package/dist/plugins/relative-base.cjs +19 -0
  77. package/dist/plugins/relative-base.cjs.map +1 -0
  78. package/dist/plugins/relative-base.d.cts +9 -0
  79. package/dist/plugins/relative-base.d.ts +9 -0
  80. package/dist/plugins/relative-base.js +17 -0
  81. package/dist/plugins/relative-base.js.map +1 -0
  82. package/dist/plugins/stub-less.cjs +26 -0
  83. package/dist/plugins/stub-less.cjs.map +1 -0
  84. package/dist/plugins/stub-less.d.cts +9 -0
  85. package/dist/plugins/stub-less.d.ts +9 -0
  86. package/dist/plugins/stub-less.js +24 -0
  87. package/dist/plugins/stub-less.js.map +1 -0
  88. package/dist/plugins/svg-react.cjs +70 -0
  89. package/dist/plugins/svg-react.cjs.map +1 -0
  90. package/dist/plugins/svg-react.d.cts +28 -0
  91. package/dist/plugins/svg-react.d.ts +28 -0
  92. package/dist/plugins/svg-react.js +63 -0
  93. package/dist/plugins/svg-react.js.map +1 -0
  94. package/dist/svg-react-runtime.cjs +4 -0
  95. package/dist/svg-react-runtime.cjs.map +1 -0
  96. package/dist/svg-react-runtime.d.cts +2 -0
  97. package/dist/svg-react-runtime.d.ts +2 -0
  98. package/dist/svg-react-runtime.js +3 -0
  99. package/dist/svg-react-runtime.js.map +1 -0
  100. package/dist/types.cjs +4 -0
  101. package/dist/types.cjs.map +1 -0
  102. package/dist/types.d.cts +291 -0
  103. package/dist/types.d.ts +291 -0
  104. package/dist/types.js +3 -0
  105. package/dist/types.js.map +1 -0
  106. package/dist/vitest-types.cjs +4 -0
  107. package/dist/vitest-types.cjs.map +1 -0
  108. package/dist/vitest-types.d.cts +22 -0
  109. package/dist/vitest-types.d.ts +22 -0
  110. package/dist/vitest-types.js +3 -0
  111. package/dist/vitest-types.js.map +1 -0
  112. package/dist/vitest.cjs +73 -0
  113. package/dist/vitest.cjs.map +1 -0
  114. package/dist/vitest.d.cts +4 -0
  115. package/dist/vitest.d.ts +4 -0
  116. package/dist/vitest.js +67 -0
  117. package/dist/vitest.js.map +1 -0
  118. package/docs/README.md +15 -0
  119. package/docs/abt-management-ui/vite.config.mts +226 -0
  120. package/docs/api.md +366 -0
  121. package/docs/migration-from-webpack.md +722 -0
  122. package/docs/sync-public-path.md +52 -0
  123. package/docs/vite-config-integration.md +476 -0
  124. package/docs/vite-pnpm-integration.md +279 -0
  125. package/docs/vs-ccc-vite-config.md +26 -0
  126. package/docs/webpack-gap.md +107 -0
  127. package/package.json +113 -0
  128. package/src/client.d.ts +9 -0
  129. package/src/create-app-vite-config.ts +218 -0
  130. package/src/create-library-vite-config.ts +199 -0
  131. package/src/create-package-vitest-config.ts +54 -0
  132. package/src/create-vite-config.ts +22 -0
  133. package/src/index.ts +42 -0
  134. package/src/plugins/copy.ts +25 -0
  135. package/src/plugins/hash-deploy.ts +199 -0
  136. package/src/plugins/less-tilde.ts +44 -0
  137. package/src/plugins/lifecycle.ts +40 -0
  138. package/src/plugins/public-assets.ts +28 -0
  139. package/src/plugins/public-config.ts +34 -0
  140. package/src/plugins/react-compiler.ts +32 -0
  141. package/src/plugins/relative-base.ts +20 -0
  142. package/src/plugins/stub-less.ts +26 -0
  143. package/src/plugins/svg-react.ts +90 -0
  144. package/src/svg-react-runtime.ts +4 -0
  145. package/src/svg-react.d.ts +6 -0
  146. package/src/types.ts +314 -0
  147. package/src/vitest-types.ts +20 -0
  148. package/src/vitest.ts +2 -0
  149. package/templates/src/vite-env.d.ts +2 -0
  150. package/templates/tsconfig.json +12 -0
  151. package/templates/vite.config.ts +26 -0
package/src/types.ts ADDED
@@ -0,0 +1,314 @@
1
+ import type { CopyOption } from "./plugins/copy.ts";
2
+ import type { HashCleanupOptions, HashTimestampFormat } from "./plugins/hash-deploy.ts";
3
+ import type { SvgReactOptions } from "./plugins/svg-react.ts";
4
+ import type { AliasOptions, PluginOption, ProxyOptions, UserConfig } from "vite";
5
+
6
+ export type { CopyOption, CopyPluginOptions, CopyTarget } from "./plugins/copy.ts";
7
+ export type { SvgReactOptions } from "./plugins/svg-react.ts";
8
+
9
+ /**
10
+ * 对齐 Vite `build.lib.formats`。
11
+ * @public
12
+ */
13
+ export type LibraryFormats = "es" | "cjs" | "umd" | "iife";
14
+
15
+ /** @public */
16
+ export type ReactCompilerMode = boolean | "build-only";
17
+
18
+ /** @public */
19
+ export interface PublicAssetsOptions {
20
+ /**
21
+ * 源目录,相对 rootDir。
22
+ * @defaultValue "public"
23
+ */
24
+ dir?: string;
25
+ }
26
+
27
+ /** @public */
28
+ export interface PublicConfigOptions {
29
+ /** Vite mode,用于默认 sourceGlob */
30
+ mode: string;
31
+ /**
32
+ * 复制源 glob,相对项目 root。
33
+ * @defaultValue `config/public-config/${mode}/*`
34
+ */
35
+ sourceGlob?: string;
36
+ /**
37
+ * 产物内目标目录(可由消费方覆盖)。
38
+ * @defaultValue "config"(站点根)
39
+ * @example "public/config"
40
+ */
41
+ dest?: string;
42
+ }
43
+
44
+ /** @public */
45
+ export interface LessOptions {
46
+ /** 等价 webpack less-loader `additionalData`;注入到每个 less 文件前 */
47
+ additionalData?: string | ((source: string, filename: string) => string);
48
+ /** 等价 `modifyVars` */
49
+ modifyVars?: Record<string, unknown>;
50
+ /** 常见于 antd less 主题;默认不设 */
51
+ javascriptEnabled?: boolean;
52
+ /**
53
+ * 是否解析 webpack 遗留的 `~pkg/...` 导入(从 node_modules)。
54
+ * - 默认 `false`(关闭,保持 Vite 默认行为与 worker 并行)
55
+ * - `true`:注入内置 `~` 解析插件,第三方包里的 `~antd/...` 也能编译
56
+ */
57
+ resolveTilde?: boolean;
58
+ }
59
+
60
+ /** @public */
61
+ export interface CreateAppViteConfigOptions {
62
+ rootDir: string;
63
+ /**
64
+ * Git 仓库目录,用于生成 Hash 构建目录名。
65
+ * @defaultValue rootDir
66
+ */
67
+ repositoryRoot?: string;
68
+ command: "serve" | "build" | string;
69
+ mode: string;
70
+
71
+ /**
72
+ * 与 Vite `resolve.alias` 相同:
73
+ * - `Record<string, string>`
74
+ * - `{ find, replacement }[]`
75
+ */
76
+ alias?: AliasOptions;
77
+
78
+ useHttps?: boolean;
79
+ /**
80
+ * 开发服务器启动时是否自动打开浏览器(映射 `server.open`)。
81
+ * - `true`(默认):打开站点根
82
+ * - `false`:不打开
83
+ * - `string`:作为 URL pathname 打开
84
+ * 也可经 `vite.server.open` / `configure` 覆盖。
85
+ */
86
+ openBrowser?: boolean | string;
87
+ /** 是否启用 hash 目录部署(且 mode !== development) */
88
+ hashDeploy?: boolean;
89
+ /**
90
+ * Hash 子目录名中的时间戳格式。
91
+ * - `"epoch"`(默认):`{commit}-{Date.now()}`
92
+ * - `"datetime"`:`{commit}-YYYY-MM-DD-HH-mm-ss`(本地时区,零填充)
93
+ */
94
+ hashTimestamp?: HashTimestampFormat;
95
+ /**
96
+ * 构建资源公共路径(Vite `base`)。
97
+ * - 未传:build 默认 `"./"`(相对路径)
98
+ * - 可传绝对 HTTPS/HTTP 目录,如 `"https://cdn.example.com/app/"`
99
+ * - 启用 `hashDeploy` 时,实际 Vite `base` 为 `{base}{hash}/`(`base` 会自动补尾 `/`)
100
+ * 开发 `serve` 不由此字段设置(仍用 Vite 默认)。
101
+ */
102
+ base?: string;
103
+ /**
104
+ * 构建产物根目录。
105
+ * - 普通 build:即 `build.outDir`
106
+ * - hash 部署:为 `<deployRoot>/<hash>/` 的父目录,并在此写入 `index.html` / `hash.txt`
107
+ * @defaultValue "dist"
108
+ */
109
+ deployRoot?: string;
110
+ /**
111
+ * Hash 构建成功后的旧版本清理策略。
112
+ * 未传时保留所有版本,由部署系统负责清理。
113
+ */
114
+ hashCleanup?: HashCleanupOptions;
115
+ /**
116
+ * 静态资源目录复制策略。
117
+ * - 默认启用:复制到产物 `public/` 子目录,并关闭 Vite 内置 publicDir
118
+ * - `false`:恢复 Vite 默认 publicDir(文件落在 outDir 根目录)
119
+ */
120
+ publicAssets?: PublicAssetsOptions | false;
121
+ publicConfig?: PublicConfigOptions | false;
122
+ /**
123
+ * 额外静态复制。
124
+ * 可传 `vite-plugin-static-copy` 完整选项,或直接传 `targets` 数组。
125
+ * 独立用法见 `copyPlugin()`。
126
+ */
127
+ copy?: CopyOption;
128
+ /**
129
+ * 注入 HTML 模板的 EJS 数据,与内置 `PUBLIC_PATH` 合并。
130
+ * HTML `PUBLIC_PATH` 始终由工厂按 base / Hash 计算,消费方同名键会被覆盖。
131
+ * JS 请用 `import.meta.env.PUBLIC_PATH`(相对 Hash 为 `./{hash}/`),见 `@hzab/vite-config/client`。
132
+ */
133
+ htmlData?: Record<string, unknown>;
134
+ devProxy?: Record<string, ProxyOptions>;
135
+ /**
136
+ * React Compiler:
137
+ * - `true`:build 与 serve 都开
138
+ * - `"build-only"`(默认):仅 build,且可用 `REACT_COMPILER=false` 关闭
139
+ * - `false`:关闭
140
+ */
141
+ reactCompiler?: ReactCompilerMode;
142
+ /**
143
+ * 是否启用 `@vitejs/plugin-react`。
144
+ * @defaultValue true
145
+ */
146
+ react?: boolean;
147
+ /**
148
+ * SVG React 组件通道(`import Icon from "./icon.svg?react"`)。
149
+ * - 未传 / `false`:关闭(默认);普通 `*.svg` 仍走 Vite 资源 URL
150
+ * - `true`:启用,默认 `svgo: true`
151
+ * - 对象:启用并传入 SVGR 选项(如 `icon` / `svgo`)
152
+ *
153
+ * 真正转换时需安装可选 peer:`@svgr/core`、`@svgr/plugin-jsx`;
154
+ * 未关闭 `svgo` 时还需 `@svgr/plugin-svgo`。类型声明见 `@hzab/vite-config/svg-react`。
155
+ */
156
+ svgr?: boolean | SvgReactOptions;
157
+ /**
158
+ * Less 预处理器选项,映射到 `css.preprocessorOptions.less`
159
+ *(webpack `less-loader` 的 `additionalData` / `modifyVars` 等)。
160
+ */
161
+ less?: LessOptions;
162
+
163
+ /**
164
+ * 构建开始回调(Vite `buildStart`)。仅 `command === "build"` 时挂载。
165
+ */
166
+ onRun?: () => void;
167
+ /**
168
+ * 产物写盘成功后的回调(sequential post `writeBundle`)。
169
+ * 排在 Hash 根入口 / `hash.txt` 写入之后;写入失败不会调用。
170
+ * 仅 `command === "build"` 时挂载。
171
+ */
172
+ onDone?: () => void | Promise<void>;
173
+
174
+ /** 追加插件(排在内置插件之后、生命周期回调之前) */
175
+ plugins?: PluginOption[];
176
+ /** 经 mergeConfig 合并进最终配置 */
177
+ vite?: UserConfig;
178
+ /** 最终回调,可任意改 */
179
+ configure?: (config: UserConfig) => UserConfig;
180
+ }
181
+
182
+ /**
183
+ * `createAppViteConfig` 的返回值:Vite 配置 + 本次 hash 子目录名。
184
+ * `buildDirHash` 为非可枚举属性,展开进 `build()` / `defineConfig` 时不会污染配置。
185
+ * @public
186
+ */
187
+ export type CreateAppViteConfigResult = UserConfig & {
188
+ /**
189
+ * 本次 hash 部署子目录名(`{commit}-{timestamp}`,或 `hashTimestamp: "datetime"` 时为
190
+ * `{commit}-YYYY-MM-DD-HH-mm-ss`)。
191
+ * 未启用 hash 部署时为 `""`。
192
+ */
193
+ readonly buildDirHash: string;
194
+ };
195
+
196
+ /** @public */
197
+ export interface CreateLibraryViteConfigOptions {
198
+ rootDir: string;
199
+ command: "serve" | "build" | string;
200
+ /**
201
+ * Vite mode。与应用工厂对齐,便于 `defineConfig(({ command, mode }) => ...)`;
202
+ * 本工厂不据此切换 Hash 或 public-config。
203
+ */
204
+ mode: string;
205
+
206
+ /**
207
+ * 与 Vite `resolve.alias` 相同:
208
+ * - `Record<string, string>`
209
+ * - `{ find, replacement }[]`
210
+ *
211
+ * 不默认 `@` / `@assets` / `@service`。
212
+ */
213
+ alias?: AliasOptions;
214
+
215
+ /**
216
+ * 库入口,相对 `rootDir`。
217
+ * @defaultValue "src/index.ts"
218
+ */
219
+ entry?: string;
220
+
221
+ /**
222
+ * UMD / IIFE 全局变量名。`formats` 含 `umd` 或 `iife` 的 **build** 必填。
223
+ */
224
+ name?: string;
225
+
226
+ /**
227
+ * 产物目录。
228
+ * @defaultValue "lib"
229
+ */
230
+ outDir?: string;
231
+
232
+ /**
233
+ * 产物文件名。未传时:单一 format 为 `index.js`;多 format 时 ESM 为 `index.js`,其余为 `index.<format>.js`。
234
+ */
235
+ fileName?: string | ((format: string, entryName: string) => string);
236
+
237
+ /**
238
+ * CSS 产物名(不含扩展名)。库入口若导入样式则写出 `<cssFileName>.css`。
239
+ * @defaultValue "index"
240
+ */
241
+ cssFileName?: string;
242
+
243
+ /**
244
+ * @defaultValue ["umd"](对齐 webpack `libraryTarget: "umd"`)
245
+ */
246
+ formats?: LibraryFormats[];
247
+
248
+ /**
249
+ * 构建时外置的依赖。仅 `command === "build"` 生效;serve 预览会打进包,便于本地联调。
250
+ * - 未传:`react` / `react-dom`(含子路径如 `react/jsx-runtime`)
251
+ * - `false`:不外置
252
+ * - 数组:整表替换默认值(追加请展开 `DEFAULT_LIBRARY_EXTERNALS`)
253
+ */
254
+ externals?: (string | RegExp)[] | false;
255
+
256
+ /**
257
+ * UMD / IIFE `output.globals`。会与默认 React 全局名及字符串 external 的包名合并;此项覆盖同名键。
258
+ */
259
+ globals?: Record<string, string>;
260
+
261
+ useHttps?: boolean;
262
+ /**
263
+ * 开发服务器启动时是否自动打开浏览器(映射 `server.open`)。
264
+ * - `true`(默认):打开站点根
265
+ * - `false`:不打开
266
+ * - `string`:作为 URL pathname 打开
267
+ */
268
+ openBrowser?: boolean | string;
269
+ devProxy?: Record<string, ProxyOptions>;
270
+ /**
271
+ * React Compiler:
272
+ * - `true`:build 与 serve 都开
273
+ * - `"build-only"`(默认):仅 build,且可用 `REACT_COMPILER=false` 关闭
274
+ * - `false`:关闭
275
+ */
276
+ reactCompiler?: ReactCompilerMode;
277
+ /**
278
+ * 是否启用 `@vitejs/plugin-react`。
279
+ * @defaultValue true
280
+ */
281
+ react?: boolean;
282
+ /**
283
+ * SVG React 组件通道(`import Icon from "./icon.svg?react"`)。
284
+ * 未传 / `false` 关闭;`true` 或选项对象启用。
285
+ */
286
+ svgr?: boolean | SvgReactOptions;
287
+ /**
288
+ * Less 预处理器选项,映射到 `css.preprocessorOptions.less`。
289
+ */
290
+ less?: LessOptions;
291
+
292
+ /**
293
+ * 本地预览 HTML,相对 `rootDir`。仅 `command === "serve"` 时交给 `vite-plugin-html`。
294
+ * 未传则使用 Vite 默认根目录 `index.html`。不默认 `example/`。
295
+ */
296
+ previewHtml?: string;
297
+
298
+ /**
299
+ * 构建开始回调(Vite `buildStart`)。仅 `command === "build"` 时挂载。
300
+ */
301
+ onRun?: () => void;
302
+ /**
303
+ * 产物写盘成功后的回调(sequential post `writeBundle`)。
304
+ * 仅 `command === "build"` 时挂载。
305
+ */
306
+ onDone?: () => void | Promise<void>;
307
+
308
+ /** 追加插件(排在内置插件之后、生命周期回调之前) */
309
+ plugins?: PluginOption[];
310
+ /** 经 mergeConfig 合并进最终配置 */
311
+ vite?: UserConfig;
312
+ /** 最终回调,可任意改 */
313
+ configure?: (config: UserConfig) => UserConfig;
314
+ }
@@ -0,0 +1,20 @@
1
+ import type { AliasOptions, PluginOption } from "vite";
2
+ import type { ViteUserConfig as VitestUserConfig } from "vitest/config";
3
+
4
+ export interface CreatePackageVitestConfigOptions {
5
+ packageRoot: string;
6
+ /**
7
+ * 与 Vite `resolve.alias` 相同:
8
+ * - `Record<string, string>`
9
+ * - `{ find, replacement }[]`
10
+ */
11
+ alias?: AliasOptions;
12
+ react?: boolean;
13
+ stubLess?: boolean;
14
+ environment?: "happy-dom" | "jsdom" | "node" | "edge-runtime" | string;
15
+ include?: string[];
16
+ setupFiles?: string[];
17
+ plugins?: PluginOption[];
18
+ vite?: VitestUserConfig;
19
+ configure?: (config: VitestUserConfig) => VitestUserConfig;
20
+ }
package/src/vitest.ts ADDED
@@ -0,0 +1,2 @@
1
+ export { createPackageVitestConfig } from "./create-package-vitest-config.ts";
2
+ export type { CreatePackageVitestConfigOptions } from "./vitest-types.ts";
@@ -0,0 +1,2 @@
1
+ /// <reference types="vite/client" />
2
+ /// <reference types="@hzab/vite-config/client" />
@@ -0,0 +1,12 @@
1
+ {
2
+ "compilerOptions": {
3
+ "target": "ES2022",
4
+ "module": "ESNext",
5
+ "moduleResolution": "Bundler",
6
+ "strict": true,
7
+ "jsx": "react-jsx",
8
+ "skipLibCheck": true,
9
+ "noEmit": true
10
+ },
11
+ "include": ["src"]
12
+ }
@@ -0,0 +1,26 @@
1
+ import { readFileSync } from "node:fs";
2
+ import path from "node:path";
3
+ import { fileURLToPath } from "node:url";
4
+ import { defineConfig } from "vite";
5
+ import { createViteConfig } from "@hzab/vite-config";
6
+
7
+ const rootDir = path.dirname(fileURLToPath(import.meta.url));
8
+
9
+ // 从应用 package.json 读取版本号(勿注入 @hzab/vite-config 自身的版本)
10
+ const { version } = JSON.parse(readFileSync(path.resolve(rootDir, "package.json"), "utf-8"));
11
+
12
+ export default defineConfig(({ command, mode }) =>
13
+ createViteConfig({
14
+ rootDir,
15
+ command,
16
+ mode,
17
+ vite: {
18
+ define: {
19
+ // 源码中的 process.env.PACKAGE_VERSION 在构建时替换为应用版本号
20
+ "process.env.PACKAGE_VERSION": JSON.stringify(version),
21
+ // 自定义键示例:透传构建时环境变量(未设置时回退默认值)
22
+ // "process.env.CUSTOM_KEY": JSON.stringify(process.env.CUSTOM_KEY ?? ""),
23
+ },
24
+ },
25
+ }),
26
+ );