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

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
@@ -6,7 +6,6 @@
6
6
 
7
7
  - 构建产物中生成 `version.json`
8
8
  - 可选地向 HTML 注入 `window["__VERSION__"]`
9
- - 通过 `import.meta.env.__VERSION__` 在代码中直接读取版本号
10
9
  - 支持自定义版本号、文件名、全局变量名和额外数据
11
10
 
12
11
  这个插件只负责产出和暴露版本信息。
@@ -107,7 +106,7 @@ export default defineConfig({
107
106
 
108
107
  如果你的项目要做“检测到新版本后提示刷新”,推荐做法是:
109
108
 
110
- - 用 `import.meta.env.__VERSION__` 读取当前页面版本
109
+ - 用 `window["__VERSION__"]` 读取当前页面版本
111
110
  - 请求 `version.json` 读取服务端版本
112
111
  - 在业务项目里自行决定提示方式和刷新时机
113
112
 
@@ -337,36 +336,9 @@ export default defineConfig({
337
336
 
338
337
  ## 在项目中读取版本
339
338
 
340
- 插件提供三种方式读取版本号,可根据场景选择:
339
+ 插件提供两种方式读取版本号,可根据场景选择:
341
340
 
342
- ### 方式一:通过 `import.meta.env` 读取(推荐)
343
-
344
- 版本号会被注入到 `import.meta.env` 中,可以直接在代码中使用:
345
-
346
- ```ts
347
- const version = import.meta.env.__VERSION__
348
- console.log(version) // '20260401153045' 或 '1.2.3'
349
- ```
350
-
351
- **优势:**
352
- - 无需等待页面加载或发起请求
353
- - 类型安全,可在 `vite-env.d.ts` 中声明类型
354
- - 适合在应用初始化时读取版本
355
-
356
- **TypeScript 类型声明:**
357
-
358
- ```ts
359
- // vite-env.d.ts
360
- interface ImportMetaEnv {
361
- readonly __VERSION__: string
362
- }
363
-
364
- interface ImportMeta {
365
- readonly env: ImportMetaEnv
366
- }
367
- ```
368
-
369
- ### 方式二:读取注入的全局变量
341
+ ### 方式一:读取注入的全局变量
370
342
 
371
343
  ```ts
372
344
  const payload = window['__VERSION__']
@@ -381,7 +353,7 @@ console.log(payload.env)
381
353
  - 这里读到的是完整对象,不是单独字符串
382
354
  - 如果配置了 `data`,这里也可以直接读取对应字段
383
355
 
384
- ### 方式三:读取 `version.json`
356
+ ### 方式二:读取 `version.json`
385
357
 
386
358
  ```ts
387
359
  const response = await fetch('/version.json')
@@ -417,8 +389,9 @@ async function checkVersion() {
417
389
  })
418
390
 
419
391
  const payload = await response.json()
392
+ const currentPayload = window.__VERSION__
420
393
 
421
- if (payload.time !== import.meta.env.__VERSION__) {
394
+ if (currentPayload && payload.time !== currentPayload.time) {
422
395
  const shouldReload = await showUpdatePrompt()
423
396
 
424
397
  if (shouldReload) {
@@ -447,7 +420,7 @@ checkVersion()
447
420
 
448
421
  建议:
449
422
 
450
- - 版本比较只判断 `payload.time` 和 `import.meta.env.__VERSION__` 是否一致
423
+ - 版本比较只判断 `payload.time` 和当前页面里的 `window.__VERSION__.time` 是否一致
451
424
  - 提示方式由业务项目自己决定,可以是弹窗、消息条、通知或静默刷新
452
425
  - 轮询间隔不要过短,通常用 1 分钟或更长更合适
453
426
  - 请求失败后继续下一轮检查,避免一次失败后整个流程中断
package/dist/index.cjs CHANGED
@@ -116,6 +116,27 @@ function toRequestPath(base, fileName) {
116
116
  const baseWithoutTrailingSlash = withLeadingSlash.endsWith("/") ? withLeadingSlash.slice(0, -1) : withLeadingSlash;
117
117
  return `${baseWithoutTrailingSlash}/${fileName}`.replace(/\/{2,}/g, "/");
118
118
  }
119
+ function resolveVersion(options, context) {
120
+ if (typeof options.version === "function") {
121
+ return options.version(context);
122
+ }
123
+ if (options.version !== void 0) {
124
+ return options.version;
125
+ }
126
+ return createDefaultVersion(context.command);
127
+ }
128
+ function createPayload(options, context, data, packageInfo = {}) {
129
+ let nextPayload = {
130
+ ...data,
131
+ ...packageInfo,
132
+ time: resolveVersion(options, context),
133
+ env: context.mode
134
+ };
135
+ if (options.payload) {
136
+ nextPayload = options.payload(nextPayload);
137
+ }
138
+ return nextPayload;
139
+ }
119
140
  function buildVersionPlugin(options = {}) {
120
141
  const fileName = normalizeFileName(options.filename ?? DEFAULT_FILENAME);
121
142
  const globalName = normalizeGlobalName(
@@ -123,52 +144,16 @@ function buildVersionPlugin(options = {}) {
123
144
  );
124
145
  const injectToHtml = options.injectToHtml ?? true;
125
146
  const data = normalizeData(options.data);
126
- let packageInfo = {};
127
147
  let payload;
128
148
  return {
129
149
  name: PLUGIN_NAME,
130
- config(config, { command }) {
131
- const context = {
132
- command,
133
- mode: config.mode || "development"
134
- };
135
- let version;
136
- if (typeof options.version === "function") {
137
- version = options.version(context);
138
- } else if (options.version !== void 0) {
139
- version = options.version;
140
- } else {
141
- version = createDefaultVersion(context.command);
142
- }
143
- return {
144
- define: {
145
- [`import.meta.env.${globalName}`]: JSON.stringify(version)
146
- }
147
- };
148
- },
149
150
  configResolved(config) {
150
- packageInfo = readPackageInfo(config.root);
151
151
  const context = {
152
152
  command: config.command,
153
153
  mode: config.mode
154
154
  };
155
- let version;
156
- if (typeof options.version === "function") {
157
- version = options.version(context);
158
- } else if (options.version !== void 0) {
159
- version = options.version;
160
- } else {
161
- version = createDefaultVersion(context.command);
162
- }
163
- payload = {
164
- ...data,
165
- ...packageInfo,
166
- time: version,
167
- env: config.mode
168
- };
169
- if (options.payload) {
170
- payload = options.payload(payload);
171
- }
155
+ const packageInfo = readPackageInfo(config.root);
156
+ payload = createPayload(options, context, data, packageInfo);
172
157
  },
173
158
  configureServer(server) {
174
159
  const requestPath = toRequestPath(server.config.base, fileName);
package/dist/index.mjs CHANGED
@@ -91,6 +91,27 @@ function toRequestPath(base, fileName) {
91
91
  const baseWithoutTrailingSlash = withLeadingSlash.endsWith("/") ? withLeadingSlash.slice(0, -1) : withLeadingSlash;
92
92
  return `${baseWithoutTrailingSlash}/${fileName}`.replace(/\/{2,}/g, "/");
93
93
  }
94
+ function resolveVersion(options, context) {
95
+ if (typeof options.version === "function") {
96
+ return options.version(context);
97
+ }
98
+ if (options.version !== void 0) {
99
+ return options.version;
100
+ }
101
+ return createDefaultVersion(context.command);
102
+ }
103
+ function createPayload(options, context, data, packageInfo = {}) {
104
+ let nextPayload = {
105
+ ...data,
106
+ ...packageInfo,
107
+ time: resolveVersion(options, context),
108
+ env: context.mode
109
+ };
110
+ if (options.payload) {
111
+ nextPayload = options.payload(nextPayload);
112
+ }
113
+ return nextPayload;
114
+ }
94
115
  function buildVersionPlugin(options = {}) {
95
116
  const fileName = normalizeFileName(options.filename ?? DEFAULT_FILENAME);
96
117
  const globalName = normalizeGlobalName(
@@ -98,52 +119,16 @@ function buildVersionPlugin(options = {}) {
98
119
  );
99
120
  const injectToHtml = options.injectToHtml ?? true;
100
121
  const data = normalizeData(options.data);
101
- let packageInfo = {};
102
122
  let payload;
103
123
  return {
104
124
  name: PLUGIN_NAME,
105
- config(config, { command }) {
106
- const context = {
107
- command,
108
- mode: config.mode || "development"
109
- };
110
- let version;
111
- if (typeof options.version === "function") {
112
- version = options.version(context);
113
- } else if (options.version !== void 0) {
114
- version = options.version;
115
- } else {
116
- version = createDefaultVersion(context.command);
117
- }
118
- return {
119
- define: {
120
- [`import.meta.env.${globalName}`]: JSON.stringify(version)
121
- }
122
- };
123
- },
124
125
  configResolved(config) {
125
- packageInfo = readPackageInfo(config.root);
126
126
  const context = {
127
127
  command: config.command,
128
128
  mode: config.mode
129
129
  };
130
- let version;
131
- if (typeof options.version === "function") {
132
- version = options.version(context);
133
- } else if (options.version !== void 0) {
134
- version = options.version;
135
- } else {
136
- version = createDefaultVersion(context.command);
137
- }
138
- payload = {
139
- ...data,
140
- ...packageInfo,
141
- time: version,
142
- env: config.mode
143
- };
144
- if (options.payload) {
145
- payload = options.payload(payload);
146
- }
130
+ const packageInfo = readPackageInfo(config.root);
131
+ payload = createPayload(options, context, data, packageInfo);
147
132
  },
148
133
  configureServer(server) {
149
134
  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.6",
3
+ "version": "0.1.7",
4
4
  "description": "A Vite plugin that emits version.json and injects version metadata into HTML.",
5
5
  "license": "MIT",
6
6
  "type": "module",