vite-plugin-refresh-guard 0.1.4 → 0.2.1
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 +19 -0
- package/README.md +30 -10
- package/dist/index.d.ts +10 -0
- package/dist/index.js +31 -1
- package/package.json +2 -2
package/CHANGELOG.md
CHANGED
|
@@ -1,5 +1,24 @@
|
|
|
1
1
|
# 更新日志
|
|
2
2
|
|
|
3
|
+
## v0.2.1 (2026-10-08)
|
|
4
|
+
|
|
5
|
+
**补上从 0.2.0 起就欠的两件事**(两件都不改运行期行为):
|
|
6
|
+
|
|
7
|
+
- **README 重写**(10281B → 精简版):0.2.0 发布时 README 已经是精简后的版本,但 CHANGELOG
|
|
8
|
+
没记,属于用户可见变化却无记录 —— 补上。README 随包发布,装 0.2.0 的用户拿到的就是这一版。
|
|
9
|
+
- **补 `.github/workflows/publish.yml`**:0.2.0 当初是手工 `npm publish` 发的,仓库既没有
|
|
10
|
+
tag 也没有发布工作流,于是 `prepublish-gate` / `readme-gate` **从未在 CI 上被触发过** ——
|
|
11
|
+
门禁写了等于白写。本次改成 tag `v*` 触发(npm Trusted Publishing / OIDC)。
|
|
12
|
+
|
|
13
|
+
|
|
14
|
+
## v0.2.0 (2026-08-13)
|
|
15
|
+
|
|
16
|
+
**dev 下改 CHANGELOG.md 即时热更新**:此前虚拟模块 `virtual:refresh-guard-changelog` 的内容在 dev server 启动时缓存,改了 CHANGELOG 必须重启 vite 才生效——在真实项目里踩到(改了日志以为没生效,重启才发现)。
|
|
17
|
+
|
|
18
|
+
- 插件 `load()` 里对 changelog 文件调 `this.addWatchFile()` 声明依赖(root 外的 changelog 也强制监听)
|
|
19
|
+
- 新增 `hotUpdate`(Vite 6-8 per-environment 钩子)与 `handleHotUpdate`(Vite 4-5 兼容)两个钩子:CHANGELOG 变化时 invalidate 虚拟模块并返回它,走 Vite 默认 js-update 推给 importers——页面不用重启,更新日志自动刷新
|
|
20
|
+
- `readChangelog` 导出并补单测(版本号解析 / 正文截取 / 日期行剔除 / `false` 关闭 / 文件缺失兜底),此前该函数零覆盖
|
|
21
|
+
|
|
3
22
|
## v0.1.4 (2026-08-11)
|
|
4
23
|
|
|
5
24
|
修复自 v0.1.1(甚至更早)起就存在的发布产物残缺问题:`tsconfig.build.json`(构建脚本 `tsc -p tsconfig.build.json` 这一步依赖的配置文件)一直没有提交进 git,导致:
|
package/README.md
CHANGED
|
@@ -1,13 +1,33 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
1
|
+
<h1 align="center">🔄 vite-plugin-refresh-guard</h1>
|
|
2
|
+
|
|
3
|
+
<p align="center">
|
|
4
|
+
<strong>部署了新版本,接下来怎么办 —— 把这件事变成一行配置。</strong><br>
|
|
5
|
+
静默刷新 / 提示后刷新 / 弹窗阻断,三选一写进 config;<br>
|
|
6
|
+
核心逻辑与框架无关,Vue 与 React 各一层薄适配。
|
|
7
|
+
</p>
|
|
8
|
+
|
|
9
|
+
<p align="center">
|
|
10
|
+
<a href="https://www.npmjs.com/package/vite-plugin-refresh-guard"><img src="https://img.shields.io/npm/v/vite-plugin-refresh-guard?style=for-the-badge&color=4d6bfe&logo=npm&label=npm" alt="npm" /></a>
|
|
11
|
+
<a href="https://www.npmjs.com/package/vite-plugin-refresh-guard"><img src="https://img.shields.io/npm/dm/vite-plugin-refresh-guard?style=for-the-badge&color=6d7f9c&label=downloads" alt="downloads" /></a>
|
|
12
|
+
<a href="https://github.com/webkubor/vite-plugin-refresh-guard/actions/workflows/ci.yml"><img src="https://img.shields.io/github/actions/workflow/status/webkubor/vite-plugin-refresh-guard/ci.yml?style=for-the-badge&label=CI" alt="CI" /></a>
|
|
13
|
+
<img src="https://img.shields.io/badge/vite-%3E%3D4%20%3C9-646CFF?style=for-the-badge&logo=vite&logoColor=white" alt="vite" />
|
|
14
|
+
<img src="https://img.shields.io/badge/vue-%3E%3D3-4FC08D?style=for-the-badge&logo=vue.js&logoColor=white" alt="vue" />
|
|
15
|
+
<img src="https://img.shields.io/badge/react-%3E%3D18-61DAFB?style=for-the-badge&logo=react&logoColor=white" alt="react" />
|
|
16
|
+
<img src="https://img.shields.io/badge/license-MIT-777?style=for-the-badge" alt="MIT" />
|
|
17
|
+
</p>
|
|
18
|
+
|
|
19
|
+
<p align="center">
|
|
20
|
+
<a href="./README.en.md">English</a> | 中文
|
|
21
|
+
</p>
|
|
22
|
+
|
|
23
|
+
## Why This
|
|
24
|
+
|
|
25
|
+
| | 本插件 | 各项目自己写胶水 | 完全不管 |
|
|
26
|
+
|---|---|---|---|
|
|
27
|
+
| 策略是否显式 | ✅ 写进 config,三选一 | ❌ 散落各处,改一次跑偏一次 | ❌ 没有这回事 |
|
|
28
|
+
| 框架覆盖 | ✅ 核心无关 + Vue / React 适配 | ⚠️ 每个项目复制一遍 | — |
|
|
29
|
+
| 新版本探测 | ✅ 构建产物指纹比对 | ⚠️ 各写各的,边界情况没人管 | ❌ 无 |
|
|
30
|
+
| 用户拿到旧版本时 | ✅ 提示 / 弹窗文案可定制 | ⚠️ 看实现 | ❌ 静默跑旧代码 |
|
|
11
31
|
|
|
12
32
|
给 Vite 项目做"部署了新版本,接下来怎么办"的可配置方案。
|
|
13
33
|
|
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
|
|
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
|
|
3
|
+
"version": "0.2.1",
|
|
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",
|
|
@@ -53,7 +53,7 @@
|
|
|
53
53
|
"typecheck": "tsc --noEmit",
|
|
54
54
|
"test": "vitest run",
|
|
55
55
|
"compat": "npm run typecheck && npm test && npm run build",
|
|
56
|
-
"prepublishOnly": "npm run build"
|
|
56
|
+
"prepublishOnly": "npm run build && node scripts/prepublish-gate.mjs && node scripts/readme-gate.mjs"
|
|
57
57
|
},
|
|
58
58
|
"peerDependencies": {
|
|
59
59
|
"react": ">=18",
|