vite-plugin-build-version-file 0.1.11 → 0.1.12
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/README.md +32 -0
- package/dist/index.cjs +34 -0
- package/dist/index.d.cts +1 -0
- package/dist/index.d.ts +1 -0
- package/dist/index.mjs +35 -1
- package/package.json +1 -1
package/README.md
CHANGED
|
@@ -7,6 +7,7 @@
|
|
|
7
7
|
- 开发态通过 dev server 提供 `version.json`
|
|
8
8
|
- 构建后在输出目录生成 `version.json`
|
|
9
9
|
- 普通 Vite HTML 自动注入 `window["__VERSION__"]`
|
|
10
|
+
- 可选通过 `define` 将同一份 payload 编译进 JS 产物
|
|
10
11
|
- VitePress 可通过顶层 `transformHtml` 注入同一份版本信息
|
|
11
12
|
- 支持自定义版本号、输出文件名、全局变量名、时区和额外字段
|
|
12
13
|
|
|
@@ -127,6 +128,7 @@ type BuildVersionContext = {
|
|
|
127
128
|
type BuildVersionPluginOptions = {
|
|
128
129
|
filename?: string
|
|
129
130
|
globalName?: string
|
|
131
|
+
defineName?: string | false
|
|
130
132
|
injectToHtml?: boolean
|
|
131
133
|
timeZone?: string
|
|
132
134
|
version?: string | number | ((ctx: BuildVersionContext) => string | number)
|
|
@@ -180,6 +182,36 @@ dist/meta/version.json
|
|
|
180
182
|
|
|
181
183
|
这里注入的是完整 payload 对象,不是单独版本字符串。
|
|
182
184
|
|
|
185
|
+
### `defineName`
|
|
186
|
+
|
|
187
|
+
- 类型:`string | false`
|
|
188
|
+
- 默认值:`"__VERSION__"`
|
|
189
|
+
- 作用:控制是否通过 Vite `define` 将完整 payload 编译进 JS 产物
|
|
190
|
+
|
|
191
|
+
示例:
|
|
192
|
+
|
|
193
|
+
```ts
|
|
194
|
+
buildVersionPlugin({
|
|
195
|
+
defineName: '__APP_VERSION__'
|
|
196
|
+
})
|
|
197
|
+
```
|
|
198
|
+
|
|
199
|
+
业务代码可直接读取:
|
|
200
|
+
|
|
201
|
+
```ts
|
|
202
|
+
console.log(__APP_VERSION__)
|
|
203
|
+
```
|
|
204
|
+
|
|
205
|
+
打包后会在编译阶段被替换成字面量对象,因此适合给 Vue、TS、JS 模块直接消费。
|
|
206
|
+
|
|
207
|
+
默认情况下会注入 `__VERSION__`。如果你不需要编译期常量,可显式传 `defineName: false` 关闭。
|
|
208
|
+
|
|
209
|
+
如果同时开启 `defineName` 和 `globalName`,两者会共用同一份 payload:
|
|
210
|
+
|
|
211
|
+
- `defineName` 写入编译后的 JS
|
|
212
|
+
- `globalName` 注入运行时 HTML
|
|
213
|
+
- `version.json` 仍照常输出
|
|
214
|
+
|
|
183
215
|
### `injectToHtml`
|
|
184
216
|
|
|
185
217
|
- 类型:`boolean`
|
package/dist/index.cjs
CHANGED
|
@@ -29,6 +29,7 @@ var import_node_path = require("path");
|
|
|
29
29
|
var PLUGIN_NAME = "vite-plugin-build-version-file";
|
|
30
30
|
var DEFAULT_FILENAME = "version.json";
|
|
31
31
|
var DEFAULT_GLOBAL_NAME = "__VERSION__";
|
|
32
|
+
var DEFAULT_DEFINE_NAME = "__VERSION__";
|
|
32
33
|
var DEFAULT_TIME_ZONE = "Asia/Shanghai";
|
|
33
34
|
function createDefaultVersion(command, timeZone) {
|
|
34
35
|
if (command === "serve") {
|
|
@@ -70,6 +71,15 @@ function normalizeGlobalName(globalName) {
|
|
|
70
71
|
}
|
|
71
72
|
return globalName;
|
|
72
73
|
}
|
|
74
|
+
function normalizeDefineName(defineName) {
|
|
75
|
+
if (defineName === false || defineName === void 0) {
|
|
76
|
+
return defineName ?? false;
|
|
77
|
+
}
|
|
78
|
+
if (!defineName.trim()) {
|
|
79
|
+
throw new Error(`[${PLUGIN_NAME}] "defineName" cannot be empty.`);
|
|
80
|
+
}
|
|
81
|
+
return defineName;
|
|
82
|
+
}
|
|
73
83
|
function normalizeTimeZone(timeZone) {
|
|
74
84
|
const normalized = timeZone.trim();
|
|
75
85
|
if (!normalized) {
|
|
@@ -154,6 +164,9 @@ function toRequestPath(base, fileName) {
|
|
|
154
164
|
const baseWithoutTrailingSlash = withLeadingSlash.endsWith("/") ? withLeadingSlash.slice(0, -1) : withLeadingSlash;
|
|
155
165
|
return `${baseWithoutTrailingSlash}/${fileName}`.replace(/\/{2,}/g, "/");
|
|
156
166
|
}
|
|
167
|
+
function resolveProjectRoot(root) {
|
|
168
|
+
return root ? (0, import_node_path.resolve)(process.cwd(), root) : process.cwd();
|
|
169
|
+
}
|
|
157
170
|
function resolveVersion(options, context, timeZone) {
|
|
158
171
|
if (typeof options.version === "function") {
|
|
159
172
|
return options.version(context);
|
|
@@ -180,6 +193,9 @@ function buildVersionPlugin(options = {}) {
|
|
|
180
193
|
const globalName = normalizeGlobalName(
|
|
181
194
|
options.globalName ?? DEFAULT_GLOBAL_NAME
|
|
182
195
|
);
|
|
196
|
+
const defineName = normalizeDefineName(
|
|
197
|
+
options.defineName ?? DEFAULT_DEFINE_NAME
|
|
198
|
+
);
|
|
183
199
|
const injectToHtml = options.injectToHtml ?? true;
|
|
184
200
|
const timeZone = normalizeTimeZone(options.timeZone ?? DEFAULT_TIME_ZONE);
|
|
185
201
|
const data = normalizeData(options.data);
|
|
@@ -193,6 +209,24 @@ function buildVersionPlugin(options = {}) {
|
|
|
193
209
|
};
|
|
194
210
|
const plugin = {
|
|
195
211
|
name: PLUGIN_NAME,
|
|
212
|
+
config(userConfig, env) {
|
|
213
|
+
if (!payload) {
|
|
214
|
+
const context = {
|
|
215
|
+
command: env.command,
|
|
216
|
+
mode: env.mode
|
|
217
|
+
};
|
|
218
|
+
const packageInfo = readPackageInfo(resolveProjectRoot(userConfig.root));
|
|
219
|
+
payload = createPayload(options, context, data, timeZone, packageInfo);
|
|
220
|
+
}
|
|
221
|
+
if (!defineName || !payload) {
|
|
222
|
+
return void 0;
|
|
223
|
+
}
|
|
224
|
+
return {
|
|
225
|
+
define: {
|
|
226
|
+
[defineName]: JSON.stringify(payload)
|
|
227
|
+
}
|
|
228
|
+
};
|
|
229
|
+
},
|
|
196
230
|
configResolved(config) {
|
|
197
231
|
skipBundleEmit = Boolean(config.build.ssr) && !config.build.ssrEmitAssets;
|
|
198
232
|
if (payload) {
|
package/dist/index.d.cts
CHANGED
|
@@ -12,6 +12,7 @@ interface BuildVersionPayload {
|
|
|
12
12
|
interface BuildVersionPluginOptions {
|
|
13
13
|
filename?: string;
|
|
14
14
|
globalName?: string;
|
|
15
|
+
defineName?: string | false;
|
|
15
16
|
injectToHtml?: boolean;
|
|
16
17
|
timeZone?: string;
|
|
17
18
|
version?: string | number | ((ctx: BuildVersionContext) => string | number);
|
package/dist/index.d.ts
CHANGED
|
@@ -12,6 +12,7 @@ interface BuildVersionPayload {
|
|
|
12
12
|
interface BuildVersionPluginOptions {
|
|
13
13
|
filename?: string;
|
|
14
14
|
globalName?: string;
|
|
15
|
+
defineName?: string | false;
|
|
15
16
|
injectToHtml?: boolean;
|
|
16
17
|
timeZone?: string;
|
|
17
18
|
version?: string | number | ((ctx: BuildVersionContext) => string | number);
|
package/dist/index.mjs
CHANGED
|
@@ -1,9 +1,10 @@
|
|
|
1
1
|
// src/index.ts
|
|
2
2
|
import { existsSync, readFileSync } from "fs";
|
|
3
|
-
import { dirname, join } from "path";
|
|
3
|
+
import { dirname, join, resolve } from "path";
|
|
4
4
|
var PLUGIN_NAME = "vite-plugin-build-version-file";
|
|
5
5
|
var DEFAULT_FILENAME = "version.json";
|
|
6
6
|
var DEFAULT_GLOBAL_NAME = "__VERSION__";
|
|
7
|
+
var DEFAULT_DEFINE_NAME = "__VERSION__";
|
|
7
8
|
var DEFAULT_TIME_ZONE = "Asia/Shanghai";
|
|
8
9
|
function createDefaultVersion(command, timeZone) {
|
|
9
10
|
if (command === "serve") {
|
|
@@ -45,6 +46,15 @@ function normalizeGlobalName(globalName) {
|
|
|
45
46
|
}
|
|
46
47
|
return globalName;
|
|
47
48
|
}
|
|
49
|
+
function normalizeDefineName(defineName) {
|
|
50
|
+
if (defineName === false || defineName === void 0) {
|
|
51
|
+
return defineName ?? false;
|
|
52
|
+
}
|
|
53
|
+
if (!defineName.trim()) {
|
|
54
|
+
throw new Error(`[${PLUGIN_NAME}] "defineName" cannot be empty.`);
|
|
55
|
+
}
|
|
56
|
+
return defineName;
|
|
57
|
+
}
|
|
48
58
|
function normalizeTimeZone(timeZone) {
|
|
49
59
|
const normalized = timeZone.trim();
|
|
50
60
|
if (!normalized) {
|
|
@@ -129,6 +139,9 @@ function toRequestPath(base, fileName) {
|
|
|
129
139
|
const baseWithoutTrailingSlash = withLeadingSlash.endsWith("/") ? withLeadingSlash.slice(0, -1) : withLeadingSlash;
|
|
130
140
|
return `${baseWithoutTrailingSlash}/${fileName}`.replace(/\/{2,}/g, "/");
|
|
131
141
|
}
|
|
142
|
+
function resolveProjectRoot(root) {
|
|
143
|
+
return root ? resolve(process.cwd(), root) : process.cwd();
|
|
144
|
+
}
|
|
132
145
|
function resolveVersion(options, context, timeZone) {
|
|
133
146
|
if (typeof options.version === "function") {
|
|
134
147
|
return options.version(context);
|
|
@@ -155,6 +168,9 @@ function buildVersionPlugin(options = {}) {
|
|
|
155
168
|
const globalName = normalizeGlobalName(
|
|
156
169
|
options.globalName ?? DEFAULT_GLOBAL_NAME
|
|
157
170
|
);
|
|
171
|
+
const defineName = normalizeDefineName(
|
|
172
|
+
options.defineName ?? DEFAULT_DEFINE_NAME
|
|
173
|
+
);
|
|
158
174
|
const injectToHtml = options.injectToHtml ?? true;
|
|
159
175
|
const timeZone = normalizeTimeZone(options.timeZone ?? DEFAULT_TIME_ZONE);
|
|
160
176
|
const data = normalizeData(options.data);
|
|
@@ -168,6 +184,24 @@ function buildVersionPlugin(options = {}) {
|
|
|
168
184
|
};
|
|
169
185
|
const plugin = {
|
|
170
186
|
name: PLUGIN_NAME,
|
|
187
|
+
config(userConfig, env) {
|
|
188
|
+
if (!payload) {
|
|
189
|
+
const context = {
|
|
190
|
+
command: env.command,
|
|
191
|
+
mode: env.mode
|
|
192
|
+
};
|
|
193
|
+
const packageInfo = readPackageInfo(resolveProjectRoot(userConfig.root));
|
|
194
|
+
payload = createPayload(options, context, data, timeZone, packageInfo);
|
|
195
|
+
}
|
|
196
|
+
if (!defineName || !payload) {
|
|
197
|
+
return void 0;
|
|
198
|
+
}
|
|
199
|
+
return {
|
|
200
|
+
define: {
|
|
201
|
+
[defineName]: JSON.stringify(payload)
|
|
202
|
+
}
|
|
203
|
+
};
|
|
204
|
+
},
|
|
171
205
|
configResolved(config) {
|
|
172
206
|
skipBundleEmit = Boolean(config.build.ssr) && !config.build.ssrEmitAssets;
|
|
173
207
|
if (payload) {
|