@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
@@ -0,0 +1,73 @@
1
+ 'use strict';
2
+
3
+ var react = require('@vitejs/plugin-react');
4
+ var vite = require('vite');
5
+
6
+ function _interopDefault (e) { return e && e.__esModule ? e : { default: e }; }
7
+
8
+ var react__default = /*#__PURE__*/_interopDefault(react);
9
+
10
+ // src/create-package-vitest-config.ts
11
+
12
+ // src/plugins/stub-less.ts
13
+ var cssModuleStub = `
14
+ const styles = new Proxy({}, {
15
+ get: (_target, property) => typeof property === "string" ? property : undefined,
16
+ });
17
+ export default styles;
18
+ `;
19
+ function stubLessPlugin() {
20
+ return {
21
+ name: "stub-less",
22
+ load(id) {
23
+ if (/\.module\.less(?:$|\?)/.test(id)) {
24
+ return cssModuleStub;
25
+ }
26
+ if (/\.less(?:$|\?)/.test(id)) {
27
+ return "";
28
+ }
29
+ }
30
+ };
31
+ }
32
+
33
+ // src/create-package-vitest-config.ts
34
+ function createPackageVitestConfig(options) {
35
+ const {
36
+ packageRoot,
37
+ alias,
38
+ react: enableReact = false,
39
+ stubLess = false,
40
+ environment = "happy-dom",
41
+ include = ["test/**/*.test.ts"],
42
+ setupFiles,
43
+ plugins: extraPlugins = [],
44
+ vite: viteOverride,
45
+ configure
46
+ } = options;
47
+ const plugins = [
48
+ ...enableReact ? [react__default.default()] : [],
49
+ ...stubLess ? [stubLessPlugin()] : [],
50
+ ...extraPlugins
51
+ ];
52
+ const base = {
53
+ root: packageRoot,
54
+ plugins,
55
+ resolve: {
56
+ alias
57
+ },
58
+ test: {
59
+ environment,
60
+ include,
61
+ ...setupFiles ? { setupFiles } : {}
62
+ }
63
+ };
64
+ let merged = viteOverride ? vite.mergeConfig(base, viteOverride) : base;
65
+ if (configure) {
66
+ merged = configure(merged);
67
+ }
68
+ return merged;
69
+ }
70
+
71
+ exports.createPackageVitestConfig = createPackageVitestConfig;
72
+ //# sourceMappingURL=vitest.cjs.map
73
+ //# sourceMappingURL=vitest.cjs.map
@@ -0,0 +1 @@
1
+ {"version":3,"sources":["../src/plugins/stub-less.ts","../src/create-package-vitest-config.ts"],"names":["react","mergeConfig"],"mappings":";;;;;;;;;;;;AAEA,IAAM,aAAA,GAAgB;AAAA;AAAA;AAAA;AAAA;AAAA,CAAA;AAWf,SAAS,cAAA,GAAyB;AACvC,EAAA,OAAO;AAAA,IACL,IAAA,EAAM,WAAA;AAAA,IACN,KAAK,EAAA,EAAI;AACP,MAAA,IAAI,wBAAA,CAAyB,IAAA,CAAK,EAAE,CAAA,EAAG;AACrC,QAAA,OAAO,aAAA;AAAA,MACT;AACA,MAAA,IAAI,gBAAA,CAAiB,IAAA,CAAK,EAAE,CAAA,EAAG;AAC7B,QAAA,OAAO,EAAA;AAAA,MACT;AAAA,IACF;AAAA,GACF;AACF;;;ACZO,SAAS,0BACd,OAAA,EACkB;AAClB,EAAA,MAAM;AAAA,IACJ,WAAA;AAAA,IACA,KAAA;AAAA,IACA,OAAO,WAAA,GAAc,KAAA;AAAA,IACrB,QAAA,GAAW,KAAA;AAAA,IACX,WAAA,GAAc,WAAA;AAAA,IACd,OAAA,GAAU,CAAC,mBAAmB,CAAA;AAAA,IAC9B,UAAA;AAAA,IACA,OAAA,EAAS,eAAe,EAAC;AAAA,IACzB,IAAA,EAAM,YAAA;AAAA,IACN;AAAA,GACF,GAAI,OAAA;AAEJ,EAAA,MAAM,OAAA,GAA0B;AAAA,IAC9B,GAAI,WAAA,GAAc,CAACA,sBAAA,EAAO,IAAI,EAAC;AAAA,IAC/B,GAAI,QAAA,GAAW,CAAC,cAAA,EAAgB,IAAI,EAAC;AAAA,IACrC,GAAG;AAAA,GACL;AAEA,EAAA,MAAM,IAAA,GAAyB;AAAA,IAC7B,IAAA,EAAM,WAAA;AAAA,IACN,OAAA;AAAA,IACA,OAAA,EAAS;AAAA,MACP;AAAA,KACF;AAAA,IACA,IAAA,EAAM;AAAA,MACJ,WAAA;AAAA,MACA,OAAA;AAAA,MACA,GAAI,UAAA,GAAa,EAAE,UAAA,KAAe;AAAC;AACrC,GACF;AAEA,EAAA,IAAI,MAAA,GAAS,YAAA,GAAeC,gBAAA,CAAY,IAAA,EAAM,YAAY,CAAA,GAAI,IAAA;AAC9D,EAAA,IAAI,SAAA,EAAW;AACb,IAAA,MAAA,GAAS,UAAU,MAAM,CAAA;AAAA,EAC3B;AACA,EAAA,OAAO,MAAA;AACT","file":"vitest.cjs","sourcesContent":["import type { Plugin } from \"vite\";\r\n\r\nconst cssModuleStub = `\r\nconst styles = new Proxy({}, {\r\n get: (_target, property) => typeof property === \"string\" ? property : undefined,\r\n});\r\nexport default styles;\r\n`;\r\n\r\n/**\r\n * 单测不解析 less,并为 CSS Modules 提供 identity stub\r\n * @public\r\n */\r\nexport function stubLessPlugin(): Plugin {\r\n return {\r\n name: \"stub-less\",\r\n load(id) {\r\n if (/\\.module\\.less(?:$|\\?)/.test(id)) {\r\n return cssModuleStub;\r\n }\r\n if (/\\.less(?:$|\\?)/.test(id)) {\r\n return \"\";\r\n }\r\n },\r\n };\r\n}\r\n","import react from \"@vitejs/plugin-react\";\r\nimport { mergeConfig, type PluginOption } from \"vite\";\r\n\r\nimport { stubLessPlugin } from \"./plugins/stub-less.ts\";\r\n\r\nimport type { CreatePackageVitestConfigOptions } from \"./vitest-types.ts\";\r\nimport type { ViteUserConfig as VitestUserConfig } from \"vitest/config\";\r\n\r\n/**\r\n * 组装包级 Vitest 配置。\r\n *\r\n * 合并顺序:`base(options) → mergeConfig(base, vite) → configure(final)`\r\n */\r\nexport function createPackageVitestConfig(\r\n options: CreatePackageVitestConfigOptions,\r\n): VitestUserConfig {\r\n const {\r\n packageRoot,\r\n alias,\r\n react: enableReact = false,\r\n stubLess = false,\r\n environment = \"happy-dom\",\r\n include = [\"test/**/*.test.ts\"],\r\n setupFiles,\r\n plugins: extraPlugins = [],\r\n vite: viteOverride,\r\n configure,\r\n } = options;\r\n\r\n const plugins: PluginOption[] = [\r\n ...(enableReact ? [react()] : []),\r\n ...(stubLess ? [stubLessPlugin()] : []),\r\n ...extraPlugins,\r\n ];\r\n\r\n const base: VitestUserConfig = {\r\n root: packageRoot,\r\n plugins,\r\n resolve: {\r\n alias,\r\n },\r\n test: {\r\n environment,\r\n include,\r\n ...(setupFiles ? { setupFiles } : {}),\r\n },\r\n };\r\n\r\n let merged = viteOverride ? mergeConfig(base, viteOverride) : base;\r\n if (configure) {\r\n merged = configure(merged);\r\n }\r\n return merged;\r\n}\r\n"]}
@@ -0,0 +1,4 @@
1
+ export { createPackageVitestConfig } from './create-package-vitest-config.cjs';
2
+ export { CreatePackageVitestConfigOptions } from './vitest-types.cjs';
3
+ import 'vitest/config';
4
+ import 'vite';
@@ -0,0 +1,4 @@
1
+ export { createPackageVitestConfig } from './create-package-vitest-config.js';
2
+ export { CreatePackageVitestConfigOptions } from './vitest-types.js';
3
+ import 'vitest/config';
4
+ import 'vite';
package/dist/vitest.js ADDED
@@ -0,0 +1,67 @@
1
+ import react from '@vitejs/plugin-react';
2
+ import { mergeConfig } from 'vite';
3
+
4
+ // src/create-package-vitest-config.ts
5
+
6
+ // src/plugins/stub-less.ts
7
+ var cssModuleStub = `
8
+ const styles = new Proxy({}, {
9
+ get: (_target, property) => typeof property === "string" ? property : undefined,
10
+ });
11
+ export default styles;
12
+ `;
13
+ function stubLessPlugin() {
14
+ return {
15
+ name: "stub-less",
16
+ load(id) {
17
+ if (/\.module\.less(?:$|\?)/.test(id)) {
18
+ return cssModuleStub;
19
+ }
20
+ if (/\.less(?:$|\?)/.test(id)) {
21
+ return "";
22
+ }
23
+ }
24
+ };
25
+ }
26
+
27
+ // src/create-package-vitest-config.ts
28
+ function createPackageVitestConfig(options) {
29
+ const {
30
+ packageRoot,
31
+ alias,
32
+ react: enableReact = false,
33
+ stubLess = false,
34
+ environment = "happy-dom",
35
+ include = ["test/**/*.test.ts"],
36
+ setupFiles,
37
+ plugins: extraPlugins = [],
38
+ vite: viteOverride,
39
+ configure
40
+ } = options;
41
+ const plugins = [
42
+ ...enableReact ? [react()] : [],
43
+ ...stubLess ? [stubLessPlugin()] : [],
44
+ ...extraPlugins
45
+ ];
46
+ const base = {
47
+ root: packageRoot,
48
+ plugins,
49
+ resolve: {
50
+ alias
51
+ },
52
+ test: {
53
+ environment,
54
+ include,
55
+ ...setupFiles ? { setupFiles } : {}
56
+ }
57
+ };
58
+ let merged = viteOverride ? mergeConfig(base, viteOverride) : base;
59
+ if (configure) {
60
+ merged = configure(merged);
61
+ }
62
+ return merged;
63
+ }
64
+
65
+ export { createPackageVitestConfig };
66
+ //# sourceMappingURL=vitest.js.map
67
+ //# sourceMappingURL=vitest.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"sources":["../src/plugins/stub-less.ts","../src/create-package-vitest-config.ts"],"names":[],"mappings":";;;;;;AAEA,IAAM,aAAA,GAAgB;AAAA;AAAA;AAAA;AAAA;AAAA,CAAA;AAWf,SAAS,cAAA,GAAyB;AACvC,EAAA,OAAO;AAAA,IACL,IAAA,EAAM,WAAA;AAAA,IACN,KAAK,EAAA,EAAI;AACP,MAAA,IAAI,wBAAA,CAAyB,IAAA,CAAK,EAAE,CAAA,EAAG;AACrC,QAAA,OAAO,aAAA;AAAA,MACT;AACA,MAAA,IAAI,gBAAA,CAAiB,IAAA,CAAK,EAAE,CAAA,EAAG;AAC7B,QAAA,OAAO,EAAA;AAAA,MACT;AAAA,IACF;AAAA,GACF;AACF;;;ACZO,SAAS,0BACd,OAAA,EACkB;AAClB,EAAA,MAAM;AAAA,IACJ,WAAA;AAAA,IACA,KAAA;AAAA,IACA,OAAO,WAAA,GAAc,KAAA;AAAA,IACrB,QAAA,GAAW,KAAA;AAAA,IACX,WAAA,GAAc,WAAA;AAAA,IACd,OAAA,GAAU,CAAC,mBAAmB,CAAA;AAAA,IAC9B,UAAA;AAAA,IACA,OAAA,EAAS,eAAe,EAAC;AAAA,IACzB,IAAA,EAAM,YAAA;AAAA,IACN;AAAA,GACF,GAAI,OAAA;AAEJ,EAAA,MAAM,OAAA,GAA0B;AAAA,IAC9B,GAAI,WAAA,GAAc,CAAC,KAAA,EAAO,IAAI,EAAC;AAAA,IAC/B,GAAI,QAAA,GAAW,CAAC,cAAA,EAAgB,IAAI,EAAC;AAAA,IACrC,GAAG;AAAA,GACL;AAEA,EAAA,MAAM,IAAA,GAAyB;AAAA,IAC7B,IAAA,EAAM,WAAA;AAAA,IACN,OAAA;AAAA,IACA,OAAA,EAAS;AAAA,MACP;AAAA,KACF;AAAA,IACA,IAAA,EAAM;AAAA,MACJ,WAAA;AAAA,MACA,OAAA;AAAA,MACA,GAAI,UAAA,GAAa,EAAE,UAAA,KAAe;AAAC;AACrC,GACF;AAEA,EAAA,IAAI,MAAA,GAAS,YAAA,GAAe,WAAA,CAAY,IAAA,EAAM,YAAY,CAAA,GAAI,IAAA;AAC9D,EAAA,IAAI,SAAA,EAAW;AACb,IAAA,MAAA,GAAS,UAAU,MAAM,CAAA;AAAA,EAC3B;AACA,EAAA,OAAO,MAAA;AACT","file":"vitest.js","sourcesContent":["import type { Plugin } from \"vite\";\r\n\r\nconst cssModuleStub = `\r\nconst styles = new Proxy({}, {\r\n get: (_target, property) => typeof property === \"string\" ? property : undefined,\r\n});\r\nexport default styles;\r\n`;\r\n\r\n/**\r\n * 单测不解析 less,并为 CSS Modules 提供 identity stub\r\n * @public\r\n */\r\nexport function stubLessPlugin(): Plugin {\r\n return {\r\n name: \"stub-less\",\r\n load(id) {\r\n if (/\\.module\\.less(?:$|\\?)/.test(id)) {\r\n return cssModuleStub;\r\n }\r\n if (/\\.less(?:$|\\?)/.test(id)) {\r\n return \"\";\r\n }\r\n },\r\n };\r\n}\r\n","import react from \"@vitejs/plugin-react\";\r\nimport { mergeConfig, type PluginOption } from \"vite\";\r\n\r\nimport { stubLessPlugin } from \"./plugins/stub-less.ts\";\r\n\r\nimport type { CreatePackageVitestConfigOptions } from \"./vitest-types.ts\";\r\nimport type { ViteUserConfig as VitestUserConfig } from \"vitest/config\";\r\n\r\n/**\r\n * 组装包级 Vitest 配置。\r\n *\r\n * 合并顺序:`base(options) → mergeConfig(base, vite) → configure(final)`\r\n */\r\nexport function createPackageVitestConfig(\r\n options: CreatePackageVitestConfigOptions,\r\n): VitestUserConfig {\r\n const {\r\n packageRoot,\r\n alias,\r\n react: enableReact = false,\r\n stubLess = false,\r\n environment = \"happy-dom\",\r\n include = [\"test/**/*.test.ts\"],\r\n setupFiles,\r\n plugins: extraPlugins = [],\r\n vite: viteOverride,\r\n configure,\r\n } = options;\r\n\r\n const plugins: PluginOption[] = [\r\n ...(enableReact ? [react()] : []),\r\n ...(stubLess ? [stubLessPlugin()] : []),\r\n ...extraPlugins,\r\n ];\r\n\r\n const base: VitestUserConfig = {\r\n root: packageRoot,\r\n plugins,\r\n resolve: {\r\n alias,\r\n },\r\n test: {\r\n environment,\r\n include,\r\n ...(setupFiles ? { setupFiles } : {}),\r\n },\r\n };\r\n\r\n let merged = viteOverride ? mergeConfig(base, viteOverride) : base;\r\n if (configure) {\r\n merged = configure(merged);\r\n }\r\n return merged;\r\n}\r\n"]}
package/docs/README.md ADDED
@@ -0,0 +1,15 @@
1
+ # 文档目录
2
+
3
+ Vite / Vitest 配置工厂与插件
4
+
5
+ | 模块 | 说明 |
6
+ | ---------------------------------------------------------- | ------------------------------------------------------------------------------------------------------------------------- |
7
+ | [API](./api.md) | 工厂(应用 / 组件库 / Vitest)、`alias`(Vite 原生)、插件与自定义合并 |
8
+ | [相对 webpack-config](./webpack-gap.md) | 已对齐项、真实缺口、Vite 原生覆盖与刻意不搬的耦合 |
9
+ | [从 webpack-config 迁移](./migration-from-webpack.md) | 页面应用(`createPageConfig` → `createAppViteConfig`)与组件库(`createLibConfig` → `createLibraryViteConfig`)的落地步骤 |
10
+ | [相对 Vite 仓 `@ccc/vite-config`](./vs-ccc-vite-config.md) | 2026-08-28 现状:能力已对齐;剩余为包名 / catalog / 覆盖率门槛 |
11
+ | [`PUBLIC_PATH` 回灌记录](./sync-public-path.md) | 从 Vite 仓移植 JS `import.meta.env.PUBLIC_PATH` 与 `./client`(已实施) |
12
+ | [发布](../README.md#发布) | 构建产物、打包检查与 npm 发布命令 |
13
+ | [审查报告](../REVIEW.md) | 当前仍开问题、测试缺口与建议处理顺序 |
14
+
15
+ 返回 [包说明](../README.md)。
@@ -0,0 +1,226 @@
1
+ // abt-management-ui
2
+ import fs from "node:fs";
3
+ import path from "node:path";
4
+ import { fileURLToPath } from "node:url";
5
+ import { defineConfig, transformWithOxc } from "vite";
6
+ import { viteCommonjs } from "@originjs/vite-plugin-commonjs";
7
+
8
+ import { createAppViteConfig } from "@hzab/vite-config";
9
+
10
+ const rootDir = path.dirname(fileURLToPath(import.meta.url));
11
+
12
+ // 读取 package.json 版本号,用于注入 process.env.PACKAGE_VERSION / process.env.APP_VERSION,
13
+ // 对齐 @hzab/webpack-config 的 DefinePlugin 默认注入(webpack 侧会暴露 PACKAGE_VERSION)。
14
+ const pkgJson = JSON.parse(fs.readFileSync(path.resolve(rootDir, "package.json"), "utf8"));
15
+ const PACKAGE_VERSION = pkgJson.version || "";
16
+
17
+ // 项目与 @hzab/* 源码包中大量存在「扩展名为 .js 却写了 JSX」的文件。webpack 由 babel-loader
18
+ // 转译;Vite 8 (rolldown) 的 transform 由 oxc 接管,OxcOptions 已 Omit lang(按扩展名推断),
19
+ // 默认对 .js 不解析 JSX,会报 "Unexpected JSX expression"(含 node_modules 里的来源包,不可改文件)。
20
+ // 这里用 vite 的 transformWithOxc 把 .js 以 jsx lang 预转换,覆盖 src 与 node_modules。
21
+ const jsxInJsPlugin = {
22
+ name: "jsx-in-js",
23
+ enforce: "pre" as const,
24
+ async transform(code: string, id: string) {
25
+ if (!id.endsWith(".js")) return;
26
+ if (id.includes("/node_modules/.vite/")) return; // 预构建产物不重复处理
27
+ try {
28
+ return await transformWithOxc(code, id, {
29
+ lang: "jsx",
30
+ sourceType: "module",
31
+ jsxRuntime: "automatic",
32
+ jsxImportSource: "react",
33
+ } as any);
34
+ } catch {
35
+ return; // 转换失败交回后续管线(可能是已无 JSX 的正常 .js)
36
+ }
37
+ },
38
+ };
39
+
40
+ // turf-jsts 是传递依赖(经 @turf/buffer),pnpm 不会把它软链到顶层 node_modules,
41
+ // 因此无法用 optimizeDeps.include 预构建;这里直接指向其原生 ESM 产物 jsts.mjs,
42
+ // 使 @turf/buffer 的 `import { BufferOp } from 'turf-jsts'` 能拿到具名导出。
43
+ function resolveTurfJstsEsm() {
44
+ const pnpmRoot = path.resolve(rootDir, "node_modules/.pnpm");
45
+ const entry = fs.readdirSync(pnpmRoot).find((name) => name.startsWith("turf-jsts@"));
46
+ if (!entry) {
47
+ throw new Error("[vite-config] turf-jsts not found in node_modules/.pnpm");
48
+ }
49
+ return path.resolve(pnpmRoot, entry, "node_modules/turf-jsts/jsts.mjs");
50
+ }
51
+
52
+ // c-formily-antd 存在仅含 lib(CJS) 的实例(如 2.3.1):其 `__exportStar` 重导出在 Vite
53
+ // 预构建时无法被静态识别,导致 `import { useFormLayout } from 'c-formily-antd'` 等具名导入
54
+ // 报 "does not provide an export named '...'"。统一改写到一个带 esm/ 的实例(如 2.3.5),
55
+ // 走原生 ESM 的 `export *` 重导出,具名导出即可被正确解析。
56
+ function resolveCFormilyAntdEsm() {
57
+ const pnpmRoot = path.resolve(rootDir, "node_modules/.pnpm");
58
+ const entries = fs.readdirSync(pnpmRoot).filter((name) => name.startsWith("c-formily-antd@"));
59
+ for (const entry of entries) {
60
+ const esmDir = path.resolve(pnpmRoot, entry, "node_modules/c-formily-antd/esm");
61
+ if (fs.existsSync(esmDir)) return esmDir;
62
+ }
63
+ throw new Error(
64
+ "[vite-config] no c-formily-antd instance with an esm build found in node_modules/.pnpm",
65
+ );
66
+ }
67
+ const cFormilyAntdEsmDir = resolveCFormilyAntdEsm();
68
+
69
+ // quickselect 是传递依赖(经 rbush / geojson-rbush / ol / @cesium/engine 等),pnpm 不会把它
70
+ // 软链到顶层 node_modules,因此无法用 optimizeDeps.include 预构建(同 turf-jsts)。其 1.x/2.x
71
+ // 是 UMD 包裹器:`(function (global, factory) { ... }(this, ...))`,顶层 `this` 在被 Vite 当作 ESM
72
+ // 服务时是 undefined,浏览器执行抛 `Cannot set properties of undefined (setting 'quickselect')`。
73
+ // 统一改写到一个带 `"type": "module"` 的原生 ESM 实例(如 3.0.0),走 `export default`。
74
+ // 各版本签名一致(quickselect(arr, k, left, right, compare)),跨版本混用安全。
75
+ function resolveQuickselectEsm() {
76
+ const pnpmRoot = path.resolve(rootDir, "node_modules/.pnpm");
77
+ const entries = fs.readdirSync(pnpmRoot).filter((name) => name.startsWith("quickselect@"));
78
+ for (const entry of entries) {
79
+ const pkgRoot = path.resolve(pnpmRoot, entry, "node_modules/quickselect");
80
+ const pkgJson = path.join(pkgRoot, "package.json");
81
+ if (!fs.existsSync(pkgJson)) continue;
82
+ try {
83
+ const pkg = JSON.parse(fs.readFileSync(pkgJson, "utf8"));
84
+ // 仅认 `"type": "module"` 的实例:仅带 module/index.js 而缺 type 的(如 2.x),
85
+ // 其 index.js 会被 Vite 按最近 package.json 的 type 判定为 CJS,`export default` 会被误解析。
86
+ if (pkg.type !== "module") continue;
87
+ const entryFile = path.resolve(pkgRoot, pkg.module || pkg.main || "index.js");
88
+ if (fs.existsSync(entryFile)) return entryFile;
89
+ } catch {
90
+ /* 单个实例解析失败则跳过,继续找下一个 */
91
+ }
92
+ }
93
+ throw new Error(
94
+ '[vite-config] no quickselect instance with "type": "module" found in node_modules/.pnpm',
95
+ );
96
+ }
97
+ const quickselectEsm = resolveQuickselectEsm();
98
+
99
+ // 使用最新包解决
100
+ // @hzab/classnames-utils 只有 webpack 打的 UMD CJS 构建(lib/index.js,module.exports = t(),
101
+ // 用 e.d getter 定义具名导出)。Vite dev 裸供该 CJS 时无法静态识别 NO_PREFIX_TYPE / PREFIX_TYPE /
102
+ // STYLES_TYPE / ClassNameUtils 等具名导出,报 "does not provide an export named 'NO_PREFIX_TYPE'"。
103
+ // 其 src/ 是干净的原生 TS ESM(export const NO_PREFIX_TYPE / export class ClassNameUtils /
104
+ // export default),直接 alias 到 src/index.ts 走原生 ESM,具名导出即可被正确解析。
105
+ // function resolveClassnamesUtilsSrc() {
106
+ // const pnpmRoot = path.resolve(rootDir, "node_modules/.pnpm");
107
+ // const entries = fs
108
+ // .readdirSync(pnpmRoot)
109
+ // .filter((name) => name.startsWith("@hzab+classnames-utils@"));
110
+ // for (const entry of entries) {
111
+ // const srcEntry = path.resolve(
112
+ // pnpmRoot,
113
+ // entry,
114
+ // "node_modules/@hzab/classnames-utils/src/index.ts",
115
+ // );
116
+ // if (fs.existsSync(srcEntry)) return srcEntry;
117
+ // }
118
+ // throw new Error(
119
+ // "[vite-config] no @hzab/classnames-utils src/index.ts found in node_modules/.pnpm",
120
+ // );
121
+ // }
122
+ // const classnamesUtilsSrc = resolveClassnamesUtilsSrc();
123
+
124
+ export default defineConfig(({ command, mode }) =>
125
+ createAppViteConfig({
126
+ rootDir,
127
+ command,
128
+ mode,
129
+ alias: {
130
+ "@": path.resolve(rootDir, "./src"),
131
+ "@assets": path.resolve(rootDir, "./src/assets"),
132
+ "@packages": path.resolve(rootDir, "./packages"),
133
+ // 对齐旧 webpack alias(config/webpack.config.js):代码里仍以该名称引用本地 packages
134
+ "hzab-schema-form-render": path.resolve(rootDir, "packages/pc/form-render"),
135
+ "hzab-schema-list-render": path.resolve(rootDir, "packages/pc/list-render"),
136
+ // '@packages': ..., '@assets': ..., '@service': ..., // 原 resolve.alias
137
+ // 把 c-formily-antd 改写到一个带 esm/ 的实例(见 resolveCFormilyAntdEsm):
138
+ // - `c-formily-antd` -> esm/index.js
139
+ // - `c-formily-antd/lib/*` -> esm/* (__builtins__、各模块等)
140
+ "c-formily-antd/lib": cFormilyAntdEsmDir,
141
+ "c-formily-antd": path.join(cFormilyAntdEsmDir, "index.js"),
142
+ // 指向原生 ESM 源,解决 "does not provide an export named 'NO_PREFIX_TYPE'"
143
+ // "@hzab/classnames-utils": classnamesUtilsSrc,
144
+ // 指向原生 ESM 产物,解决 "does not provide an export named 'BufferOp'"
145
+ "turf-jsts": resolveTurfJstsEsm(),
146
+ // 指向原生 ESM 产物,解决 rbush 依赖的 1.x/2.x UMD 包裹器顶层 `this` 在 ESM 下为 undefined 的报错
147
+ quickselect: quickselectEsm,
148
+ },
149
+ // 原 devServer.proxy
150
+ devProxy: { "/api": "http://localhost:13000" },
151
+ // 原 lessLoaderOptions
152
+ less: {
153
+ // additionalData: `@import url(@/size.less);`,
154
+ // modifyVars: { '@primary-color': '#1890ff' },
155
+ javascriptEnabled: true, // formily 内联 JS 才需要(Vite 侧默认 false,见第 8 节)
156
+ resolveTilde: true, // 开启 `~` 解析(默认关闭,项目含 `~antd` 时开)
157
+ },
158
+ // 原 isHash + hashPublicPath
159
+ hashDeploy: mode !== "development",
160
+ // hash 子目录名的时间戳格式:默认 epoch(毫秒时间戳);datetime 为 YYYY-MM-DD-HH-mm-ss(年月日时分秒)
161
+ hashTimestamp: "datetime",
162
+ hashCleanup: { keep: 3 }, // 可选;默认永久保留旧版本
163
+ // base: 'https://test.com/aaa/', // 若 hashPublicPath 是固定 CDN 前缀
164
+ // 原 public-config 多环境复制;glob 键优先 PUBLIC_CONF_DIR(对齐 webpack 多环境目录名),未设置时回退 Vite mode
165
+ publicConfig: { mode: process.env.PUBLIC_CONF_DIR || mode, dest: "public/config" }, // 对齐 webpack 产物路径;默认产物 config/
166
+ // 注入 HTML 模板的 EJS 数据;仅 PUBLIC_PATH 由工厂始终注入,其余键(如 APP_TITLE)必须在此声明
167
+ htmlData: { APP_TITLE: "My App" },
168
+ svgr: true, // 启用 svg 组件通道,import 需从 ?svgEle 改为 ?react
169
+ // 关闭 React Compiler:babel-plugin-react-compiler 未安装,build 时默认的
170
+ // "build-only" 会因找不到该包而失败;如需启用,先安装依赖再改回 "build-only"。
171
+ reactCompiler: false,
172
+ plugins: [
173
+ jsxInJsPlugin,
174
+ viteCommonjs({
175
+ // 可选:显式指定需要转换的包名
176
+ include: [
177
+ "classnames",
178
+ "dayjs",
179
+ "rbush",
180
+ "earcut",
181
+ "skmeans",
182
+ "turf-jsts", // 保留
183
+ "polygon-clipping",
184
+ "density-clustering",
185
+ "point-in-polygon",
186
+ "object-assign",
187
+ "hoist-non-react-statics",
188
+ "prop-types",
189
+ ],
190
+ }),
191
+ ],
192
+ vite: {
193
+ server: { port: 3000 }, // 原 devServer.port
194
+ // 老 IE 星号属性 hack(如 *zoom)会被 lightningcss 当语法错误中断生产构建;
195
+ // errorRecovery 会剥离这些早已失效的 hack,使构建继续。
196
+ css: {
197
+ lightningcss: {
198
+ errorRecovery: true,
199
+ },
200
+ },
201
+ // react-is / deep-equal / geojson-equality 都是 CJS-only 的共享传递依赖。把它们提升为顶层依赖
202
+ // (见 package.json)后,这里显式 include,强制被 Vite 预构建为 ESM,避免被当作外部依赖裸供
203
+ // (浏览器报 "module is not defined" / "require is not defined")。
204
+ // deep-equal / geojson-equality 的整个 es-shims CJS 闭包(object-keys、regexp.prototype.flags、
205
+ // define-properties 等)会随之一并内联,不再逐个裸供。
206
+ optimizeDeps: {
207
+ include: ["react-is", "deep-equal", "geojson-equality"],
208
+ },
209
+ // 原 DefinePlugin / processEnv:webpack 由 DefinePlugin 全局注入 process.env.*,
210
+ // 而 Vite 默认仅替换 process.env.NODE_ENV(浏览器端没有全局 process),其余
211
+ // process.env.* 会残留并抛 "ReferenceError: process is not defined"(如 src/utils/axios.js)。
212
+ // 此处对齐 @hzab/webpack-config 的默认注入集合(WEBPACK_ENV / WEBPACK_PUBLIC_PATH /
213
+ // PACKAGE_VERSION)与用户自定义 processEnv(BUSINESS_PLATFORM)。
214
+ define: {
215
+ "process.env.BUSINESS_PLATFORM": JSON.stringify(process.env.BUSINESS_PLATFORM || "unknown"),
216
+ "process.env.WEBPACK_ENV": JSON.stringify(mode),
217
+ // Vite 无 publicPath 概念;置空使 axios 里 public 资源请求走「不加 baseURL」分支,
218
+ // 即 process.env.WEBPACK_PUBLIC_PATH 为 falsy 时落到 /^(\.?\/)*public\// 判断。
219
+ "process.env.WEBPACK_PUBLIC_PATH": JSON.stringify(""),
220
+ "process.env.PACKAGE_VERSION": JSON.stringify(PACKAGE_VERSION),
221
+ // webpack 默认不注入 APP_VERSION(原为 undefined);补 version 使水印呈现版本号
222
+ "process.env.APP_VERSION": JSON.stringify(PACKAGE_VERSION),
223
+ },
224
+ },
225
+ }),
226
+ );