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

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/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 {};
@@ -104,6 +129,19 @@ function serializePayload(payload) {
104
129
  return `${JSON.stringify(payload, null, 2)}
105
130
  `;
106
131
  }
132
+ function createInjectScript(globalName, payload) {
133
+ return `window[${JSON.stringify(globalName)}] = ${JSON.stringify(payload)};`;
134
+ }
135
+ function injectScriptToHead(code, script) {
136
+ const scriptTag = `<script>${script}</script>`;
137
+ if (code.includes(script)) {
138
+ return code;
139
+ }
140
+ if (code.includes("</head>")) {
141
+ return code.replace("</head>", `${scriptTag}</head>`);
142
+ }
143
+ return `${scriptTag}${code}`;
144
+ }
107
145
  function parsePathname(requestUrl) {
108
146
  if (!requestUrl) {
109
147
  return "/";
@@ -116,20 +154,20 @@ function toRequestPath(base, fileName) {
116
154
  const baseWithoutTrailingSlash = withLeadingSlash.endsWith("/") ? withLeadingSlash.slice(0, -1) : withLeadingSlash;
117
155
  return `${baseWithoutTrailingSlash}/${fileName}`.replace(/\/{2,}/g, "/");
118
156
  }
119
- function resolveVersion(options, context) {
157
+ function resolveVersion(options, context, timeZone) {
120
158
  if (typeof options.version === "function") {
121
159
  return options.version(context);
122
160
  }
123
161
  if (options.version !== void 0) {
124
162
  return options.version;
125
163
  }
126
- return createDefaultVersion(context.command);
164
+ return createDefaultVersion(context.command, timeZone);
127
165
  }
128
- function createPayload(options, context, data, packageInfo = {}) {
166
+ function createPayload(options, context, data, timeZone, packageInfo = {}) {
129
167
  let nextPayload = {
130
168
  ...data,
131
169
  ...packageInfo,
132
- time: resolveVersion(options, context),
170
+ time: resolveVersion(options, context, timeZone),
133
171
  env: context.mode
134
172
  };
135
173
  if (options.payload) {
@@ -143,9 +181,16 @@ function buildVersionPlugin(options = {}) {
143
181
  options.globalName ?? DEFAULT_GLOBAL_NAME
144
182
  );
145
183
  const injectToHtml = options.injectToHtml ?? true;
184
+ const timeZone = normalizeTimeZone(options.timeZone ?? DEFAULT_TIME_ZONE);
146
185
  const data = normalizeData(options.data);
147
186
  let payload;
148
- return {
187
+ const transformHtml = (code, ..._args) => {
188
+ if (!injectToHtml || !payload) {
189
+ return void 0;
190
+ }
191
+ return injectScriptToHead(code, createInjectScript(globalName, payload));
192
+ };
193
+ const plugin = {
149
194
  name: PLUGIN_NAME,
150
195
  configResolved(config) {
151
196
  const context = {
@@ -153,7 +198,7 @@ function buildVersionPlugin(options = {}) {
153
198
  mode: config.mode
154
199
  };
155
200
  const packageInfo = readPackageInfo(config.root);
156
- payload = createPayload(options, context, data, packageInfo);
201
+ payload = createPayload(options, context, data, timeZone, packageInfo);
157
202
  },
158
203
  configureServer(server) {
159
204
  const requestPath = toRequestPath(server.config.base, fileName);
@@ -181,7 +226,7 @@ function buildVersionPlugin(options = {}) {
181
226
  {
182
227
  tag: "script",
183
228
  injectTo: "head",
184
- children: `window[${JSON.stringify(globalName)}] = ${JSON.stringify(payload)};`
229
+ children: createInjectScript(globalName, payload)
185
230
  }
186
231
  ];
187
232
  return tags;
@@ -203,6 +248,10 @@ function buildVersionPlugin(options = {}) {
203
248
  }
204
249
  }
205
250
  };
251
+ Object.defineProperty(plugin, "transformHtml", {
252
+ value: transformHtml
253
+ });
254
+ return plugin;
206
255
  }
207
256
  var index_default = buildVersionPlugin;
208
257
  // Annotate the CommonJS export names for ESM import in node:
package/dist/index.d.cts CHANGED
@@ -13,11 +13,15 @@ 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;
19
20
  payload?: (json: any) => any;
20
21
  }
21
- declare function buildVersionPlugin(options?: BuildVersionPluginOptions): Plugin;
22
+ type BuildVersionPlugin = Plugin & {
23
+ transformHtml: (code: string, ...args: unknown[]) => string | undefined;
24
+ };
25
+ declare function buildVersionPlugin(options?: BuildVersionPluginOptions): BuildVersionPlugin;
22
26
 
23
- export { type BuildVersionContext, type BuildVersionPayload, type BuildVersionPluginOptions, buildVersionPlugin, buildVersionPlugin as default };
27
+ export { type BuildVersionContext, type BuildVersionPayload, type BuildVersionPlugin, type BuildVersionPluginOptions, buildVersionPlugin, buildVersionPlugin as default };
package/dist/index.d.ts CHANGED
@@ -13,11 +13,15 @@ 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;
19
20
  payload?: (json: any) => any;
20
21
  }
21
- declare function buildVersionPlugin(options?: BuildVersionPluginOptions): Plugin;
22
+ type BuildVersionPlugin = Plugin & {
23
+ transformHtml: (code: string, ...args: unknown[]) => string | undefined;
24
+ };
25
+ declare function buildVersionPlugin(options?: BuildVersionPluginOptions): BuildVersionPlugin;
22
26
 
23
- export { type BuildVersionContext, type BuildVersionPayload, type BuildVersionPluginOptions, buildVersionPlugin, buildVersionPlugin as default };
27
+ export { type BuildVersionContext, type BuildVersionPayload, type BuildVersionPlugin, type BuildVersionPluginOptions, buildVersionPlugin, buildVersionPlugin as default };
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 {};
@@ -79,6 +104,19 @@ function serializePayload(payload) {
79
104
  return `${JSON.stringify(payload, null, 2)}
80
105
  `;
81
106
  }
107
+ function createInjectScript(globalName, payload) {
108
+ return `window[${JSON.stringify(globalName)}] = ${JSON.stringify(payload)};`;
109
+ }
110
+ function injectScriptToHead(code, script) {
111
+ const scriptTag = `<script>${script}</script>`;
112
+ if (code.includes(script)) {
113
+ return code;
114
+ }
115
+ if (code.includes("</head>")) {
116
+ return code.replace("</head>", `${scriptTag}</head>`);
117
+ }
118
+ return `${scriptTag}${code}`;
119
+ }
82
120
  function parsePathname(requestUrl) {
83
121
  if (!requestUrl) {
84
122
  return "/";
@@ -91,20 +129,20 @@ function toRequestPath(base, fileName) {
91
129
  const baseWithoutTrailingSlash = withLeadingSlash.endsWith("/") ? withLeadingSlash.slice(0, -1) : withLeadingSlash;
92
130
  return `${baseWithoutTrailingSlash}/${fileName}`.replace(/\/{2,}/g, "/");
93
131
  }
94
- function resolveVersion(options, context) {
132
+ function resolveVersion(options, context, timeZone) {
95
133
  if (typeof options.version === "function") {
96
134
  return options.version(context);
97
135
  }
98
136
  if (options.version !== void 0) {
99
137
  return options.version;
100
138
  }
101
- return createDefaultVersion(context.command);
139
+ return createDefaultVersion(context.command, timeZone);
102
140
  }
103
- function createPayload(options, context, data, packageInfo = {}) {
141
+ function createPayload(options, context, data, timeZone, packageInfo = {}) {
104
142
  let nextPayload = {
105
143
  ...data,
106
144
  ...packageInfo,
107
- time: resolveVersion(options, context),
145
+ time: resolveVersion(options, context, timeZone),
108
146
  env: context.mode
109
147
  };
110
148
  if (options.payload) {
@@ -118,9 +156,16 @@ function buildVersionPlugin(options = {}) {
118
156
  options.globalName ?? DEFAULT_GLOBAL_NAME
119
157
  );
120
158
  const injectToHtml = options.injectToHtml ?? true;
159
+ const timeZone = normalizeTimeZone(options.timeZone ?? DEFAULT_TIME_ZONE);
121
160
  const data = normalizeData(options.data);
122
161
  let payload;
123
- return {
162
+ const transformHtml = (code, ..._args) => {
163
+ if (!injectToHtml || !payload) {
164
+ return void 0;
165
+ }
166
+ return injectScriptToHead(code, createInjectScript(globalName, payload));
167
+ };
168
+ const plugin = {
124
169
  name: PLUGIN_NAME,
125
170
  configResolved(config) {
126
171
  const context = {
@@ -128,7 +173,7 @@ function buildVersionPlugin(options = {}) {
128
173
  mode: config.mode
129
174
  };
130
175
  const packageInfo = readPackageInfo(config.root);
131
- payload = createPayload(options, context, data, packageInfo);
176
+ payload = createPayload(options, context, data, timeZone, packageInfo);
132
177
  },
133
178
  configureServer(server) {
134
179
  const requestPath = toRequestPath(server.config.base, fileName);
@@ -156,7 +201,7 @@ function buildVersionPlugin(options = {}) {
156
201
  {
157
202
  tag: "script",
158
203
  injectTo: "head",
159
- children: `window[${JSON.stringify(globalName)}] = ${JSON.stringify(payload)};`
204
+ children: createInjectScript(globalName, payload)
160
205
  }
161
206
  ];
162
207
  return tags;
@@ -178,6 +223,10 @@ function buildVersionPlugin(options = {}) {
178
223
  }
179
224
  }
180
225
  };
226
+ Object.defineProperty(plugin, "transformHtml", {
227
+ value: transformHtml
228
+ });
229
+ return plugin;
181
230
  }
182
231
  var index_default = buildVersionPlugin;
183
232
  export {
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.9",
4
4
  "description": "A Vite plugin that emits version.json and injects version metadata into HTML.",
5
5
  "license": "MIT",
6
6
  "type": "module",
@@ -23,7 +23,9 @@
23
23
  "scripts": {
24
24
  "build": "tsup --config tsup.config.ts",
25
25
  "build:fixture": "pnpm build && vite build --config debug/fixture-app/vite.config.ts",
26
+ "build:vitepress": "pnpm build && vitepress build debug/vitepress-app",
26
27
  "dev:fixture": "pnpm build && vite --config debug/fixture-app/vite.config.ts",
28
+ "dev:vitepress": "pnpm build && vitepress dev debug/vitepress-app",
27
29
  "prepublishOnly": "pnpm build:fixture"
28
30
  },
29
31
  "keywords": [
@@ -49,6 +51,7 @@
49
51
  "tsup": "^8.0.0",
50
52
  "typescript": "^5.0.0",
51
53
  "vite": "^5.0.0",
54
+ "vitepress": "^1.6.4",
52
55
  "vue": "^3.4.0"
53
56
  }
54
57
  }