vite-plugin-refresh-guard 0.1.4 → 0.2.0

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/CHANGELOG.md CHANGED
@@ -1,5 +1,13 @@
1
1
  # 更新日志
2
2
 
3
+ ## v0.2.0 (2026-08-13)
4
+
5
+ **dev 下改 CHANGELOG.md 即时热更新**:此前虚拟模块 `virtual:refresh-guard-changelog` 的内容在 dev server 启动时缓存,改了 CHANGELOG 必须重启 vite 才生效——在真实项目里踩到(改了日志以为没生效,重启才发现)。
6
+
7
+ - 插件 `load()` 里对 changelog 文件调 `this.addWatchFile()` 声明依赖(root 外的 changelog 也强制监听)
8
+ - 新增 `hotUpdate`(Vite 6-8 per-environment 钩子)与 `handleHotUpdate`(Vite 4-5 兼容)两个钩子:CHANGELOG 变化时 invalidate 虚拟模块并返回它,走 Vite 默认 js-update 推给 importers——页面不用重启,更新日志自动刷新
9
+ - `readChangelog` 导出并补单测(版本号解析 / 正文截取 / 日期行剔除 / `false` 关闭 / 文件缺失兜底),此前该函数零覆盖
10
+
3
11
  ## v0.1.4 (2026-08-11)
4
12
 
5
13
  修复自 v0.1.1(甚至更早)起就存在的发布产物残缺问题:`tsconfig.build.json`(构建脚本 `tsc -p tsconfig.build.json` 这一步依赖的配置文件)一直没有提交进 git,导致:
package/dist/index.d.ts CHANGED
@@ -12,6 +12,16 @@ export interface RefreshGuardPluginOptions {
12
12
  */
13
13
  changelog?: string | false;
14
14
  }
15
+ /**
16
+ * 读取 CHANGELOG 最新版本的版本号与正文(导出供测试复用;接入方不需要直接用)。
17
+ */
18
+ export declare function readChangelog(root: string, changelogOption: string | false | undefined): {
19
+ latestVersion: null;
20
+ latestContent: string;
21
+ } | {
22
+ latestVersion: string;
23
+ latestContent: string;
24
+ };
15
25
  /**
16
26
  * Vite plugin half of refresh-guard: bakes the current build's version into your bundle
17
27
  * (as `__REFRESH_GUARD_VERSION__`) and emits a `version.json` manifest that the runtime
package/dist/index.js CHANGED
@@ -12,7 +12,7 @@ function readChangelog(root, changelogOption) {
12
12
  const heading = md.match(/^##\s+(v?[\d.]+)/m);
13
13
  if (!heading) return { latestVersion: null, latestContent: "" };
14
14
  const sections = md.split(/^## /m);
15
- const body = sections.length > 1 ? sections[1].replace(/^v?[\d.]+\s*\(.*?\)\s*\n?/, "").trim() : "";
15
+ const body = sections.length > 1 ? sections[1].replace(/^v?[\d.]+\s*(?:\(.*?\))?\s*\n?/, "").trim() : "";
16
16
  return { latestVersion: heading[1], latestContent: body };
17
17
  }
18
18
  function refreshGuard(options = {}) {
@@ -20,6 +20,7 @@ function refreshGuard(options = {}) {
20
20
  let version = options.version;
21
21
  const buildId = String(Date.now());
22
22
  let commit = "";
23
+ let changelogPath = "";
23
24
  return {
24
25
  name: "vite-plugin-refresh-guard",
25
26
  // `config()` runs before `configResolved()` — the version has to be settled here, not there,
@@ -39,6 +40,7 @@ function refreshGuard(options = {}) {
39
40
  },
40
41
  configResolved(resolvedConfig) {
41
42
  config = resolvedConfig;
43
+ changelogPath = options.changelog === false ? "" : resolve(config.root, options.changelog || "CHANGELOG.md");
42
44
  if (options.includeCommit !== false) {
43
45
  try {
44
46
  commit = execSync("git rev-parse --short HEAD", { cwd: config.root, stdio: ["ignore", "pipe", "ignore"] }).toString().trim();
@@ -52,11 +54,38 @@ function refreshGuard(options = {}) {
52
54
  },
53
55
  load(id) {
54
56
  if (id !== RESOLVED_VIRTUAL_ID) return;
57
+ if (changelogPath) this.addWatchFile(changelogPath);
55
58
  const { latestVersion, latestContent } = readChangelog(config.root, options.changelog);
56
59
  return `export const latestVersion = ${JSON.stringify(latestVersion)};
57
60
  export const latestContent = ${JSON.stringify(latestContent)};
58
61
  `;
59
62
  },
63
+ /**
64
+ * dev 下 CHANGELOG.md 变化 → invalidate 虚拟模块并返回它,让 Vite 走默认 updateModules
65
+ * 给 importers 推 js-update,页面不用重启就能拿到最新更新日志。
66
+ *
67
+ * 兼容 Vite 4-8:Vite 6+ 用 per-environment `hotUpdate`(environment 在 this.environment,
68
+ * 触发文件在 options.file);Vite 4-5 用旧版 `handleHotUpdate(ctx)`(ctx.file + ctx.server.moduleGraph)。
69
+ * 两个钩子都实现,各自能跑到的版本走各自的,跑不到的会被 Vite 忽略。
70
+ */
71
+ hotUpdate(options2) {
72
+ if (!changelogPath) return;
73
+ const mod = this.environment.moduleGraph?.getModuleById(RESOLVED_VIRTUAL_ID);
74
+ if (options2.file === changelogPath && mod) {
75
+ this.environment.moduleGraph.invalidateModule(mod);
76
+ return [mod];
77
+ }
78
+ },
79
+ handleHotUpdate(ctx) {
80
+ if (!changelogPath) return;
81
+ if (ctx.file !== changelogPath) return;
82
+ const graph = ctx.server.moduleGraph;
83
+ const mod = graph.getModuleById(RESOLVED_VIRTUAL_ID);
84
+ if (mod) {
85
+ graph.invalidateModule(mod);
86
+ return [mod];
87
+ }
88
+ },
60
89
  generateBundle() {
61
90
  const manifest = {
62
91
  version: version ?? "0.0.0",
@@ -75,5 +104,6 @@ export const latestContent = ${JSON.stringify(latestContent)};
75
104
  var index_default = refreshGuard;
76
105
  export {
77
106
  index_default as default,
107
+ readChangelog,
78
108
  refreshGuard
79
109
  };
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "vite-plugin-refresh-guard",
3
- "version": "0.1.4",
3
+ "version": "0.2.0",
4
4
  "description": "Vite 应用的可配置版本更新检测+刷新方案:静默自动刷新 / 提示后自动刷新 / 弹窗阻断,按产品需要选档位,核心逻辑框架无关,附 Vue/React 适配层。Configurable new-version detection + refresh for Vite apps — silent / toast-auto / modal-blocking, framework-agnostic core with Vue and React adapters.",
5
5
  "type": "module",
6
6
  "license": "MIT",