@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/dist/index.cjs ADDED
@@ -0,0 +1,631 @@
1
+ 'use strict';
2
+
3
+ var basicSsl = require('@vitejs/plugin-basic-ssl');
4
+ var react = require('@vitejs/plugin-react');
5
+ var vite = require('vite');
6
+ var vitePluginHtml = require('vite-plugin-html');
7
+ var vitePluginStaticCopy = require('vite-plugin-static-copy');
8
+ var child_process = require('child_process');
9
+ var fs = require('fs');
10
+ var path = require('path');
11
+ var babel = require('@rolldown/plugin-babel');
12
+ var fs2 = require('fs/promises');
13
+
14
+ function _interopDefault (e) { return e && e.__esModule ? e : { default: e }; }
15
+
16
+ var basicSsl__default = /*#__PURE__*/_interopDefault(basicSsl);
17
+ var react__default = /*#__PURE__*/_interopDefault(react);
18
+ var fs__default = /*#__PURE__*/_interopDefault(fs);
19
+ var path__default = /*#__PURE__*/_interopDefault(path);
20
+ var babel__default = /*#__PURE__*/_interopDefault(babel);
21
+ var fs2__default = /*#__PURE__*/_interopDefault(fs2);
22
+
23
+ // src/create-app-vite-config.ts
24
+ function resolveCopyPluginOptions(options) {
25
+ return Array.isArray(options) ? { targets: options } : options;
26
+ }
27
+ function copyPlugin(options) {
28
+ return vitePluginStaticCopy.viteStaticCopy(resolveCopyPluginOptions(options));
29
+ }
30
+ var HASH_BUILD_DIR_PATTERN = /^(?:[0-9a-f]{12}|local)-(\d{4}-\d{2}-\d{2}-\d{2}-\d{2}-\d{2}|\d+)$/;
31
+ function parseHashTimestamp(value) {
32
+ if (/^\d+$/.test(value)) return Number(value);
33
+ const [year = 0, month = 1, day = 1, hour = 0, minute = 0, second = 0] = value.split("-").map(Number);
34
+ return new Date(year, month - 1, day, hour, minute, second).getTime();
35
+ }
36
+ function padTwo(value) {
37
+ return String(value).padStart(2, "0");
38
+ }
39
+ function formatHashTimestamp(now, format = "epoch") {
40
+ if (format !== "datetime") return String(now);
41
+ const date = new Date(now);
42
+ return [
43
+ date.getFullYear(),
44
+ padTwo(date.getMonth() + 1),
45
+ padTwo(date.getDate()),
46
+ padTwo(date.getHours()),
47
+ padTwo(date.getMinutes()),
48
+ padTwo(date.getSeconds())
49
+ ].join("-");
50
+ }
51
+ function cleanupOldHashBuilds(deployRoot, currentBuildDir, keep) {
52
+ const oldBuilds = fs__default.default.readdirSync(deployRoot, { withFileTypes: true }).flatMap((entry) => {
53
+ if (!entry.isDirectory() || entry.name === currentBuildDir) return [];
54
+ const match = HASH_BUILD_DIR_PATTERN.exec(entry.name);
55
+ return match ? [{ name: entry.name, timestamp: parseHashTimestamp(match[1]) }] : [];
56
+ }).sort((left, right) => right.timestamp - left.timestamp || right.name.localeCompare(left.name));
57
+ for (const build of oldBuilds.slice(Math.max(keep - 1, 0))) {
58
+ fs__default.default.rmSync(path__default.default.join(deployRoot, build.name), { recursive: true });
59
+ }
60
+ }
61
+ function hashDeployPlugin(options) {
62
+ const { rootDir, buildDirStr, publicPath, cleanup } = options;
63
+ if (cleanup && (!Number.isInteger(cleanup.keep) || cleanup.keep < 1)) {
64
+ throw new Error("[hash-deploy] cleanup.keep must be an integer greater than or equal to 1.");
65
+ }
66
+ let outputDir;
67
+ let deployRoot;
68
+ return {
69
+ name: "hash-deploy",
70
+ enforce: "post",
71
+ configResolved(config) {
72
+ const resolvedRoot = path__default.default.resolve(config.root);
73
+ const expectedRoot = path__default.default.resolve(rootDir);
74
+ if (resolvedRoot !== expectedRoot) {
75
+ throw new Error(
76
+ `[hash-deploy] Vite root "${resolvedRoot}" must match rootDir "${expectedRoot}".`
77
+ );
78
+ }
79
+ const expectedBase = publicPath.startsWith("./") ? "./" : publicPath;
80
+ if (config.base !== expectedBase) {
81
+ throw new Error(
82
+ `[hash-deploy] Vite base "${config.base}" must remain "${expectedBase}" during hash deployment.`
83
+ );
84
+ }
85
+ outputDir = path__default.default.resolve(resolvedRoot, config.build.outDir);
86
+ if (path__default.default.basename(outputDir) !== buildDirStr) {
87
+ throw new Error(
88
+ `[hash-deploy] build.outDir "${config.build.outDir}" must end with the hash directory "${buildDirStr}".`
89
+ );
90
+ }
91
+ deployRoot = path__default.default.dirname(outputDir);
92
+ },
93
+ writeBundle: {
94
+ sequential: true,
95
+ order: "post",
96
+ handler() {
97
+ if (!outputDir || !deployRoot) {
98
+ throw new Error("[hash-deploy] Vite configuration was not resolved before writeBundle.");
99
+ }
100
+ const sourceIndex = path__default.default.join(outputDir, "index.html");
101
+ if (!fs__default.default.existsSync(sourceIndex)) {
102
+ throw new Error(`[hash-deploy] Generated entry was not found: "${sourceIndex}".`);
103
+ }
104
+ fs__default.default.mkdirSync(deployRoot, { recursive: true });
105
+ fs__default.default.writeFileSync(path__default.default.join(deployRoot, "hash.txt"), buildDirStr);
106
+ const sourceHtml = fs__default.default.readFileSync(sourceIndex, "utf8");
107
+ const rootHtml = publicPath.startsWith("./") ? sourceHtml.replace(/(\b(?:href|src)\s*=\s*["'])\.\//g, `$1${publicPath}`) : sourceHtml;
108
+ fs__default.default.writeFileSync(path__default.default.join(deployRoot, "index.html"), rootHtml);
109
+ if (cleanup) {
110
+ cleanupOldHashBuilds(deployRoot, buildDirStr, cleanup.keep);
111
+ }
112
+ }
113
+ }
114
+ };
115
+ }
116
+ function getCommitHash(repositoryRoot = process.cwd()) {
117
+ try {
118
+ return child_process.execFileSync("git", ["rev-parse", "HEAD"], {
119
+ cwd: repositoryRoot,
120
+ encoding: "utf8",
121
+ stdio: ["ignore", "pipe", "ignore"]
122
+ }).trim().slice(0, 12);
123
+ } catch {
124
+ return "local";
125
+ }
126
+ }
127
+ function createHashBuildDirName(repositoryRootOrNowOrOptions, now = Date.now()) {
128
+ if (repositoryRootOrNowOrOptions != null && typeof repositoryRootOrNowOrOptions === "object") {
129
+ const {
130
+ repositoryRoot: repositoryRoot2,
131
+ now: optionsNow = Date.now(),
132
+ timestampFormat = "epoch"
133
+ } = repositoryRootOrNowOrOptions;
134
+ return `${getCommitHash(repositoryRoot2)}-${formatHashTimestamp(optionsNow, timestampFormat)}`;
135
+ }
136
+ const repositoryRoot = typeof repositoryRootOrNowOrOptions === "string" ? repositoryRootOrNowOrOptions : void 0;
137
+ const timestamp = typeof repositoryRootOrNowOrOptions === "number" ? repositoryRootOrNowOrOptions : now;
138
+ return `${getCommitHash(repositoryRoot)}-${formatHashTimestamp(timestamp)}`;
139
+ }
140
+
141
+ // src/plugins/less-tilde.ts
142
+ function lessTildeResolutionPlugin() {
143
+ return {
144
+ install(less, pluginManager) {
145
+ class TildeFileManager extends less.FileManager {
146
+ supports(filename) {
147
+ return filename.startsWith("~");
148
+ }
149
+ loadFile(filename, currentDirectory, options, environment) {
150
+ return super.loadFile(filename.slice(1), currentDirectory, options, environment);
151
+ }
152
+ }
153
+ pluginManager.addFileManager(new TildeFileManager());
154
+ }
155
+ };
156
+ }
157
+ function buildLessOptions(less) {
158
+ const resolveTilde = less?.resolveTilde === true;
159
+ return {
160
+ ...less?.additionalData != null ? { additionalData: less.additionalData } : {},
161
+ ...less?.modifyVars != null ? { modifyVars: less.modifyVars } : {},
162
+ ...less?.javascriptEnabled != null ? { javascriptEnabled: less.javascriptEnabled } : {},
163
+ ...resolveTilde ? { plugins: [lessTildeResolutionPlugin()] } : {}
164
+ };
165
+ }
166
+
167
+ // src/plugins/lifecycle.ts
168
+ function lifecyclePlugin(options = {}) {
169
+ const { onRun, onDone } = options;
170
+ return {
171
+ name: "build-lifecycle",
172
+ enforce: "post",
173
+ buildStart() {
174
+ if (typeof onRun === "function") {
175
+ onRun();
176
+ }
177
+ },
178
+ writeBundle: {
179
+ sequential: true,
180
+ order: "post",
181
+ async handler() {
182
+ if (typeof onDone === "function") {
183
+ await onDone();
184
+ }
185
+ }
186
+ }
187
+ };
188
+ }
189
+
190
+ // src/plugins/public-assets.ts
191
+ function publicAssetsPlugin(options = {}) {
192
+ const { dir = "public" } = options;
193
+ return copyPlugin({
194
+ targets: [
195
+ {
196
+ src: `${dir}/**/*`,
197
+ dest: "."
198
+ }
199
+ ]
200
+ });
201
+ }
202
+
203
+ // src/plugins/public-config.ts
204
+ function publicConfigPlugin(options) {
205
+ const { sourceGlob, dest = "config" } = options;
206
+ return copyPlugin({
207
+ targets: [
208
+ {
209
+ src: sourceGlob,
210
+ dest,
211
+ rename: {
212
+ stripBase: true
213
+ }
214
+ }
215
+ ]
216
+ });
217
+ }
218
+ function reactCompilerPlugin(options) {
219
+ const { command, mode = "build-only" } = options;
220
+ if (mode === false) return [];
221
+ const isBuild = command === "build";
222
+ const enabled = mode === true ? true : isBuild && process.env.REACT_COMPILER !== "false";
223
+ if (!enabled) return [];
224
+ return [babel__default.default({ presets: [react.reactCompilerPreset()] })];
225
+ }
226
+ var SVG_EXT_RE = /\.svg$/i;
227
+ function isSvgReactId(id) {
228
+ const queryStart = id.indexOf("?");
229
+ if (queryStart === -1) return false;
230
+ const file = id.slice(0, queryStart);
231
+ if (!SVG_EXT_RE.test(file)) return false;
232
+ return /(?:^|&)react(?:&|$)/.test(id.slice(queryStart + 1));
233
+ }
234
+ function missingPeerError(pkg, cause) {
235
+ const error = new Error(
236
+ `[svg-react] "${pkg}" is required to transform *.svg?react. Install the optional peer or disable CreateAppViteConfigOptions.svgr.`
237
+ );
238
+ error.cause = cause;
239
+ return error;
240
+ }
241
+ async function resolveSvgrConfig(options) {
242
+ const merged = { svgo: true, ...options };
243
+ if (merged.plugins != null) return merged;
244
+ const plugins = [];
245
+ if (merged.svgo !== false) {
246
+ try {
247
+ plugins.push((await import('@svgr/plugin-svgo')).default);
248
+ } catch (cause) {
249
+ throw missingPeerError("@svgr/plugin-svgo", cause);
250
+ }
251
+ }
252
+ try {
253
+ plugins.push((await import('@svgr/plugin-jsx')).default);
254
+ } catch (cause) {
255
+ throw missingPeerError("@svgr/plugin-jsx", cause);
256
+ }
257
+ return { ...merged, plugins };
258
+ }
259
+ function svgReactPlugin(options = {}) {
260
+ return {
261
+ name: "ccc:svg-react",
262
+ enforce: "pre",
263
+ async load(id) {
264
+ if (!isSvgReactId(id)) return;
265
+ const filePath = id.slice(0, id.indexOf("?"));
266
+ const svg = await fs2__default.default.readFile(filePath, "utf8");
267
+ let transform;
268
+ try {
269
+ ({ transform } = await import('@svgr/core'));
270
+ } catch (cause) {
271
+ throw missingPeerError("@svgr/core", cause);
272
+ }
273
+ const componentCode = await transform(svg, await resolveSvgrConfig(options), { filePath });
274
+ const result = await vite.transformWithOxc(componentCode, `${filePath}.jsx`, {
275
+ lang: "jsx"
276
+ });
277
+ return { code: result.code, map: result.map };
278
+ }
279
+ };
280
+ }
281
+
282
+ // src/create-app-vite-config.ts
283
+ var JS_PUBLIC_PATH_DEFINE_KEY = "import.meta.env.PUBLIC_PATH";
284
+ function normalizeAssetBase(base) {
285
+ if (base == null) return void 0;
286
+ const trimmed = base.trim();
287
+ if (!trimmed) return void 0;
288
+ return trimmed.endsWith("/") ? trimmed : `${trimmed}/`;
289
+ }
290
+ function ensureTrailingSlash(value) {
291
+ return value.endsWith("/") ? value : `${value}/`;
292
+ }
293
+ function resolveJsPublicPath(isHash, publicPath, config) {
294
+ if (isHash) return publicPath;
295
+ if (typeof config.base === "string" && config.base.length > 0) {
296
+ return ensureTrailingSlash(config.base);
297
+ }
298
+ return publicPath || "/";
299
+ }
300
+ function applyJsPublicPathDefine(config, publicPath) {
301
+ config.define = {
302
+ ...config.define,
303
+ [JS_PUBLIC_PATH_DEFINE_KEY]: JSON.stringify(publicPath)
304
+ };
305
+ return config;
306
+ }
307
+ function createAppViteConfig(options) {
308
+ const {
309
+ rootDir,
310
+ repositoryRoot,
311
+ command,
312
+ mode,
313
+ alias,
314
+ useHttps = false,
315
+ openBrowser = true,
316
+ hashDeploy = false,
317
+ hashTimestamp = "epoch",
318
+ base: baseOption,
319
+ deployRoot: deployRootOption = "dist",
320
+ hashCleanup,
321
+ publicAssets,
322
+ publicConfig,
323
+ copy: copyOption,
324
+ htmlData,
325
+ devProxy,
326
+ reactCompiler = "build-only",
327
+ react: enableReact = true,
328
+ svgr: svgrOption = false,
329
+ less,
330
+ onRun,
331
+ onDone,
332
+ plugins: extraPlugins = [],
333
+ vite: viteOverride,
334
+ configure
335
+ } = options;
336
+ const isDev = command === "serve";
337
+ const isBuild = command === "build";
338
+ const isHash = isBuild && Boolean(hashDeploy) && mode !== "development";
339
+ const deployRoot = deployRootOption.replace(/\/+$/, "") || "dist";
340
+ const assetBase = normalizeAssetBase(baseOption);
341
+ const buildDirStr = isHash ? createHashBuildDirName({
342
+ repositoryRoot: repositoryRoot ?? rootDir,
343
+ timestampFormat: hashTimestamp
344
+ }) : "";
345
+ const publicPath = isHash ? `${assetBase ?? "./"}${buildDirStr}/` : isBuild ? assetBase ?? "./" : "";
346
+ const htmlPublicPath = isHash && publicPath.startsWith("./") ? "./" : publicPath;
347
+ const isAbsoluteAssetBase = assetBase != null && (assetBase.startsWith("https://") || assetBase.startsWith("http://") || assetBase.startsWith("/"));
348
+ const viteBase = isHash ? isAbsoluteAssetBase ? publicPath : "./" : isBuild ? publicPath : void 0;
349
+ const enablePublicAssets = publicAssets !== false;
350
+ const plugins = [
351
+ ...enablePublicAssets ? [publicAssetsPlugin(publicAssets && typeof publicAssets === "object" ? publicAssets : {})] : [],
352
+ ...svgrOption ? [svgReactPlugin(svgrOption === true ? {} : svgrOption)] : [],
353
+ ...enableReact ? [react__default.default()] : [],
354
+ ...reactCompilerPlugin({ command, mode: reactCompiler }),
355
+ ...publicConfig === false || publicConfig == null ? [] : [
356
+ publicConfigPlugin({
357
+ sourceGlob: publicConfig.sourceGlob ?? `config/public-config/${publicConfig.mode}/*`,
358
+ dest: publicConfig.dest
359
+ })
360
+ ],
361
+ ...copyOption ? [copyPlugin(copyOption)] : [],
362
+ vitePluginHtml.createHtmlPlugin({
363
+ minify: isBuild,
364
+ inject: {
365
+ data: {
366
+ ...htmlData,
367
+ PUBLIC_PATH: htmlPublicPath || ""
368
+ }
369
+ }
370
+ }),
371
+ ...isBuild && isHash ? [
372
+ hashDeployPlugin({
373
+ rootDir,
374
+ buildDirStr,
375
+ publicPath,
376
+ cleanup: hashCleanup
377
+ })
378
+ ] : [],
379
+ ...isDev && useHttps ? [basicSsl__default.default()] : [],
380
+ ...extraPlugins,
381
+ ...isBuild && (onRun || onDone) ? [lifecyclePlugin({ onRun, onDone })] : []
382
+ ];
383
+ const base = {
384
+ root: rootDir,
385
+ ...viteBase != null ? { base: viteBase } : {},
386
+ plugins,
387
+ ...enablePublicAssets ? { publicDir: false } : {},
388
+ resolve: {
389
+ alias
390
+ },
391
+ css: {
392
+ preprocessorOptions: {
393
+ less: buildLessOptions(less)
394
+ }
395
+ },
396
+ ...isDev ? {
397
+ server: {
398
+ https: useHttps ? {} : void 0,
399
+ host: true,
400
+ open: openBrowser,
401
+ ...devProxy ? { proxy: devProxy } : {}
402
+ }
403
+ } : {
404
+ build: {
405
+ outDir: isHash ? `${deployRoot}/${buildDirStr}/` : deployRoot,
406
+ assetsDir: "assets",
407
+ emptyOutDir: true
408
+ }
409
+ }
410
+ };
411
+ let merged = viteOverride ? vite.mergeConfig(base, viteOverride) : base;
412
+ if (configure) {
413
+ merged = configure(merged);
414
+ }
415
+ applyJsPublicPathDefine(merged, resolveJsPublicPath(isHash, publicPath, merged));
416
+ Object.defineProperty(merged, "buildDirHash", {
417
+ value: buildDirStr,
418
+ enumerable: false,
419
+ configurable: true
420
+ });
421
+ return merged;
422
+ }
423
+
424
+ // src/create-vite-config.ts
425
+ function createViteConfig(options = {}) {
426
+ return createAppViteConfig({
427
+ rootDir: options.rootDir ?? process.cwd(),
428
+ command: options.command ?? "serve",
429
+ mode: options.mode ?? "development",
430
+ openBrowser: options.openBrowser ?? false,
431
+ reactCompiler: false,
432
+ ...options
433
+ });
434
+ }
435
+ var DEFAULT_LIBRARY_EXTERNALS = ["react", "react-dom"];
436
+ var DEFAULT_LIBRARY_GLOBALS = {
437
+ react: "React",
438
+ "react-dom": "ReactDOM"
439
+ };
440
+ var DEFAULT_LIBRARY_FORMATS = ["umd"];
441
+ function needsLibraryName(formats) {
442
+ return formats.includes("umd") || formats.includes("iife");
443
+ }
444
+ function resolveExternals(externals) {
445
+ if (externals === false) return [];
446
+ if (externals == null) return [...DEFAULT_LIBRARY_EXTERNALS];
447
+ return externals;
448
+ }
449
+ function resolveLibraryFileName(fileName, formats) {
450
+ if (fileName != null) return fileName;
451
+ if (formats.length === 1) {
452
+ return () => "index.js";
453
+ }
454
+ return (format) => format === "es" ? "index.js" : `index.${format}.js`;
455
+ }
456
+ function resolveLibraryGlobals(externals, globals) {
457
+ const inferred = {};
458
+ for (const ext of externals) {
459
+ if (typeof ext !== "string") continue;
460
+ inferred[ext] = DEFAULT_LIBRARY_GLOBALS[ext] ?? ext;
461
+ }
462
+ return { ...inferred, ...globals };
463
+ }
464
+ function matchLibraryExternal(id, externals) {
465
+ const source = id.split("?")[0] ?? id;
466
+ return externals.some((ext) => {
467
+ if (typeof ext === "string") {
468
+ return source === ext || source.startsWith(`${ext}/`);
469
+ }
470
+ return ext.test(source);
471
+ });
472
+ }
473
+ function resolveLibraryGlobalName(id, globals) {
474
+ const source = id.split("?")[0] ?? id;
475
+ if (globals[source]) return globals[source];
476
+ const matched = Object.keys(globals).find(
477
+ (key) => source === key || source.startsWith(`${key}/`)
478
+ );
479
+ return matched ? globals[matched] ?? source : source;
480
+ }
481
+ function createLibraryViteConfig(options) {
482
+ const {
483
+ rootDir,
484
+ command,
485
+ alias,
486
+ entry = "src/index.ts",
487
+ name,
488
+ outDir = "lib",
489
+ fileName,
490
+ cssFileName = "index",
491
+ formats = DEFAULT_LIBRARY_FORMATS,
492
+ externals: externalsOption,
493
+ globals: globalsOption,
494
+ useHttps = false,
495
+ openBrowser = true,
496
+ devProxy,
497
+ reactCompiler = "build-only",
498
+ react: enableReact = true,
499
+ svgr: svgrOption = false,
500
+ less,
501
+ previewHtml,
502
+ onRun,
503
+ onDone,
504
+ plugins: extraPlugins = [],
505
+ vite: viteOverride,
506
+ configure
507
+ } = options;
508
+ const isDev = command === "serve";
509
+ const isBuild = command === "build";
510
+ if (isBuild && needsLibraryName(formats) && (name == null || name.trim() === "")) {
511
+ throw new Error(
512
+ '[vite-config] createLibraryViteConfig: `name` is required when formats include "umd" or "iife".'
513
+ );
514
+ }
515
+ const plugins = [
516
+ ...svgrOption ? [svgReactPlugin(svgrOption === true ? {} : svgrOption)] : [],
517
+ ...enableReact ? [react__default.default()] : [],
518
+ ...reactCompilerPlugin({ command, mode: reactCompiler }),
519
+ ...isDev && previewHtml ? [
520
+ vitePluginHtml.createHtmlPlugin({
521
+ minify: false,
522
+ template: path__default.default.resolve(rootDir, previewHtml)
523
+ })
524
+ ] : [],
525
+ ...isDev && useHttps ? [basicSsl__default.default()] : [],
526
+ ...extraPlugins,
527
+ ...isBuild && (onRun || onDone) ? [lifecyclePlugin({ onRun, onDone })] : []
528
+ ];
529
+ const resolvedExternals = resolveExternals(externalsOption);
530
+ const resolvedGlobals = resolveLibraryGlobals(resolvedExternals, globalsOption);
531
+ const base = {
532
+ root: rootDir,
533
+ publicDir: false,
534
+ plugins,
535
+ resolve: {
536
+ alias
537
+ },
538
+ css: {
539
+ preprocessorOptions: {
540
+ less: buildLessOptions(less)
541
+ }
542
+ },
543
+ ...isDev ? {
544
+ server: {
545
+ https: useHttps ? {} : void 0,
546
+ host: true,
547
+ open: openBrowser,
548
+ ...devProxy ? { proxy: devProxy } : {}
549
+ }
550
+ } : {
551
+ build: {
552
+ outDir,
553
+ emptyOutDir: true,
554
+ lib: {
555
+ entry: path__default.default.resolve(rootDir, entry),
556
+ ...name != null && name.trim() !== "" ? { name } : {},
557
+ formats,
558
+ fileName: resolveLibraryFileName(fileName, formats),
559
+ cssFileName
560
+ },
561
+ rolldownOptions: {
562
+ external: (id) => matchLibraryExternal(id, resolvedExternals),
563
+ output: {
564
+ globals: (id) => resolveLibraryGlobalName(id, resolvedGlobals)
565
+ }
566
+ }
567
+ }
568
+ }
569
+ };
570
+ let merged = viteOverride ? vite.mergeConfig(base, viteOverride) : base;
571
+ if (configure) {
572
+ merged = configure(merged);
573
+ }
574
+ return merged;
575
+ }
576
+
577
+ // src/plugins/relative-base.ts
578
+ function relativeBasePlugin(deployPath = "./") {
579
+ return {
580
+ name: "relative-base",
581
+ transformIndexHtml(html) {
582
+ return html.replace(/href="\.?\/assets\//g, `href="${deployPath}assets/`).replace(/src="\.?\/assets\//g, `src="${deployPath}assets/`);
583
+ },
584
+ transform(code, id) {
585
+ if (id.includes("node_modules")) return null;
586
+ return code.replace(/import\.meta\.env\.BASE_URL/g, `'${deployPath}'`);
587
+ }
588
+ };
589
+ }
590
+
591
+ // src/plugins/stub-less.ts
592
+ var cssModuleStub = `
593
+ const styles = new Proxy({}, {
594
+ get: (_target, property) => typeof property === "string" ? property : undefined,
595
+ });
596
+ export default styles;
597
+ `;
598
+ function stubLessPlugin() {
599
+ return {
600
+ name: "stub-less",
601
+ load(id) {
602
+ if (/\.module\.less(?:$|\?)/.test(id)) {
603
+ return cssModuleStub;
604
+ }
605
+ if (/\.less(?:$|\?)/.test(id)) {
606
+ return "";
607
+ }
608
+ }
609
+ };
610
+ }
611
+
612
+ exports.DEFAULT_LIBRARY_EXTERNALS = DEFAULT_LIBRARY_EXTERNALS;
613
+ exports.DEFAULT_LIBRARY_GLOBALS = DEFAULT_LIBRARY_GLOBALS;
614
+ exports.buildLessOptions = buildLessOptions;
615
+ exports.copyPlugin = copyPlugin;
616
+ exports.createAppViteConfig = createAppViteConfig;
617
+ exports.createHashBuildDirName = createHashBuildDirName;
618
+ exports.createLibraryViteConfig = createLibraryViteConfig;
619
+ exports.createViteConfig = createViteConfig;
620
+ exports.getCommitHash = getCommitHash;
621
+ exports.hashDeployPlugin = hashDeployPlugin;
622
+ exports.lessTildeResolutionPlugin = lessTildeResolutionPlugin;
623
+ exports.lifecyclePlugin = lifecyclePlugin;
624
+ exports.publicAssetsPlugin = publicAssetsPlugin;
625
+ exports.publicConfigPlugin = publicConfigPlugin;
626
+ exports.reactCompilerPlugin = reactCompilerPlugin;
627
+ exports.relativeBasePlugin = relativeBasePlugin;
628
+ exports.stubLessPlugin = stubLessPlugin;
629
+ exports.svgReactPlugin = svgReactPlugin;
630
+ //# sourceMappingURL=index.cjs.map
631
+ //# sourceMappingURL=index.cjs.map