vite-plugin-build-version-file 0.1.0 → 0.1.2
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 +9 -9
- package/dist/index.cjs +44 -32
- package/dist/index.d.cts +2 -0
- package/dist/index.d.ts +2 -0
- package/dist/index.mjs +44 -32
- package/package.json +1 -1
package/README.md
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
# vite-plugin-build-version
|
|
1
|
+
# vite-plugin-build-version-file
|
|
2
2
|
|
|
3
3
|
一个面向 `Vue 3 + Vite` 的构建版本插件。
|
|
4
4
|
|
|
@@ -24,13 +24,13 @@
|
|
|
24
24
|
## 安装
|
|
25
25
|
|
|
26
26
|
```bash
|
|
27
|
-
pnpm add vite-plugin-build-version
|
|
27
|
+
pnpm add vite-plugin-build-version-file
|
|
28
28
|
```
|
|
29
29
|
|
|
30
30
|
也可以使用:
|
|
31
31
|
|
|
32
32
|
```bash
|
|
33
|
-
npm install vite-plugin-build-version
|
|
33
|
+
npm install vite-plugin-build-version-file
|
|
34
34
|
```
|
|
35
35
|
|
|
36
36
|
## 快速开始
|
|
@@ -38,7 +38,7 @@ npm install vite-plugin-build-version
|
|
|
38
38
|
```ts
|
|
39
39
|
import { defineConfig } from 'vite'
|
|
40
40
|
import vue from '@vitejs/plugin-vue'
|
|
41
|
-
import buildVersionPlugin from 'vite-plugin-build-version'
|
|
41
|
+
import buildVersionPlugin from 'vite-plugin-build-version-file'
|
|
42
42
|
|
|
43
43
|
export default defineConfig({
|
|
44
44
|
plugins: [vue(), buildVersionPlugin()]
|
|
@@ -148,7 +148,7 @@ type BuildVersionPluginOptions = {
|
|
|
148
148
|
```ts
|
|
149
149
|
import { defineConfig } from 'vite'
|
|
150
150
|
import vue from '@vitejs/plugin-vue'
|
|
151
|
-
import buildVersionPlugin from 'vite-plugin-build-version'
|
|
151
|
+
import buildVersionPlugin from 'vite-plugin-build-version-file'
|
|
152
152
|
|
|
153
153
|
export default defineConfig({
|
|
154
154
|
plugins: [vue(), buildVersionPlugin()]
|
|
@@ -160,7 +160,7 @@ export default defineConfig({
|
|
|
160
160
|
```ts
|
|
161
161
|
import { defineConfig } from 'vite'
|
|
162
162
|
import vue from '@vitejs/plugin-vue'
|
|
163
|
-
import buildVersionPlugin from 'vite-plugin-build-version'
|
|
163
|
+
import buildVersionPlugin from 'vite-plugin-build-version-file'
|
|
164
164
|
|
|
165
165
|
export default defineConfig({
|
|
166
166
|
plugins: [
|
|
@@ -183,7 +183,7 @@ const version = window['__APP_VERSION__']
|
|
|
183
183
|
```ts
|
|
184
184
|
import { defineConfig } from 'vite'
|
|
185
185
|
import vue from '@vitejs/plugin-vue'
|
|
186
|
-
import buildVersionPlugin from 'vite-plugin-build-version'
|
|
186
|
+
import buildVersionPlugin from 'vite-plugin-build-version-file'
|
|
187
187
|
|
|
188
188
|
export default defineConfig({
|
|
189
189
|
plugins: [
|
|
@@ -200,7 +200,7 @@ export default defineConfig({
|
|
|
200
200
|
```ts
|
|
201
201
|
import { defineConfig } from 'vite'
|
|
202
202
|
import vue from '@vitejs/plugin-vue'
|
|
203
|
-
import buildVersionPlugin from 'vite-plugin-build-version'
|
|
203
|
+
import buildVersionPlugin from 'vite-plugin-build-version-file'
|
|
204
204
|
|
|
205
205
|
export default defineConfig({
|
|
206
206
|
plugins: [
|
|
@@ -223,7 +223,7 @@ dist/meta/version.json
|
|
|
223
223
|
```ts
|
|
224
224
|
import { defineConfig } from 'vite'
|
|
225
225
|
import vue from '@vitejs/plugin-vue'
|
|
226
|
-
import buildVersionPlugin from 'vite-plugin-build-version'
|
|
226
|
+
import buildVersionPlugin from 'vite-plugin-build-version-file'
|
|
227
227
|
|
|
228
228
|
export default defineConfig({
|
|
229
229
|
plugins: [
|
package/dist/index.cjs
CHANGED
|
@@ -24,10 +24,14 @@ __export(index_exports, {
|
|
|
24
24
|
default: () => index_default
|
|
25
25
|
});
|
|
26
26
|
module.exports = __toCommonJS(index_exports);
|
|
27
|
-
var PLUGIN_NAME = "vite-plugin-build-version";
|
|
27
|
+
var PLUGIN_NAME = "vite-plugin-build-version-file";
|
|
28
28
|
var DEFAULT_FILENAME = "version.json";
|
|
29
29
|
var DEFAULT_GLOBAL_NAME = "__VERSION__";
|
|
30
|
-
function
|
|
30
|
+
function createDefaultVersion(command) {
|
|
31
|
+
if (command === "serve") {
|
|
32
|
+
return 0;
|
|
33
|
+
}
|
|
34
|
+
const date = /* @__PURE__ */ new Date();
|
|
31
35
|
const pad = (value) => value.toString().padStart(2, "0");
|
|
32
36
|
return [
|
|
33
37
|
date.getFullYear(),
|
|
@@ -38,12 +42,6 @@ function formatBuildTimestamp(date) {
|
|
|
38
42
|
pad(date.getSeconds())
|
|
39
43
|
].join("");
|
|
40
44
|
}
|
|
41
|
-
function createDefaultVersion(command) {
|
|
42
|
-
if (command === "serve") {
|
|
43
|
-
return 0;
|
|
44
|
-
}
|
|
45
|
-
return formatBuildTimestamp(/* @__PURE__ */ new Date());
|
|
46
|
-
}
|
|
47
45
|
function normalizeFileName(filename) {
|
|
48
46
|
const normalized = filename.replace(/\\/g, "/").replace(/^\/+/, "");
|
|
49
47
|
if (!normalized || normalized.endsWith("/")) {
|
|
@@ -57,28 +55,6 @@ function normalizeGlobalName(globalName) {
|
|
|
57
55
|
}
|
|
58
56
|
return globalName;
|
|
59
57
|
}
|
|
60
|
-
function ensureVersionValue(value) {
|
|
61
|
-
if (typeof value === "string" || typeof value === "number") {
|
|
62
|
-
return value;
|
|
63
|
-
}
|
|
64
|
-
throw new Error(
|
|
65
|
-
`[${PLUGIN_NAME}] "version" must resolve to a string or number.`
|
|
66
|
-
);
|
|
67
|
-
}
|
|
68
|
-
function resolveVersionValue(versionOption, context) {
|
|
69
|
-
if (versionOption === void 0) {
|
|
70
|
-
return createDefaultVersion(context.command);
|
|
71
|
-
}
|
|
72
|
-
if (typeof versionOption !== "function") {
|
|
73
|
-
return ensureVersionValue(versionOption);
|
|
74
|
-
}
|
|
75
|
-
try {
|
|
76
|
-
return ensureVersionValue(versionOption(context));
|
|
77
|
-
} catch (error) {
|
|
78
|
-
const message = error instanceof Error ? error.message : String(error);
|
|
79
|
-
throw new Error(`[${PLUGIN_NAME}] Failed to resolve version: ${message}`);
|
|
80
|
-
}
|
|
81
|
-
}
|
|
82
58
|
function serializePayload(payload) {
|
|
83
59
|
return `${JSON.stringify(payload, null, 2)}
|
|
84
60
|
`;
|
|
@@ -104,15 +80,45 @@ function buildVersionPlugin(options = {}) {
|
|
|
104
80
|
let payload;
|
|
105
81
|
return {
|
|
106
82
|
name: PLUGIN_NAME,
|
|
83
|
+
config(config, { command }) {
|
|
84
|
+
const context = {
|
|
85
|
+
command,
|
|
86
|
+
mode: config.mode || "development"
|
|
87
|
+
};
|
|
88
|
+
let version;
|
|
89
|
+
if (typeof options.version === "function") {
|
|
90
|
+
version = options.version(context);
|
|
91
|
+
} else if (options.version !== void 0) {
|
|
92
|
+
version = options.version;
|
|
93
|
+
} else {
|
|
94
|
+
version = createDefaultVersion(context.command);
|
|
95
|
+
}
|
|
96
|
+
return {
|
|
97
|
+
define: {
|
|
98
|
+
[`import.meta.env.${globalName}`]: JSON.stringify(version)
|
|
99
|
+
}
|
|
100
|
+
};
|
|
101
|
+
},
|
|
107
102
|
configResolved(config) {
|
|
108
103
|
const context = {
|
|
109
104
|
command: config.command,
|
|
110
105
|
mode: config.mode
|
|
111
106
|
};
|
|
107
|
+
let version;
|
|
108
|
+
if (typeof options.version === "function") {
|
|
109
|
+
version = options.version(context);
|
|
110
|
+
} else if (options.version !== void 0) {
|
|
111
|
+
version = options.version;
|
|
112
|
+
} else {
|
|
113
|
+
version = createDefaultVersion(context.command);
|
|
114
|
+
}
|
|
112
115
|
payload = {
|
|
113
|
-
version
|
|
116
|
+
version,
|
|
114
117
|
production: config.command === "build"
|
|
115
118
|
};
|
|
119
|
+
if (options.payload) {
|
|
120
|
+
payload = options.payload(payload);
|
|
121
|
+
}
|
|
116
122
|
},
|
|
117
123
|
configureServer(server) {
|
|
118
124
|
const requestPath = toRequestPath(server.config.base, fileName);
|
|
@@ -140,7 +146,7 @@ function buildVersionPlugin(options = {}) {
|
|
|
140
146
|
{
|
|
141
147
|
tag: "script",
|
|
142
148
|
injectTo: "head",
|
|
143
|
-
children: `window[${JSON.stringify(globalName)}] = ${JSON.stringify(payload
|
|
149
|
+
children: `window[${JSON.stringify(globalName)}] = ${JSON.stringify(payload)};`
|
|
144
150
|
}
|
|
145
151
|
];
|
|
146
152
|
return tags;
|
|
@@ -154,6 +160,12 @@ function buildVersionPlugin(options = {}) {
|
|
|
154
160
|
fileName,
|
|
155
161
|
source: serializePayload(payload)
|
|
156
162
|
});
|
|
163
|
+
if (options.log) {
|
|
164
|
+
console.log(options.log(payload));
|
|
165
|
+
} else {
|
|
166
|
+
console.log("[vite-plugin-build-version-file][build finished]");
|
|
167
|
+
console.log(serializePayload(payload));
|
|
168
|
+
}
|
|
157
169
|
}
|
|
158
170
|
};
|
|
159
171
|
}
|
package/dist/index.d.cts
CHANGED
|
@@ -13,6 +13,8 @@ interface BuildVersionPluginOptions {
|
|
|
13
13
|
globalName?: string;
|
|
14
14
|
injectToHtml?: boolean;
|
|
15
15
|
version?: string | number | ((ctx: BuildVersionContext) => string | number);
|
|
16
|
+
log?: (content: any) => string;
|
|
17
|
+
payload?: (json: any) => any;
|
|
16
18
|
}
|
|
17
19
|
declare function buildVersionPlugin(options?: BuildVersionPluginOptions): Plugin;
|
|
18
20
|
|
package/dist/index.d.ts
CHANGED
|
@@ -13,6 +13,8 @@ interface BuildVersionPluginOptions {
|
|
|
13
13
|
globalName?: string;
|
|
14
14
|
injectToHtml?: boolean;
|
|
15
15
|
version?: string | number | ((ctx: BuildVersionContext) => string | number);
|
|
16
|
+
log?: (content: any) => string;
|
|
17
|
+
payload?: (json: any) => any;
|
|
16
18
|
}
|
|
17
19
|
declare function buildVersionPlugin(options?: BuildVersionPluginOptions): Plugin;
|
|
18
20
|
|
package/dist/index.mjs
CHANGED
|
@@ -1,8 +1,12 @@
|
|
|
1
1
|
// src/index.ts
|
|
2
|
-
var PLUGIN_NAME = "vite-plugin-build-version";
|
|
2
|
+
var PLUGIN_NAME = "vite-plugin-build-version-file";
|
|
3
3
|
var DEFAULT_FILENAME = "version.json";
|
|
4
4
|
var DEFAULT_GLOBAL_NAME = "__VERSION__";
|
|
5
|
-
function
|
|
5
|
+
function createDefaultVersion(command) {
|
|
6
|
+
if (command === "serve") {
|
|
7
|
+
return 0;
|
|
8
|
+
}
|
|
9
|
+
const date = /* @__PURE__ */ new Date();
|
|
6
10
|
const pad = (value) => value.toString().padStart(2, "0");
|
|
7
11
|
return [
|
|
8
12
|
date.getFullYear(),
|
|
@@ -13,12 +17,6 @@ function formatBuildTimestamp(date) {
|
|
|
13
17
|
pad(date.getSeconds())
|
|
14
18
|
].join("");
|
|
15
19
|
}
|
|
16
|
-
function createDefaultVersion(command) {
|
|
17
|
-
if (command === "serve") {
|
|
18
|
-
return 0;
|
|
19
|
-
}
|
|
20
|
-
return formatBuildTimestamp(/* @__PURE__ */ new Date());
|
|
21
|
-
}
|
|
22
20
|
function normalizeFileName(filename) {
|
|
23
21
|
const normalized = filename.replace(/\\/g, "/").replace(/^\/+/, "");
|
|
24
22
|
if (!normalized || normalized.endsWith("/")) {
|
|
@@ -32,28 +30,6 @@ function normalizeGlobalName(globalName) {
|
|
|
32
30
|
}
|
|
33
31
|
return globalName;
|
|
34
32
|
}
|
|
35
|
-
function ensureVersionValue(value) {
|
|
36
|
-
if (typeof value === "string" || typeof value === "number") {
|
|
37
|
-
return value;
|
|
38
|
-
}
|
|
39
|
-
throw new Error(
|
|
40
|
-
`[${PLUGIN_NAME}] "version" must resolve to a string or number.`
|
|
41
|
-
);
|
|
42
|
-
}
|
|
43
|
-
function resolveVersionValue(versionOption, context) {
|
|
44
|
-
if (versionOption === void 0) {
|
|
45
|
-
return createDefaultVersion(context.command);
|
|
46
|
-
}
|
|
47
|
-
if (typeof versionOption !== "function") {
|
|
48
|
-
return ensureVersionValue(versionOption);
|
|
49
|
-
}
|
|
50
|
-
try {
|
|
51
|
-
return ensureVersionValue(versionOption(context));
|
|
52
|
-
} catch (error) {
|
|
53
|
-
const message = error instanceof Error ? error.message : String(error);
|
|
54
|
-
throw new Error(`[${PLUGIN_NAME}] Failed to resolve version: ${message}`);
|
|
55
|
-
}
|
|
56
|
-
}
|
|
57
33
|
function serializePayload(payload) {
|
|
58
34
|
return `${JSON.stringify(payload, null, 2)}
|
|
59
35
|
`;
|
|
@@ -79,15 +55,45 @@ function buildVersionPlugin(options = {}) {
|
|
|
79
55
|
let payload;
|
|
80
56
|
return {
|
|
81
57
|
name: PLUGIN_NAME,
|
|
58
|
+
config(config, { command }) {
|
|
59
|
+
const context = {
|
|
60
|
+
command,
|
|
61
|
+
mode: config.mode || "development"
|
|
62
|
+
};
|
|
63
|
+
let version;
|
|
64
|
+
if (typeof options.version === "function") {
|
|
65
|
+
version = options.version(context);
|
|
66
|
+
} else if (options.version !== void 0) {
|
|
67
|
+
version = options.version;
|
|
68
|
+
} else {
|
|
69
|
+
version = createDefaultVersion(context.command);
|
|
70
|
+
}
|
|
71
|
+
return {
|
|
72
|
+
define: {
|
|
73
|
+
[`import.meta.env.${globalName}`]: JSON.stringify(version)
|
|
74
|
+
}
|
|
75
|
+
};
|
|
76
|
+
},
|
|
82
77
|
configResolved(config) {
|
|
83
78
|
const context = {
|
|
84
79
|
command: config.command,
|
|
85
80
|
mode: config.mode
|
|
86
81
|
};
|
|
82
|
+
let version;
|
|
83
|
+
if (typeof options.version === "function") {
|
|
84
|
+
version = options.version(context);
|
|
85
|
+
} else if (options.version !== void 0) {
|
|
86
|
+
version = options.version;
|
|
87
|
+
} else {
|
|
88
|
+
version = createDefaultVersion(context.command);
|
|
89
|
+
}
|
|
87
90
|
payload = {
|
|
88
|
-
version
|
|
91
|
+
version,
|
|
89
92
|
production: config.command === "build"
|
|
90
93
|
};
|
|
94
|
+
if (options.payload) {
|
|
95
|
+
payload = options.payload(payload);
|
|
96
|
+
}
|
|
91
97
|
},
|
|
92
98
|
configureServer(server) {
|
|
93
99
|
const requestPath = toRequestPath(server.config.base, fileName);
|
|
@@ -115,7 +121,7 @@ function buildVersionPlugin(options = {}) {
|
|
|
115
121
|
{
|
|
116
122
|
tag: "script",
|
|
117
123
|
injectTo: "head",
|
|
118
|
-
children: `window[${JSON.stringify(globalName)}] = ${JSON.stringify(payload
|
|
124
|
+
children: `window[${JSON.stringify(globalName)}] = ${JSON.stringify(payload)};`
|
|
119
125
|
}
|
|
120
126
|
];
|
|
121
127
|
return tags;
|
|
@@ -129,6 +135,12 @@ function buildVersionPlugin(options = {}) {
|
|
|
129
135
|
fileName,
|
|
130
136
|
source: serializePayload(payload)
|
|
131
137
|
});
|
|
138
|
+
if (options.log) {
|
|
139
|
+
console.log(options.log(payload));
|
|
140
|
+
} else {
|
|
141
|
+
console.log("[vite-plugin-build-version-file][build finished]");
|
|
142
|
+
console.log(serializePayload(payload));
|
|
143
|
+
}
|
|
132
144
|
}
|
|
133
145
|
};
|
|
134
146
|
}
|