@hzab/vite-config 0.0.2-alpha.0 → 0.0.3-alpha.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.
- package/CHANGELOG.md +95 -83
- package/LICENSE +21 -21
- package/README.md +184 -173
- package/dist/create-app-vite-config.cjs +160 -27
- package/dist/create-app-vite-config.cjs.map +1 -1
- package/dist/create-app-vite-config.js +160 -27
- package/dist/create-app-vite-config.js.map +1 -1
- package/dist/create-library-vite-config.cjs +39 -4
- package/dist/create-library-vite-config.cjs.map +1 -1
- package/dist/create-library-vite-config.js +38 -3
- package/dist/create-library-vite-config.js.map +1 -1
- package/dist/create-package-vitest-config.cjs.map +1 -1
- package/dist/create-package-vitest-config.js.map +1 -1
- package/dist/create-vite-config.cjs +160 -27
- package/dist/create-vite-config.cjs.map +1 -1
- package/dist/create-vite-config.js +160 -27
- package/dist/create-vite-config.js.map +1 -1
- package/dist/index.cjs +198 -30
- package/dist/index.cjs.map +1 -1
- package/dist/index.d.cts +1 -1
- package/dist/index.d.ts +1 -1
- package/dist/index.js +199 -31
- package/dist/index.js.map +1 -1
- package/dist/plugins/copy.cjs.map +1 -1
- package/dist/plugins/copy.js.map +1 -1
- package/dist/plugins/hash-deploy.cjs +150 -25
- package/dist/plugins/hash-deploy.cjs.map +1 -1
- package/dist/plugins/hash-deploy.d.cts +16 -5
- package/dist/plugins/hash-deploy.d.ts +16 -5
- package/dist/plugins/hash-deploy.js +150 -25
- package/dist/plugins/hash-deploy.js.map +1 -1
- package/dist/plugins/less-tilde.cjs.map +1 -1
- package/dist/plugins/less-tilde.d.cts +17 -2
- package/dist/plugins/less-tilde.d.ts +17 -2
- package/dist/plugins/less-tilde.js.map +1 -1
- package/dist/plugins/lifecycle.cjs.map +1 -1
- package/dist/plugins/lifecycle.js.map +1 -1
- package/dist/plugins/public-assets.cjs.map +1 -1
- package/dist/plugins/public-assets.js.map +1 -1
- package/dist/plugins/public-config.cjs.map +1 -1
- package/dist/plugins/public-config.js.map +1 -1
- package/dist/plugins/react-compiler.cjs.map +1 -1
- package/dist/plugins/react-compiler.js.map +1 -1
- package/dist/plugins/relative-base.cjs.map +1 -1
- package/dist/plugins/relative-base.js.map +1 -1
- package/dist/plugins/stub-less.cjs.map +1 -1
- package/dist/plugins/stub-less.js.map +1 -1
- package/dist/plugins/svg-react.cjs.map +1 -1
- package/dist/plugins/svg-react.js.map +1 -1
- package/dist/types.d.cts +19 -8
- package/dist/types.d.ts +19 -8
- package/dist/vitest.cjs.map +1 -1
- package/dist/vitest.js.map +1 -1
- package/docs/README.md +15 -15
- package/docs/abt-management-ui/vite.config.mts +226 -226
- package/docs/api.md +404 -366
- package/docs/migration-from-webpack.md +723 -722
- package/docs/sync-public-path.md +52 -52
- package/docs/vite-config-integration.md +477 -476
- package/docs/vite-pnpm-integration.md +279 -279
- package/docs/vs-ccc-vite-config.md +26 -26
- package/docs/webpack-gap.md +108 -107
- package/package.json +1 -1
- package/src/client.d.ts +9 -9
- package/src/create-app-vite-config.ts +236 -218
- package/src/create-library-vite-config.ts +248 -199
- package/src/create-package-vitest-config.ts +54 -54
- package/src/create-vite-config.ts +22 -22
- package/src/index.ts +41 -42
- package/src/plugins/copy.ts +25 -25
- package/src/plugins/hash-deploy.ts +239 -199
- package/src/plugins/hash-dir-name.ts +212 -0
- package/src/plugins/less-tilde.ts +72 -44
- package/src/plugins/lifecycle.ts +40 -40
- package/src/plugins/public-assets.ts +28 -28
- package/src/plugins/public-config.ts +34 -34
- package/src/plugins/react-compiler.ts +32 -32
- package/src/plugins/relative-base.ts +20 -20
- package/src/plugins/stub-less.ts +26 -26
- package/src/plugins/svg-react.ts +90 -90
- package/src/svg-react-runtime.ts +4 -4
- package/src/svg-react.d.ts +6 -6
- package/src/types.ts +325 -314
- package/src/vitest-types.ts +20 -20
- package/src/vitest.ts +2 -2
- package/templates/src/vite-env.d.ts +2 -2
- package/templates/tsconfig.json +12 -12
- package/templates/vite.config.ts +26 -26
|
@@ -1,218 +1,236 @@
|
|
|
1
|
-
import basicSsl from "@vitejs/plugin-basic-ssl";
|
|
2
|
-
import react from "@vitejs/plugin-react";
|
|
3
|
-
import { mergeConfig, type PluginOption, type UserConfig } from "vite";
|
|
4
|
-
import { createHtmlPlugin } from "vite-plugin-html";
|
|
5
|
-
|
|
6
|
-
import { copyPlugin } from "./plugins/copy.ts";
|
|
7
|
-
import {
|
|
8
|
-
import {
|
|
9
|
-
import {
|
|
10
|
-
import {
|
|
11
|
-
import {
|
|
12
|
-
import {
|
|
13
|
-
import {
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
}
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
}
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
*
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
if (
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
*
|
|
55
|
-
*
|
|
56
|
-
*
|
|
57
|
-
*
|
|
58
|
-
*
|
|
59
|
-
*
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
|
|
94
|
-
const
|
|
95
|
-
const
|
|
96
|
-
const
|
|
97
|
-
const
|
|
98
|
-
|
|
99
|
-
|
|
100
|
-
|
|
101
|
-
|
|
102
|
-
|
|
103
|
-
|
|
104
|
-
|
|
105
|
-
|
|
106
|
-
|
|
107
|
-
|
|
108
|
-
|
|
109
|
-
|
|
110
|
-
|
|
111
|
-
const
|
|
112
|
-
?
|
|
113
|
-
|
|
114
|
-
|
|
115
|
-
|
|
116
|
-
|
|
117
|
-
|
|
118
|
-
*
|
|
119
|
-
|
|
120
|
-
|
|
121
|
-
|
|
122
|
-
|
|
123
|
-
|
|
124
|
-
|
|
125
|
-
|
|
126
|
-
|
|
127
|
-
|
|
128
|
-
|
|
129
|
-
|
|
130
|
-
|
|
131
|
-
|
|
132
|
-
|
|
133
|
-
|
|
134
|
-
|
|
135
|
-
|
|
136
|
-
|
|
137
|
-
|
|
138
|
-
|
|
139
|
-
|
|
140
|
-
|
|
141
|
-
|
|
142
|
-
|
|
143
|
-
|
|
144
|
-
|
|
145
|
-
|
|
146
|
-
|
|
147
|
-
|
|
148
|
-
|
|
149
|
-
|
|
150
|
-
|
|
151
|
-
|
|
152
|
-
|
|
153
|
-
|
|
154
|
-
|
|
155
|
-
|
|
156
|
-
|
|
157
|
-
|
|
158
|
-
|
|
159
|
-
|
|
160
|
-
|
|
161
|
-
|
|
162
|
-
|
|
163
|
-
|
|
164
|
-
|
|
165
|
-
|
|
166
|
-
|
|
167
|
-
|
|
168
|
-
|
|
169
|
-
|
|
170
|
-
:
|
|
171
|
-
|
|
172
|
-
|
|
173
|
-
|
|
174
|
-
|
|
175
|
-
|
|
176
|
-
|
|
177
|
-
|
|
178
|
-
...(
|
|
179
|
-
|
|
180
|
-
|
|
181
|
-
|
|
182
|
-
|
|
183
|
-
|
|
184
|
-
|
|
185
|
-
|
|
186
|
-
|
|
187
|
-
|
|
188
|
-
|
|
189
|
-
...(isDev
|
|
190
|
-
|
|
191
|
-
|
|
192
|
-
|
|
193
|
-
|
|
194
|
-
|
|
195
|
-
|
|
196
|
-
|
|
197
|
-
|
|
198
|
-
|
|
199
|
-
|
|
200
|
-
|
|
201
|
-
|
|
202
|
-
|
|
203
|
-
|
|
204
|
-
|
|
205
|
-
|
|
206
|
-
|
|
207
|
-
|
|
208
|
-
|
|
209
|
-
|
|
210
|
-
|
|
211
|
-
|
|
212
|
-
|
|
213
|
-
|
|
214
|
-
|
|
215
|
-
|
|
216
|
-
|
|
217
|
-
|
|
218
|
-
}
|
|
1
|
+
import basicSsl from "@vitejs/plugin-basic-ssl";
|
|
2
|
+
import react from "@vitejs/plugin-react";
|
|
3
|
+
import { mergeConfig, type PluginOption, type UserConfig } from "vite";
|
|
4
|
+
import { createHtmlPlugin } from "vite-plugin-html";
|
|
5
|
+
|
|
6
|
+
import { copyPlugin } from "./plugins/copy.ts";
|
|
7
|
+
import { DEFAULT_HASH_DIR_NAME_PATTERN } from "./plugins/hash-dir-name.ts";
|
|
8
|
+
import { createHashBuildDirName, hashDeployPlugin } from "./plugins/hash-deploy.ts";
|
|
9
|
+
import { buildLessOptions } from "./plugins/less-tilde.ts";
|
|
10
|
+
import { lifecyclePlugin } from "./plugins/lifecycle.ts";
|
|
11
|
+
import { publicAssetsPlugin } from "./plugins/public-assets.ts";
|
|
12
|
+
import { publicConfigPlugin } from "./plugins/public-config.ts";
|
|
13
|
+
import { reactCompilerPlugin } from "./plugins/react-compiler.ts";
|
|
14
|
+
import { svgReactPlugin } from "./plugins/svg-react.ts";
|
|
15
|
+
|
|
16
|
+
import type { CreateAppViteConfigOptions, CreateAppViteConfigResult } from "./types.ts";
|
|
17
|
+
|
|
18
|
+
/** 根入口运行时资源前缀,供 JS `fetch` / 动态 URL 使用。与 HTML 子目录 `PUBLIC_PATH` 可能不同。 */
|
|
19
|
+
const JS_PUBLIC_PATH_DEFINE_KEY = "import.meta.env.PUBLIC_PATH";
|
|
20
|
+
|
|
21
|
+
/** 规范化资源 base:空串视为未设置;非空则保证尾 `/`。 */
|
|
22
|
+
function normalizeAssetBase(base: string | undefined): string | undefined {
|
|
23
|
+
if (base == null) return undefined;
|
|
24
|
+
const trimmed = base.trim();
|
|
25
|
+
if (!trimmed) return undefined;
|
|
26
|
+
return trimmed.endsWith("/") ? trimmed : `${trimmed}/`;
|
|
27
|
+
}
|
|
28
|
+
|
|
29
|
+
function ensureTrailingSlash(value: string): string {
|
|
30
|
+
return value.endsWith("/") ? value : `${value}/`;
|
|
31
|
+
}
|
|
32
|
+
|
|
33
|
+
/**
|
|
34
|
+
* JS 可读的根入口前缀。相对 hash 必须用 `./{hash}/`,不能用 Vite `base`(后者保持 `./`)。
|
|
35
|
+
* 非 hash 则跟最终 `config.base`;serve 未设 base 时为 `"/"`。
|
|
36
|
+
*/
|
|
37
|
+
function resolveJsPublicPath(isHash: boolean, publicPath: string, config: UserConfig): string {
|
|
38
|
+
if (isHash) return publicPath;
|
|
39
|
+
if (typeof config.base === "string" && config.base.length > 0) {
|
|
40
|
+
return ensureTrailingSlash(config.base);
|
|
41
|
+
}
|
|
42
|
+
return publicPath || "/";
|
|
43
|
+
}
|
|
44
|
+
|
|
45
|
+
function applyJsPublicPathDefine(config: UserConfig, publicPath: string): UserConfig {
|
|
46
|
+
config.define = {
|
|
47
|
+
...config.define,
|
|
48
|
+
[JS_PUBLIC_PATH_DEFINE_KEY]: JSON.stringify(publicPath),
|
|
49
|
+
};
|
|
50
|
+
return config;
|
|
51
|
+
}
|
|
52
|
+
|
|
53
|
+
/**
|
|
54
|
+
* 组装应用侧 Vite 配置。
|
|
55
|
+
*
|
|
56
|
+
* 合并顺序:`base(options) → mergeConfig(base, vite) → configure(final)`,
|
|
57
|
+
* 之后写入 `import.meta.env.PUBLIC_PATH`(同名 `define` 会被覆盖)。
|
|
58
|
+
*
|
|
59
|
+
* 返回值附带只读 `buildDirHash`(本次 hash 子目录名;未启用时为 `""`)。
|
|
60
|
+
* @public
|
|
61
|
+
*/
|
|
62
|
+
export function createAppViteConfig(
|
|
63
|
+
options: CreateAppViteConfigOptions,
|
|
64
|
+
): CreateAppViteConfigResult {
|
|
65
|
+
const {
|
|
66
|
+
rootDir,
|
|
67
|
+
repositoryRoot,
|
|
68
|
+
command,
|
|
69
|
+
mode,
|
|
70
|
+
alias,
|
|
71
|
+
useHttps = false,
|
|
72
|
+
openBrowser = true,
|
|
73
|
+
hashDeploy = false,
|
|
74
|
+
hashDirName,
|
|
75
|
+
base: baseOption,
|
|
76
|
+
deployRoot: deployRootOption = "dist",
|
|
77
|
+
hashCleanup,
|
|
78
|
+
publicAssets,
|
|
79
|
+
publicConfig,
|
|
80
|
+
copy: copyOption,
|
|
81
|
+
htmlData,
|
|
82
|
+
devProxy,
|
|
83
|
+
reactCompiler = "build-only",
|
|
84
|
+
react: enableReact = true,
|
|
85
|
+
svgr: svgrOption = false,
|
|
86
|
+
less,
|
|
87
|
+
onRun,
|
|
88
|
+
onDone,
|
|
89
|
+
plugins: extraPlugins = [],
|
|
90
|
+
vite: viteOverride,
|
|
91
|
+
configure,
|
|
92
|
+
} = options;
|
|
93
|
+
|
|
94
|
+
const isDev = command === "serve";
|
|
95
|
+
const isBuild = command === "build";
|
|
96
|
+
const isHash = isBuild && Boolean(hashDeploy) && mode !== "development";
|
|
97
|
+
const deployRoot = deployRootOption.replace(/\/+$/, "") || "dist";
|
|
98
|
+
const assetBase = normalizeAssetBase(baseOption);
|
|
99
|
+
|
|
100
|
+
// hashTimestamp 已移除:只有 "datetime" 会改变目录名,出现即报错并给出等价模式串写法;
|
|
101
|
+
// 与默认等价的 "epoch" 忽略即可。避免部署目录名被静默改变。
|
|
102
|
+
const legacyHashTimestamp = (options as { hashTimestamp?: unknown }).hashTimestamp;
|
|
103
|
+
if (legacyHashTimestamp != null && legacyHashTimestamp !== "epoch") {
|
|
104
|
+
throw new Error(
|
|
105
|
+
`[vite-config] hashTimestamp: ${JSON.stringify(
|
|
106
|
+
legacyHashTimestamp,
|
|
107
|
+
)} has been removed; use hashDirName instead (e.g. "hash-timestamp" or "hash-YYYY-MM-DD-HH-mm-ss").`,
|
|
108
|
+
);
|
|
109
|
+
}
|
|
110
|
+
|
|
111
|
+
const buildDirStr = isHash
|
|
112
|
+
? createHashBuildDirName({
|
|
113
|
+
repositoryRoot: repositoryRoot ?? rootDir,
|
|
114
|
+
hashDirName,
|
|
115
|
+
})
|
|
116
|
+
: "";
|
|
117
|
+
/**
|
|
118
|
+
* 生效的目录名模式串:透传给 hashDeployPlugin,让 cleanup 只识别同一模式串生成的目录。
|
|
119
|
+
* `hashDirName` 不传时即默认模式串。
|
|
120
|
+
*/
|
|
121
|
+
const buildDirNamePattern = hashDirName ?? DEFAULT_HASH_DIR_NAME_PATTERN;
|
|
122
|
+
/**
|
|
123
|
+
* HTML / 根入口资源前缀(JS `import.meta.env.PUBLIC_PATH` 用这个)。
|
|
124
|
+
* - hash + 相对:`./{hash}/`(Vite `base` 仍为 `./`,由插件改写根入口)
|
|
125
|
+
* - hash + 绝对 base:`{base}{hash}/`(与 Vite `base` 一致)
|
|
126
|
+
* - 普通 build:`base` 或 `./`
|
|
127
|
+
*/
|
|
128
|
+
const publicPath = isHash
|
|
129
|
+
? `${assetBase ?? "./"}${buildDirStr}/`
|
|
130
|
+
: isBuild
|
|
131
|
+
? (assetBase ?? "./")
|
|
132
|
+
: "";
|
|
133
|
+
/**
|
|
134
|
+
* Hash 子目录入口相对于自身加载资源;根入口由 hashDeployPlugin 再补 hash 前缀。
|
|
135
|
+
* 绝对 base 则两个入口都使用同一个完整 URL。
|
|
136
|
+
*/
|
|
137
|
+
const htmlPublicPath = isHash && publicPath.startsWith("./") ? "./" : publicPath;
|
|
138
|
+
/** 相对 base(含默认 `./`)在 hash 下必须保持 Vite `base: "./"`,否则 `./{hash}/` 会被规范成 `/`。 */
|
|
139
|
+
const isAbsoluteAssetBase =
|
|
140
|
+
assetBase != null &&
|
|
141
|
+
(assetBase.startsWith("https://") ||
|
|
142
|
+
assetBase.startsWith("http://") ||
|
|
143
|
+
assetBase.startsWith("/"));
|
|
144
|
+
const viteBase = isHash
|
|
145
|
+
? isAbsoluteAssetBase
|
|
146
|
+
? publicPath
|
|
147
|
+
: "./"
|
|
148
|
+
: isBuild
|
|
149
|
+
? publicPath
|
|
150
|
+
: undefined;
|
|
151
|
+
const enablePublicAssets = publicAssets !== false;
|
|
152
|
+
|
|
153
|
+
const plugins: PluginOption[] = [
|
|
154
|
+
...(enablePublicAssets
|
|
155
|
+
? [publicAssetsPlugin(publicAssets && typeof publicAssets === "object" ? publicAssets : {})]
|
|
156
|
+
: []),
|
|
157
|
+
...(svgrOption ? [svgReactPlugin(svgrOption === true ? {} : svgrOption)] : []),
|
|
158
|
+
...(enableReact ? [react()] : []),
|
|
159
|
+
...reactCompilerPlugin({ command, mode: reactCompiler }),
|
|
160
|
+
...(publicConfig === false || publicConfig == null
|
|
161
|
+
? []
|
|
162
|
+
: [
|
|
163
|
+
publicConfigPlugin({
|
|
164
|
+
sourceGlob: publicConfig.sourceGlob ?? `config/public-config/${publicConfig.mode}/*`,
|
|
165
|
+
dest: publicConfig.dest,
|
|
166
|
+
}),
|
|
167
|
+
]),
|
|
168
|
+
...(copyOption ? [copyPlugin(copyOption)] : []),
|
|
169
|
+
createHtmlPlugin({
|
|
170
|
+
minify: isBuild,
|
|
171
|
+
inject: {
|
|
172
|
+
data: {
|
|
173
|
+
...htmlData,
|
|
174
|
+
PUBLIC_PATH: htmlPublicPath || "",
|
|
175
|
+
},
|
|
176
|
+
},
|
|
177
|
+
}),
|
|
178
|
+
...(isBuild && isHash
|
|
179
|
+
? [
|
|
180
|
+
hashDeployPlugin({
|
|
181
|
+
rootDir,
|
|
182
|
+
buildDirStr,
|
|
183
|
+
buildDirNamePattern,
|
|
184
|
+
publicPath,
|
|
185
|
+
cleanup: hashCleanup,
|
|
186
|
+
}),
|
|
187
|
+
]
|
|
188
|
+
: []),
|
|
189
|
+
...(isDev && useHttps ? [basicSsl()] : []),
|
|
190
|
+
...extraPlugins,
|
|
191
|
+
...(isBuild && (onRun || onDone) ? [lifecyclePlugin({ onRun, onDone })] : []),
|
|
192
|
+
];
|
|
193
|
+
|
|
194
|
+
const base: UserConfig = {
|
|
195
|
+
root: rootDir,
|
|
196
|
+
...(viteBase != null ? { base: viteBase } : {}),
|
|
197
|
+
plugins,
|
|
198
|
+
...(enablePublicAssets ? { publicDir: false } : {}),
|
|
199
|
+
resolve: {
|
|
200
|
+
alias,
|
|
201
|
+
},
|
|
202
|
+
css: {
|
|
203
|
+
preprocessorOptions: {
|
|
204
|
+
less: buildLessOptions(less),
|
|
205
|
+
},
|
|
206
|
+
},
|
|
207
|
+
...(isDev
|
|
208
|
+
? {
|
|
209
|
+
server: {
|
|
210
|
+
https: useHttps ? {} : undefined,
|
|
211
|
+
host: true,
|
|
212
|
+
open: openBrowser,
|
|
213
|
+
...(devProxy ? { proxy: devProxy } : {}),
|
|
214
|
+
},
|
|
215
|
+
}
|
|
216
|
+
: {
|
|
217
|
+
build: {
|
|
218
|
+
outDir: isHash ? `${deployRoot}/${buildDirStr}/` : deployRoot,
|
|
219
|
+
assetsDir: "assets",
|
|
220
|
+
emptyOutDir: true,
|
|
221
|
+
},
|
|
222
|
+
}),
|
|
223
|
+
};
|
|
224
|
+
|
|
225
|
+
let merged = viteOverride ? mergeConfig(base, viteOverride) : base;
|
|
226
|
+
if (configure) {
|
|
227
|
+
merged = configure(merged);
|
|
228
|
+
}
|
|
229
|
+
applyJsPublicPathDefine(merged, resolveJsPublicPath(isHash, publicPath, merged));
|
|
230
|
+
Object.defineProperty(merged, "buildDirHash", {
|
|
231
|
+
value: buildDirStr,
|
|
232
|
+
enumerable: false,
|
|
233
|
+
configurable: true,
|
|
234
|
+
});
|
|
235
|
+
return merged as CreateAppViteConfigResult;
|
|
236
|
+
}
|