vite-plugin-build-version-file 0.1.7 → 0.1.8

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 CHANGED
@@ -122,6 +122,7 @@ type BuildVersionPluginOptions = {
122
122
  filename?: string
123
123
  globalName?: string
124
124
  injectToHtml?: boolean
125
+ timeZone?: string
125
126
  version?: string | number | ((ctx: BuildVersionContext) => string | number)
126
127
  data?: Record<string, any>
127
128
  }
@@ -181,6 +182,18 @@ type BuildVersionPluginOptions = {
181
182
  }
182
183
  ```
183
184
 
185
+ ### `timeZone`
186
+
187
+ - 类型:`string`
188
+ - 默认值:`"Asia/Shanghai"`
189
+ - 作用:控制默认时间按哪个时区生成
190
+
191
+ 说明:
192
+
193
+ - 只影响插件默认生成的 `time`
194
+ - 如果构建环境是 UTC,也会按设置的时区输出
195
+ - 如果传了 `version`,则以你自己的 `version` 逻辑为准
196
+
184
197
  ### `data`
185
198
 
186
199
  - 类型:`Record<string, any>`
@@ -301,7 +314,24 @@ export default defineConfig({
301
314
  })
302
315
  ```
303
316
 
304
- ### 6. 追加自定义数据
317
+ ### 6. 指定时区生成时间
318
+
319
+ ```ts
320
+ import { defineConfig } from 'vite'
321
+ import vue from '@vitejs/plugin-vue'
322
+ import buildVersionPlugin from 'vite-plugin-build-version-file'
323
+
324
+ export default defineConfig({
325
+ plugins: [
326
+ vue(),
327
+ buildVersionPlugin({
328
+ timeZone: 'Asia/Shanghai'
329
+ })
330
+ ]
331
+ })
332
+ ```
333
+
334
+ ### 7. 追加自定义数据
305
335
 
