vite-plugin-refresh-guard 0.2.1 → 0.3.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.
@@ -0,0 +1,356 @@
1
+ <script setup lang="ts">
2
+ import { computed, ref, watch } from 'vue'
3
+ import { extractBulletPoints } from '../core/changelog'
4
+ import type { UpdateCardProps } from '../core/types'
5
+
6
+ const props = withDefaults(defineProps<UpdateCardProps>(), {
7
+ visible: true,
8
+ version: null,
9
+ date: null,
10
+ content: null,
11
+ bulletPoints: undefined,
12
+ maxBullets: 4,
13
+ title: '✨ 新版本上线',
14
+ viewMoreUrl: '',
15
+ viewMoreText: '查看完整更新日志',
16
+ refreshText: '立即刷新',
17
+ closable: true,
18
+ layout: 'floating',
19
+ dark: null,
20
+ })
21
+
22
+ const emit = defineEmits<{
23
+ refresh: []
24
+ viewMore: []
25
+ close: []
26
+ }>()
27
+
28
+ const dismissed = ref(false)
29
+
30
+ // 当版本发生变化时,重置关闭状态
31
+ watch(() => props.version, () => {
32
+ dismissed.value = false
33
+ })
34
+
35
+ const isShown = computed(() => props.visible && !dismissed.value)
36
+
37
+ const bullets = computed(() => {
38
+ if (props.bulletPoints && props.bulletPoints.length > 0) {
39
+ return props.bulletPoints
40
+ }
41
+ if (props.content) {
42
+ return extractBulletPoints(props.content)
43
+ }
44
+ return []
45
+ })
46
+
47
+ const displayedBullets = computed(() => bullets.value.slice(0, props.maxBullets))
48
+ const remainingCount = computed(() => Math.max(0, bullets.value.length - props.maxBullets))
49
+
50
+ function handleClose() {
51
+ dismissed.value = true
52
+ emit('close')
53
+ }
54
+
55
+ function handleViewMore(e: Event) {
56
+ emit('viewMore')
57
+ if (!props.viewMoreUrl) {
58
+ e.preventDefault()
59
+ }
60
+ }
61
+ </script>
62
+
63
+ <template>
64
+ <component :is="layout === 'floating' ? 'Teleport' : 'div'" :to="layout === 'floating' ? 'body' : undefined">
65
+ <div
66
+ v-if="isShown"
67
+ class="rg-card-wrapper"
68
+ :class="[
69
+ layout === 'floating' ? 'rg-card-floating' : 'rg-card-inline',
70
+ { 'rg-dark': dark === true, 'rg-light': dark === false }
71
+ ]"
72
+ role="region"
73
+ aria-label="新版本更新通知"
74
+ >
75
+ <div class="rg-card">
76
+ <!-- 头部区域 -->
77
+ <div class="rg-card-header">
78
+ <div class="rg-card-title-group">
79
+ <span class="rg-card-title">{{ title }}</span>
80
+ <span v-if="version" class="rg-card-badge">{{ version }}</span>
81
+ <span v-if="date" class="rg-card-date">{{ date }}</span>
82
+ </div>
83
+ <button
84
+ v-if="closable"
85
+ type="button"
86
+ class="rg-card-close"
87
+ aria-label="关闭更新提示"
88
+ @click="handleClose"
89
+ >
90
+ ✕
91
+ </button>
92
+ </div>
93
+
94
+ <!-- 正文要点列表 -->
95
+ <div class="rg-card-body">
96
+ <ul v-if="displayedBullets.length > 0" class="rg-bullet-list">
97
+ <li v-for="(b, i) in displayedBullets" :key="i" class="rg-bullet-item">
98
+ <span class="rg-bullet-dot" aria-hidden="true">•</span>
99
+ <span class="rg-bullet-text">{{ b }}</span>
100
+ </li>
101
+ </ul>
102
+ <p v-else-if="content" class="rg-card-fallback-text">{{ content }}</p>
103
+ <p v-else class="rg-card-fallback-text">系统已部署新版本,刷新页面即可使用最新功能与优化。</p>
104
+
105
+ <div v-if="remainingCount > 0" class="rg-remaining-note">
106
+ <span>还有 {{ remainingCount }} 项更新改进</span>
107
+ </div>
108
+ </div>
109
+
110
+ <!-- 底部操作栏 -->
111
+ <div class="rg-card-footer">
112
+ <a
113
+ v-if="viewMoreUrl || $attrs.onViewMore"
114
+ :href="viewMoreUrl || 'javascript:void(0)'"
115
+ class="rg-card-link"
116
+ @click="handleViewMore"
117
+ >
118
+ {{ viewMoreText }} →
119
+ </a>
120
+ <span v-else class="rg-card-spacer" />
121
+
122
+ <button
123
+ type="button"
124
+ class="rg-card-refresh-btn"
125
+ @click="emit('refresh')"
126
+ >
127
+ {{ refreshText }}
128
+ </button>
129
+ </div>
130
+ </div>
131
+ </div>
132
+ </component>
133
+ </template>
134
+
135
+ <style scoped>
136
+ .rg-card-wrapper {
137
+ box-sizing: border-box;
138
+ font-family: -apple-system, BlinkMacSystemFont, 'Segoe UI', Roboto, Helvetica, Arial, sans-serif;
139
+ }
140
+
141
+ .rg-card-floating {
142
+ position: fixed;
143
+ right: 20px;
144
+ bottom: 20px;
145
+ z-index: 9998;
146
+ width: 360px;
147
+ max-width: calc(100vw - 32px);
148
+ animation: rg-slide-up 0.28s cubic-bezier(0.16, 1, 0.3, 1);
149
+ }
150
+
151
+ .rg-card-inline {
152
+ position: relative;
153
+ width: 100%;
154
+ margin: 12px 0;
155
+ }
156
+
157
+ @keyframes rg-slide-up {
158
+ from {
159
+ opacity: 0;
160
+ transform: translateY(16px) scale(0.97);
161
+ }
162
+ to {
163
+ opacity: 1;
164
+ transform: translateY(0) scale(1);
165
+ }
166
+ }
167
+
168
+ .rg-card {
169
+ padding: 16px 18px;
170
+ border-radius: var(--rg-radius, 14px);
171
+ background: var(--rg-card-bg, #ffffff);
172
+ color: var(--rg-card-fg, #111827);
173
+ border: 1px solid var(--rg-card-border, #e5e7eb);
174
+ box-shadow: var(--rg-card-shadow, 0 10px 30px -4px rgba(0, 0, 0, 0.12), 0 4px 10px -2px rgba(0, 0, 0, 0.05));
175
+ backdrop-filter: blur(8px);
176
+ }
177
+
178
+ .rg-card-header {
179
+ display: flex;
180
+ align-items: center;
181
+ justify-content: space-between;
182
+ margin-bottom: 12px;
183
+ }
184
+
185
+ .rg-card-title-group {
186
+ display: flex;
187
+ align-items: center;
188
+ gap: 8px;
189
+ flex-wrap: wrap;
190
+ }
191
+
192
+ .rg-card-title {
193
+ font-size: 14.5px;
194
+ font-weight: 700;
195
+ letter-spacing: -0.01em;
196
+ color: inherit;
197
+ }
198
+
199
+ .rg-card-badge {
200
+ display: inline-flex;
201
+ align-items: center;
202
+ padding: 2px 7px;
203
+ border-radius: 9999px;
204
+ font-size: 11px;
205
+ font-weight: 600;
206
+ font-family: ui-monospace, SFMono-Regular, Menlo, Monaco, Consolas, monospace;
207
+ background: var(--rg-badge-bg, rgba(37, 99, 235, 0.1));
208
+ color: var(--rg-badge-fg, #2563eb);
209
+ }
210
+
211
+ .rg-card-date {
212
+ font-size: 11.5px;
213
+ color: var(--rg-muted-fg, #6b7280);
214
+ }
215
+
216
+ .rg-card-close {
217
+ display: inline-flex;
218
+ align-items: center;
219
+ justify-content: center;
220
+ width: 22px;
221
+ height: 22px;
222
+ padding: 0;
223
+ border: none;
224
+ border-radius: 6px;
225
+ background: transparent;
226
+ color: var(--rg-muted-fg, #9ca3af);
227
+ font-size: 13px;
228
+ cursor: pointer;
229
+ transition: all 0.15s ease;
230
+ }
231
+
232
+ .rg-card-close:hover {
233
+ background: var(--rg-hover-bg, rgba(0, 0, 0, 0.06));
234
+ color: var(--rg-card-fg, #111827);
235
+ }
236
+
237
+ .rg-card-body {
238
+ margin-bottom: 14px;
239
+ }
240
+
241
+ .rg-bullet-list {
242
+ margin: 0;
243
+ padding: 0;
244
+ list-style: none;
245
+ display: flex;
246
+ flex-direction: column;
247
+ gap: 6px;
248
+ }
249
+
250
+ .rg-bullet-item {
251
+ display: flex;
252
+ align-items: flex-start;
253
+ gap: 6px;
254
+ font-size: 13px;
255
+ line-height: 1.5;
256
+ color: var(--rg-body-fg, #374151);
257
+ }
258
+
259
+ .rg-bullet-dot {
260
+ flex-shrink: 0;
261
+ font-size: 14px;
262
+ line-height: 1.4;
263
+ color: var(--rg-accent, #2563eb);
264
+ }
265
+
266
+ .rg-bullet-text {
267
+ flex: 1;
268
+ word-break: break-word;
269
+ }
270
+
271
+ .rg-card-fallback-text {
272
+ margin: 0;
273
+ font-size: 13px;
274
+ line-height: 1.55;
275
+ color: var(--rg-body-fg, #4b5563);
276
+ }
277
+
278
+ .rg-remaining-note {
279
+ margin-top: 6px;
280
+ padding-left: 12px;
281
+ font-size: 11.5px;
282
+ color: var(--rg-muted-fg, #6b7280);
283
+ }
284
+
285
+ .rg-card-footer {
286
+ display: flex;
287
+ align-items: center;
288
+ justify-content: space-between;
289
+ gap: 12px;
290
+ padding-top: 10px;
291
+ border-top: 1px solid var(--rg-card-border, #f3f4f6);
292
+ }
293
+
294
+ .rg-card-spacer {
295
+ flex: 1;
296
+ }
297
+
298
+ .rg-card-link {
299
+ font-size: 12.5px;
300
+ font-weight: 500;
301
+ color: var(--rg-accent, #2563eb);
302
+ text-decoration: none;
303
+ transition: opacity 0.15s ease;
304
+ }
305
+
306
+ .rg-card-link:hover {
307
+ opacity: 0.82;
308
+ text-decoration: underline;
309
+ }
310
+
311
+ .rg-card-refresh-btn {
312
+ padding: 6px 14px;
313
+ border: none;
314
+ border-radius: var(--rg-btn-radius, 8px);
315
+ background: var(--rg-accent, #2563eb);
316
+ color: var(--rg-accent-fg, #ffffff);
317
+ font-size: 12.5px;
318
+ font-weight: 600;
319
+ cursor: pointer;
320
+ transition: all 0.15s ease;
321
+ white-space: nowrap;
322
+ }
323
+
324
+ .rg-card-refresh-btn:hover {
325
+ filter: brightness(0.92);
326
+ }
327
+
328
+ .rg-card-refresh-btn:active {
329
+ transform: scale(0.98);
330
+ }
331
+
332
+ /* 深色模式适配 */
333
+ .rg-card-wrapper.rg-dark .rg-card {
334
+ background: var(--rg-card-bg-dark, #1f2937);
335
+ color: var(--rg-card-fg-dark, #f9fafb);
336
+ border-color: var(--rg-card-border-dark, #374151);
337
+ box-shadow: 0 10px 30px -4px rgba(0, 0, 0, 0.4);
338
+ }
339
+ .rg-card-wrapper.rg-dark .rg-bullet-item { color: var(--rg-body-fg-dark, #d1d5db); }
340
+ .rg-card-wrapper.rg-dark .rg-card-badge { background: rgba(59, 130, 246, 0.2); color: #60a5fa; }
341
+ .rg-card-wrapper.rg-dark .rg-card-footer { border-color: #374151; }
342
+ .rg-card-wrapper.rg-dark .rg-card-close:hover { background: rgba(255, 255, 255, 0.1); color: #fff; }
343
+
344
+ @media (prefers-color-scheme: dark) {
345
+ .rg-card-wrapper:not(.rg-light) .rg-card {
346
+ background: var(--rg-card-bg-dark, #1f2937);
347
+ color: var(--rg-card-fg-dark, #f9fafb);
348
+ border-color: var(--rg-card-border-dark, #374151);
349
+ box-shadow: 0 10px 30px -4px rgba(0, 0, 0, 0.4);
350
+ }
351
+ .rg-card-wrapper:not(.rg-light) .rg-bullet-item { color: var(--rg-body-fg-dark, #d1d5db); }
352
+ .rg-card-wrapper:not(.rg-light) .rg-card-badge { background: rgba(59, 130, 246, 0.2); color: #60a5fa; }
353
+ .rg-card-wrapper:not(.rg-light) .rg-card-footer { border-color: #374151; }
354
+ .rg-card-wrapper:not(.rg-light) .rg-card-close:hover { background: rgba(255, 255, 255, 0.1); color: #fff; }
355
+ }
356
+ </style>
@@ -1,5 +1,7 @@
1
- import { UpdateChecker } from '../core/checker';
2
- import type { VersionCheckerOptions, VersionInfo } from '../core/types';
1
+ import { UpdateChecker, fetchChangelog, parseChangelog, extractBulletPoints } from '../core/checker';
2
+ import type { VersionCheckerOptions, VersionInfo, ParsedChangelog, ChangelogVersionEntry, UpdateCardProps } from '../core/types';
3
+ export { fetchChangelog, parseChangelog, extractBulletPoints };
4
+ export type { VersionCheckerOptions, VersionInfo, ParsedChangelog, ChangelogVersionEntry, UpdateCardProps, };
3
5
  /**
4
6
  * Vue composable wrapping the framework-agnostic UpdateChecker. Mounts/unmounts the checker
5
7
  * with the component's lifecycle automatically.
@@ -9,9 +11,11 @@ import type { VersionCheckerOptions, VersionInfo } from '../core/types';
9
11
  export declare function useVersionCheck(currentVersion: string, options?: VersionCheckerOptions): {
10
12
  hasUpdate: import("vue").Ref<boolean, boolean>;
11
13
  latestInfo: import("vue").ShallowRef<VersionInfo | null, VersionInfo | null>;
14
+ changelog: import("vue").ShallowRef<ParsedChangelog | null, ParsedChangelog | null>;
12
15
  mode: import("../core/types").UpdateMode;
13
16
  applyUpdate: () => void;
14
17
  checkNow: () => Promise<boolean>;
18
+ fetchChangelog: (url?: string) => Promise<ParsedChangelog>;
15
19
  /** Feed a vite-plugin-pwa `onNeedRefresh` (or any other out-of-band) signal into the same mode logic. */
16
20
  notifyExternalUpdate: (info?: VersionInfo) => void;
17
21
  checker: UpdateChecker;
@@ -1,33 +1,48 @@
1
1
  import {
2
2
  UpdateChecker
3
- } from "../chunk-ULA5FWKW.js";
3
+ } from "../chunk-CGDGH2ML.js";
4
+ import {
5
+ extractBulletPoints,
6
+ fetchChangelog,
7
+ parseChangelog
8
+ } from "../chunk-JLHX4WLS.js";
4
9
 
5
10
  // src/vue/useVersionCheck.ts
6
11
  import { onMounted, onUnmounted, ref, shallowRef } from "vue";
7
12
  function useVersionCheck(currentVersion, options = {}) {
8
13
  const hasUpdate = ref(false);
9
14
  const latestInfo = shallowRef(null);
15
+ const changelog = shallowRef(null);
10
16
  const checker = new UpdateChecker(currentVersion, options);
11
17
  const off = checker.on("update-available", (info) => {
12
18
  hasUpdate.value = true;
13
19
  latestInfo.value = info;
14
20
  });
21
+ const offChangelog = checker.on("changelog-loaded", (parsed) => {
22
+ changelog.value = parsed;
23
+ });
15
24
  onMounted(() => checker.start());
16
25
  onUnmounted(() => {
17
26
  checker.stop();
18
27
  off();
28
+ offChangelog();
19
29
  });
20
30
  return {
21
31
  hasUpdate,
22
32
  latestInfo,
33
+ changelog,
23
34
  mode: options.mode ?? "toast-auto",
24
35
  applyUpdate: () => checker.applyUpdate(),
25
36
  checkNow: () => checker.checkNow(),
37
+ fetchChangelog: (url) => checker.fetchChangelog(url),
26
38
  /** Feed a vite-plugin-pwa `onNeedRefresh` (or any other out-of-band) signal into the same mode logic. */
27
39
  notifyExternalUpdate: (info) => checker.notifyExternalUpdate(info),
28
40
  checker
29
41
  };
30
42
  }
31
43
  export {
44
+ extractBulletPoints,
45
+ fetchChangelog,
46
+ parseChangelog,
32
47
  useVersionCheck
33
48
  };
package/package.json CHANGED
@@ -1,7 +1,8 @@
1
1
  {
2
2
  "name": "vite-plugin-refresh-guard",
3
- "version": "0.2.1",
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.",
3
+ "version": "0.3.0",
4
+ "$schema": "https://raw.githubusercontent.com/vitejs/vite-plugin-registry/refs/heads/main/data/schema/extended-package-json.schema.json",
5
+ "description": "Vite plugin for web update notification — detect a new version on every page load and prompt users to reload: silent auto reload, force refresh, toast prompt, or blocking modal. Framework-agnostic core with Vue and React adapters. Vite 应用版本更新检测与自动刷新方案。",
5
6
  "type": "module",
6
7
  "license": "MIT",
7
8
  "repository": {
@@ -11,14 +12,21 @@
11
12
  "keywords": [
12
13
  "vite",
13
14
  "vite-plugin",
15
+ "web-update-notification",
16
+ "update-notification",
17
+ "new-version",
14
18
  "version-check",
15
19
  "app-update",
20
+ "auto-reload",
21
+ "auto-refresh",
22
+ "force-refresh",
23
+ "refresh",
16
24
  "service-worker",
17
25
  "pwa",
18
- "refresh",
19
26
  "changelog",
20
27
  "vue",
21
- "react"
28
+ "react",
29
+ "更新提示"
22
30
  ],
23
31
  "exports": {
24
32
  ".": {
@@ -33,6 +41,7 @@
33
41
  "import": "./dist/vue/useVersionCheck.js"
34
42
  },
35
43
  "./vue/UpdatePrompt.vue": "./dist/vue/UpdatePrompt.vue",
44
+ "./vue/UpdateCard.vue": "./dist/vue/UpdateCard.vue",
36
45
  "./react": {
37
46
  "types": "./dist/react/useVersionCheck.d.ts",
38
47
  "import": "./dist/react/useVersionCheck.js"
@@ -40,6 +49,10 @@
40
49
  "./react/UpdatePrompt": {
41
50
  "types": "./dist/react/UpdatePrompt.d.ts",
42
51
  "import": "./dist/react/UpdatePrompt.js"
52
+ },
53
+ "./react/UpdateCard": {
54
+ "types": "./dist/react/UpdateCard.d.ts",
55
+ "import": "./dist/react/UpdateCard.js"
43
56
  }
44
57
  },
45
58
  "files": [
@@ -49,7 +62,7 @@
49
62
  "CHANGELOG.md"
50
63
  ],
51
64
  "scripts": {
52
- "build": "tsup src/index.ts src/vue/useVersionCheck.ts src/react/useVersionCheck.ts src/react/UpdatePrompt.tsx --format esm --clean && tsc -p tsconfig.build.json && node scripts/copy-assets.mjs",
65
+ "build": "tsup src/index.ts src/vue/useVersionCheck.ts src/react/useVersionCheck.ts src/react/UpdatePrompt.tsx src/react/UpdateCard.tsx --format esm --clean && tsc -p tsconfig.build.json && node scripts/copy-assets.mjs",
53
66
  "typecheck": "tsc --noEmit",
54
67
  "test": "vitest run",
55
68
  "compat": "npm run typecheck && npm test && npm run build",
@@ -72,6 +85,22 @@
72
85
  "optional": true
73
86
  }
74
87
  },
88
+ "compatiblePackages": {
89
+ "schemaVersion": 1,
90
+ "vite": {
91
+ "type": "compatible",
92
+ "versions": ">=4 <10",
93
+ "note": "Vite 6-8 走 per-environment hotUpdate,Vite 4-5 兜底 handleHotUpdate;两个钩子都实现了。"
94
+ },
95
+ "rollup": {
96
+ "type": "incompatible",
97
+ "reason": "Vite-only:版本号与项目根在 Vite 的 config / configResolved 钩子里解析,Rollup 不调用这两个钩子,虚拟模块 virtual:refresh-guard-changelog 的 load 会直接抛错。"
98
+ },
99
+ "rolldown": {
100
+ "type": "incompatible",
101
+ "reason": "Vite-only:本插件不是 Rolldown 插件,依赖 Vite 的配置钩子与 dev server 的 HMR 生命周期。"
102
+ }
103
+ },
75
104
  "devDependencies": {
76
105
  "@types/node": "^24.10.11",
77
106
  "@types/react": "^19.2.18",