306
336
  ```ts
307
337
  import { defineConfig } from 'vite'
package/dist/index.cjs CHANGED
@@ -29,19 +29,32 @@ 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
- function createDefaultVersion(command) {
32
+ var DEFAULT_TIME_ZONE = "Asia/Shanghai";
33
+ function createDefaultVersion(command, timeZone) {
33
34
  if (command === "serve") {
34
35
  return 0;
35
36
  }
36
- const date = /* @__PURE__ */ new Date();
37
- const pad = (value) => value.toString().padStart(2, "0");
37
+ const parts = new Intl.DateTimeFormat("en-US", {
38
+ timeZone,
39
+ year: "numeric",
40
+ month: "2-digit",
41
+ day: "2-digit",
42
+ hour: "2-digit",
43
+ minute: "2-digit",
44
+ second: "2-digit",
45
+ hour12: false,
46
+ hourCycle: "h23"
47
+ }).formatToParts(/* @__PURE__ */ new Date());
48
+ const getPartValue = (type) => {
49
+ return parts.find((part) => part.type === type)?.value ?? "";
50
+ };
38
51
  return [
39
- date.getFullYear(),
40
- pad(date.getMonth() + 1),
41
- pad(date.getDate()),
42
- pad(date.getHours()),
43
- pad(date.getMinutes()),
44
- pad(date.getSeconds())
52
+ getPartValue("year"),
53
+ getPartValue("month"),
54
+ getPartValue("day"),
55
+ getPartValue("hour"),
56
+ getPartValue("minute"),
57
+ getPartValue("second")
45
58
  ].join("");
46
59
  }
47
60
  function normalizeFileName(filename) {
@@ -57,6 +70,18 @@ function normalizeGlobalName(globalName) {
57
70
  }
58
71
  return globalName;
59
72
  }
73
+ function normalizeTimeZone(timeZone) {
74
+ const normalized = timeZone.trim();
75
+ if (!normalized) {
76
+ throw new Error(`[${PLUGIN_NAME}] "timeZone" cannot be empty.`);
77
+ }
78
+ try {
79
+ new Intl.DateTimeFormat("en-US", { timeZone: normalized }).format(/* @__PURE__ */ new Date());
80
+ } catch {
81
+ throw new Error(`[${PLUGIN_NAME}] "timeZone" is invalid.`);
82
+ }
83
+ return normalized;
84
+ }
60
85
  function normalizeData(data) {
61
86
  if (data === void 0) {
62
87
  return {};
@@ -116,20 +141,20 @@ function toRequestPath(base, fileName) {
116
141
  const baseWithoutTrailingSlash = withLeadingSlash.endsWith("/") ? withLeadingSlash.slice(0, -1) : withLeadingSlash;
117
142
  return `${baseWithoutTrailingSlash}/${fileName}`.replace(/\/{2,}/g, "/");
118
143
  }
119
- function resolveVersion(options, context) {
144
+ function resolveVersion(options, context, timeZone) {
120
145
  if (typeof options.version === "function") {
121
146
  return options.version(context);
122
147
  }
123
148
  if (options.version !== void 0) {
124
149
  return options.version;
125
150
  }
126
- return createDefaultVersion(context.command);
151
+ return createDefaultVersion(context.command, timeZone);
127
152
  }
128
- function createPayload(options, context, data, packageInfo = {}) {
153
+ function createPayload(options, context, data, timeZone, packageInfo = {}) {
129
154
  let nextPayload = {
130
155
  ...data,
131
156
  ...packageInfo,
132
- time: resolveVersion(options, context),
157
+ time: resolveVersion(options, context, timeZone),
133
158
  env: context.mode
134
159
  };
135
160
  if (options.payload) {
@@ -143,6 +168,7 @@ function buildVersionPlugin(options = {}) {
143
168
  options.globalName ?? DEFAULT_GLOBAL_NAME
144
169
  );
145
170
  const injectToHtml = options.injectToHtml ?? true;
171
+ const timeZone = normalizeTimeZone(options.timeZone ?? DEFAULT_TIME_ZONE);
146
172
  const data = normalizeData(options.data);
147
173
  let payload;
148
174
  return {
@@ -153,7 +179,7 @@ function buildVersionPlugin(options = {}) {
153
179
  mode: config.mode
154
180
  };
155
181
  const packageInfo = readPackageInfo(config.root);
156
- payload = createPayload(options, context, data, packageInfo);
182
+ payload = createPayload(options, context, data, timeZone, packageInfo);
157
183
  },
158
184
  configureServer(server) {
159
185
  const requestPath = toRequestPath(server.config.base, fileName);
package/dist/index.d.cts CHANGED
@@ -13,6 +13,7 @@ interface BuildVersionPluginOptions {
13
13
  filename?: string;
14
14
  globalName?: string;
15
15
  injectToHtml?: boolean;
16
+ timeZone?: string;
16
17
  version?: string | number | ((ctx: BuildVersionContext) => string | number);
17
18
  data?: Record<string, any>;
18
19
  log?: (content: any) => string;
package/dist/index.d.ts CHANGED
@@ -13,6 +13,7 @@ interface BuildVersionPluginOptions {
13
13
  filename?: string;
14
14
  globalName?: string;
15
15
  injectToHtml?: boolean;
16
+ timeZone?: string;
16
17
  version?: string | number | ((ctx: BuildVersionContext) => string | number);
17
18
  data?: Record<string, any>;
18
19
  log?: (content: any) => string;
package/dist/index.mjs CHANGED
@@ -4,19 +4,32 @@ import { dirname, join } 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
- function createDefaultVersion(command) {
7
+ var DEFAULT_TIME_ZONE = "Asia/Shanghai";
8
+ function createDefaultVersion(command, timeZone) {
8
9
  if (command === "serve") {
9
10
  return 0;
10
11
  }
11
- const date = /* @__PURE__ */ new Date();
12
- const pad = (value) => value.toString().padStart(2, "0");
12
+ const parts = new Intl.DateTimeFormat("en-US", {
13
+ timeZone,
14
+ year: "numeric",
15
+ month: "2-digit",
16
+ day: "2-digit",
17
+ hour: "2-digit",
18
+ minute: "2-digit",
19
+ second: "2-digit",
20
+ hour12: false,
21
+ hourCycle: "h23"
22
+ }).formatToParts(/* @__PURE__ */ new Date());
23
+ const getPartValue = (type) => {
24
+ return parts.find((part) => part.type === type)?.value ?? "";
25
+ };
13
26
  return [
14
- date.getFullYear(),
15
- pad(date.getMonth() + 1),
16
- pad(date.getDate()),
17
- pad(date.getHours()),
18
- pad(date.getMinutes()),
19
- pad(date.getSeconds())
27
+ getPartValue("year"),
28
+ getPartValue("month"),
29
+ getPartValue("day"),
30
+ getPartValue("hour"),
31
+ getPartValue("minute"),
32
+ getPartValue("second")
20
33
  ].join("");
21
34
  }
22
35
  function normalizeFileName(filename) {
@@ -32,6 +45,18 @@ function normalizeGlobalName(globalName) {
32
45
  }
33
46
  return globalName;
34
47
  }
48
+ function normalizeTimeZone(timeZone) {
49
+ const normalized = timeZone.trim();
50
+ if (!normalized) {
51
+ throw new Error(`[${PLUGIN_NAME}] "timeZone" cannot be empty.`);
52
+ }
53
+ try {
54
+ new Intl.DateTimeFormat("en-US", { timeZone: normalized }).format(/* @__PURE__ */ new Date());
55
+ } catch {
56
+ throw new Error(`[${PLUGIN_NAME}] "timeZone" is invalid.`);
57
+ }
58
+ return normalized;
59
+ }
35
60
  function normalizeData(data) {
36
61
  if (data === void 0) {
37
62
  return {};
@@ -91,20 +116,20 @@ function toRequestPath(base, fileName) {
91
116
  const baseWithoutTrailingSlash = withLeadingSlash.endsWith("/") ? withLeadingSlash.slice(0, -1) : withLeadingSlash;
92
117
  return `${baseWithoutTrailingSlash}/${fileName}`.replace(/\/{2,}/g, "/");
93
118
  }
94
- function resolveVersion(options, context) {
119
+ function resolveVersion(options, context, timeZone) {
95
120
  if (typeof options.version === "function") {
96
121
  return options.version(context);
97
122
  }
98
123
  if (options.version !== void 0) {
99
124
  return options.version;
100
125
  }
101
- return createDefaultVersion(context.command);
126
+ return createDefaultVersion(context.command, timeZone);
102
127
  }
103
- function createPayload(options, context, data, packageInfo = {}) {
128
+ function createPayload(options, context, data, timeZone, packageInfo = {}) {
104
129
  let nextPayload = {
105
130
  ...data,
106
131
  ...packageInfo,
107
- time: resolveVersion(options, context),
132
+ time: resolveVersion(options, context, timeZone),
108
133
  env: context.mode
109
134
  };
110
135
  if (options.payload) {
@@ -118,6 +143,7 @@ function buildVersionPlugin(options = {}) {
118
143
  options.globalName ?? DEFAULT_GLOBAL_NAME
119
144
  );
120
145
  const injectToHtml = options.injectToHtml ?? true;
146
+ const timeZone = normalizeTimeZone(options.timeZone ?? DEFAULT_TIME_ZONE);
121
147
  const data = normalizeData(options.data);
122
148
  let payload;
123
149
  return {
@@ -128,7 +154,7 @@ function buildVersionPlugin(options = {}) {
128
154
  mode: config.mode
129
155
  };
130
156
  const packageInfo = readPackageInfo(config.root);
131
- payload = createPayload(options, context, data, packageInfo);
157
+ payload = createPayload(options, context, data, timeZone, packageInfo);
132
158
  },
133
159
  configureServer(server) {
134
160
  const requestPath = toRequestPath(server.config.base, fileName);
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "vite-plugin-build-version-file",
3
- "version": "0.1.7",
3
+ "version": "0.1.8",
4
4
  "description": "A Vite plugin that emits version.json and injects version metadata into HTML.",
5
5
  "license": "MIT",
6
6
  "type": "module",