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.
package/dist/index.js CHANGED
@@ -1,3 +1,9 @@
1
+ import {
2
+ extractBulletPoints,
3
+ fetchChangelog,
4
+ parseChangelog
5
+ } from "./chunk-JLHX4WLS.js";
6
+
1
7
  // src/index.ts
2
8
  import { execSync } from "child_process";
3
9
  import { existsSync, readFileSync } from "fs";
@@ -5,15 +11,14 @@ import { resolve } from "path";
5
11
  var VIRTUAL_ID = "virtual:refresh-guard-changelog";
6
12
  var RESOLVED_VIRTUAL_ID = "\0" + VIRTUAL_ID;
7
13
  function readChangelog(root, changelogOption) {
8
- if (changelogOption === false) return { latestVersion: null, latestContent: "" };
9
- const path = resolve(root, changelogOption || "CHANGELOG.md");
10
- if (!existsSync(path)) return { latestVersion: null, latestContent: "" };
14
+ if (changelogOption === false) {
15
+ return { latestVersion: null, latestContent: "", allVersions: [] };
16
+ }
17
+ const relPath = typeof changelogOption === "string" ? changelogOption : typeof changelogOption === "object" && changelogOption.file ? changelogOption.file : "CHANGELOG.md";
18
+ const path = resolve(root, relPath);
19
+ if (!existsSync(path)) return { latestVersion: null, latestContent: "", allVersions: [] };
11
20
  const md = readFileSync(path, "utf-8");
12
- const heading = md.match(/^##\s+(v?[\d.]+)/m);
13
- if (!heading) return { latestVersion: null, latestContent: "" };
14
- const sections = md.split(/^## /m);
15
- const body = sections.length > 1 ? sections[1].replace(/^v?[\d.]+\s*(?:\(.*?\))?\s*\n?/, "").trim() : "";
16
- return { latestVersion: heading[1], latestContent: body };
21
+ return parseChangelog(md);
17
22
  }
18
23
  function refreshGuard(options = {}) {
19
24
  let config;
@@ -40,7 +45,8 @@ function refreshGuard(options = {}) {
40
45
  },
41
46
  configResolved(resolvedConfig) {
42
47
  config = resolvedConfig;
43
- changelogPath = options.changelog === false ? "" : resolve(config.root, options.changelog || "CHANGELOG.md");
48
+ const relFile = typeof options.changelog === "string" ? options.changelog : typeof options.changelog === "object" && options.changelog?.file ? options.changelog.file : "CHANGELOG.md";
49
+ changelogPath = options.changelog === false ? "" : resolve(config.root, relFile);
44
50
  if (options.includeCommit !== false) {
45
51
  try {
46
52
  commit = execSync("git rev-parse --short HEAD", { cwd: config.root, stdio: ["ignore", "pipe", "ignore"] }).toString().trim();
@@ -55,9 +61,12 @@ function refreshGuard(options = {}) {
55
61
  load(id) {
56
62
  if (id !== RESOLVED_VIRTUAL_ID) return;
57
63
  if (changelogPath) this.addWatchFile(changelogPath);
58
- const { latestVersion, latestContent } = readChangelog(config.root, options.changelog);
64
+ const { latestVersion, latestContent, allVersions } = readChangelog(config.root, options.changelog);
65
+ const changelogUrl = typeof options.changelog === "object" && options.changelog !== null ? options.changelog.url || "/" + (options.changelog.outFile || "CHANGELOG.md") : void 0;
59
66
  return `export const latestVersion = ${JSON.stringify(latestVersion)};
60
67
  export const latestContent = ${JSON.stringify(latestContent)};
68
+ export const allVersions = ${JSON.stringify(allVersions)};
69
+ export const changelogUrl = ${JSON.stringify(changelogUrl)};
61
70
  `;
62
71
  },
63
72
  /**
@@ -98,12 +107,22 @@ export const latestContent = ${JSON.stringify(latestContent)};
98
107
  fileName: options.outFile || "version.json",
99
108
  source: JSON.stringify(manifest, null, 2)
100
109
  });
110
+ if (typeof options.changelog === "object" && options.changelog !== null && options.changelog.mode === "fetch" && changelogPath && existsSync(changelogPath)) {
111
+ this.emitFile({
112
+ type: "asset",
113
+ fileName: options.changelog.outFile || "CHANGELOG.md",
114
+ source: readFileSync(changelogPath, "utf-8")
115
+ });
116
+ }
101
117
  }
102
118
  };
103
119
  }
104
120
  var index_default = refreshGuard;
105
121
  export {
106
122
  index_default as default,
123
+ extractBulletPoints,
124
+ fetchChangelog,
125
+ parseChangelog,
107
126
  readChangelog,
108
127
  refreshGuard
109
128
  };
@@ -0,0 +1,8 @@
1
+ import React from 'react';
2
+ import type { UpdateCardProps } from '../core/types';
3
+ export interface ReactUpdateCardProps extends UpdateCardProps {
4
+ onRefresh?: () => void;
5
+ onViewMore?: () => void;
6
+ onClose?: () => void;
7
+ }
8
+ export declare function UpdateCard({ visible, version, date, content, bulletPoints, maxBullets, title, viewMoreUrl, viewMoreText, refreshText, closable, layout, dark, onRefresh, onViewMore, onClose, }: ReactUpdateCardProps): React.JSX.Element | null;
@@ -0,0 +1,275 @@
1
+ import {
2
+ extractBulletPoints
3
+ } from "../chunk-JLHX4WLS.js";
4
+
5
+ // src/react/UpdateCard.tsx
6
+ import { useState, useEffect } from "react";
7
+ import { createPortal } from "react-dom";
8
+ import { jsx, jsxs } from "react/jsx-runtime";
9
+ function UpdateCard({
10
+ visible = true,
11
+ version = null,
12
+ date = null,
13
+ content = null,
14
+ bulletPoints,
15
+ maxBullets = 4,
16
+ title = "\u2728 \u65B0\u7248\u672C\u4E0A\u7EBF",
17
+ viewMoreUrl = "",
18
+ viewMoreText = "\u67E5\u770B\u5B8C\u6574\u66F4\u65B0\u65E5\u5FD7",
19
+ refreshText = "\u7ACB\u5373\u5237\u65B0",
20
+ closable = true,
21
+ layout = "floating",
22
+ dark = false,
23
+ onRefresh,
24
+ onViewMore,
25
+ onClose
26
+ }) {
27
+ const [dismissed, setDismissed] = useState(false);
28
+ useEffect(() => {
29
+ setDismissed(false);
30
+ }, [version]);
31
+ if (!visible || dismissed) return null;
32
+ const bullets = bulletPoints && bulletPoints.length > 0 ? bulletPoints : content ? extractBulletPoints(content) : [];
33
+ const displayedBullets = bullets.slice(0, maxBullets);
34
+ const remainingCount = Math.max(0, bullets.length - maxBullets);
35
+ const handleClose = () => {
36
+ setDismissed(true);
37
+ onClose?.();
38
+ };
39
+ const handleViewMore = (e) => {
40
+ onViewMore?.();
41
+ if (!viewMoreUrl) {
42
+ e.preventDefault();
43
+ }
44
+ };
45
+ const cardStyle = {
46
+ ...styles.card,
47
+ ...dark ? styles.cardDark : {}
48
+ };
49
+ const cardContent = /* @__PURE__ */ jsx(
50
+ "div",
51
+ {
52
+ style: layout === "floating" ? styles.wrapperFloating : styles.wrapperInline,
53
+ role: "region",
54
+ "aria-label": "\u65B0\u7248\u672C\u66F4\u65B0\u901A\u77E5",
55
+ children: /* @__PURE__ */ jsxs("div", { style: cardStyle, children: [
56
+ /* @__PURE__ */ jsxs("div", { style: styles.header, children: [
57
+ /* @__PURE__ */ jsxs("div", { style: styles.titleGroup, children: [
58
+ /* @__PURE__ */ jsx("span", { style: styles.title, children: title }),
59
+ version && /* @__PURE__ */ jsx("span", { style: dark ? { ...styles.badge, ...styles.badgeDark } : styles.badge, children: version }),
60
+ date && /* @__PURE__ */ jsx("span", { style: styles.date, children: date })
61
+ ] }),
62
+ closable && /* @__PURE__ */ jsx(
63
+ "button",
64
+ {
65
+ type: "button",
66
+ style: styles.closeBtn,
67
+ "aria-label": "\u5173\u95ED\u66F4\u65B0\u63D0\u793A",
68
+ onClick: handleClose,
69
+ children: "\u2715"
70
+ }
71
+ )
72
+ ] }),
73
+ /* @__PURE__ */ jsxs("div", { style: styles.body, children: [
74
+ displayedBullets.length > 0 ? /* @__PURE__ */ jsx("ul", { style: styles.list, children: displayedBullets.map((b, i) => /* @__PURE__ */ jsxs("li", { style: styles.listItem, children: [
75
+ /* @__PURE__ */ jsx("span", { style: styles.bulletDot, "aria-hidden": "true", children: "\u2022" }),
76
+ /* @__PURE__ */ jsx("span", { style: dark ? { ...styles.bulletText, ...styles.bulletTextDark } : styles.bulletText, children: b })
77
+ ] }, i)) }) : content ? /* @__PURE__ */ jsx("p", { style: dark ? { ...styles.fallbackText, ...styles.fallbackTextDark } : styles.fallbackText, children: content }) : /* @__PURE__ */ jsx("p", { style: dark ? { ...styles.fallbackText, ...styles.fallbackTextDark } : styles.fallbackText, children: "\u7CFB\u7EDF\u5DF2\u90E8\u7F72\u65B0\u7248\u672C\uFF0C\u5237\u65B0\u9875\u9762\u5373\u53EF\u4F7F\u7528\u6700\u65B0\u529F\u80FD\u4E0E\u4F18\u5316\u3002" }),
78
+ remainingCount > 0 && /* @__PURE__ */ jsxs("div", { style: styles.remaining, children: [
79
+ "\u8FD8\u6709 ",
80
+ remainingCount,
81
+ " \u9879\u66F4\u65B0\u6539\u8FDB"
82
+ ] })
83
+ ] }),
84
+ /* @__PURE__ */ jsxs("div", { style: dark ? { ...styles.footer, ...styles.footerDark } : styles.footer, children: [
85
+ viewMoreUrl || onViewMore ? /* @__PURE__ */ jsxs(
86
+ "a",
87
+ {
88
+ href: viewMoreUrl || "javascript:void(0)",
89
+ style: styles.link,
90
+ onClick: handleViewMore,
91
+ children: [
92
+ viewMoreText,
93
+ " \u2192"
94
+ ]
95
+ }
96
+ ) : /* @__PURE__ */ jsx("span", { style: { flex: 1 } }),
97
+ /* @__PURE__ */ jsx(
98
+ "button",
99
+ {
100
+ type: "button",
101
+ style: styles.refreshBtn,
102
+ onClick: onRefresh,
103
+ children: refreshText
104
+ }
105
+ )
106
+ ] })
107
+ ] })
108
+ }
109
+ );
110
+ if (layout === "floating" && typeof document !== "undefined") {
111
+ return createPortal(cardContent, document.body);
112
+ }
113
+ return cardContent;
114
+ }
115
+ var styles = {
116
+ wrapperFloating: {
117
+ position: "fixed",
118
+ right: 20,
119
+ bottom: 20,
120
+ zIndex: 9998,
121
+ width: 360,
122
+ maxWidth: "calc(100vw - 32px)",
123
+ fontFamily: "-apple-system, BlinkMacSystemFont, 'Segoe UI', Roboto, Helvetica, Arial, sans-serif"
124
+ },
125
+ wrapperInline: {
126
+ position: "relative",
127
+ width: "100%",
128
+ margin: "12px 0",
129
+ fontFamily: "-apple-system, BlinkMacSystemFont, 'Segoe UI', Roboto, Helvetica, Arial, sans-serif"
130
+ },
131
+ card: {
132
+ padding: "16px 18px",
133
+ borderRadius: "var(--rg-radius, 14px)",
134
+ background: "var(--rg-card-bg, #ffffff)",
135
+ color: "var(--rg-card-fg, #111827)",
136
+ border: "1px solid var(--rg-card-border, #e5e7eb)",
137
+ boxShadow: "var(--rg-card-shadow, 0 10px 30px -4px rgba(0,0,0,0.12), 0 4px 10px -2px rgba(0,0,0,0.05))"
138
+ },
139
+ cardDark: {
140
+ background: "var(--rg-card-bg-dark, #1f2937)",
141
+ color: "var(--rg-card-fg-dark, #f9fafb)",
142
+ borderColor: "var(--rg-card-border-dark, #374151)",
143
+ boxShadow: "0 10px 30px -4px rgba(0,0,0,0.4)"
144
+ },
145
+ header: {
146
+ display: "flex",
147
+ alignItems: "center",
148
+ justifyContent: "space-between",
149
+ marginBottom: 12
150
+ },
151
+ titleGroup: {
152
+ display: "flex",
153
+ alignItems: "center",
154
+ gap: 8,
155
+ flexWrap: "wrap"
156
+ },
157
+ title: {
158
+ fontSize: 14.5,
159
+ fontWeight: 700,
160
+ letterSpacing: "-0.01em",
161
+ color: "inherit"
162
+ },
163
+ badge: {
164
+ display: "inline-flex",
165
+ alignItems: "center",
166
+ padding: "2px 7px",
167
+ borderRadius: 9999,
168
+ fontSize: 11,
169
+ fontWeight: 600,
170
+ fontFamily: "ui-monospace, SFMono-Regular, Menlo, Monaco, Consolas, monospace",
171
+ background: "var(--rg-badge-bg, rgba(37,99,235,0.1))",
172
+ color: "var(--rg-badge-fg, #2563eb)"
173
+ },
174
+ badgeDark: {
175
+ background: "rgba(59,130,246,0.2)",
176
+ color: "#60a5fa"
177
+ },
178
+ date: {
179
+ fontSize: 11.5,
180
+ color: "var(--rg-muted-fg, #6b7280)"
181
+ },
182
+ closeBtn: {
183
+ display: "inline-flex",
184
+ alignItems: "center",
185
+ justifyContent: "center",
186
+ width: 22,
187
+ height: 22,
188
+ padding: 0,
189
+ border: "none",
190
+ borderRadius: 6,
191
+ background: "transparent",
192
+ color: "var(--rg-muted-fg, #9ca3af)",
193
+ fontSize: 13,
194
+ cursor: "pointer"
195
+ },
196
+ body: {
197
+ marginBottom: 14
198
+ },
199
+ list: {
200
+ margin: 0,
201
+ padding: 0,
202
+ listStyle: "none",
203
+ display: "flex",
204
+ flexDirection: "column",
205
+ gap: 6
206
+ },
207
+ listItem: {
208
+ display: "flex",
209
+ alignItems: "flex-start",
210
+ gap: 6,
211
+ fontSize: 13,
212
+ lineHeight: 1.5
213
+ },
214
+ bulletDot: {
215
+ flexShrink: 0,
216
+ fontSize: 14,
217
+ lineHeight: 1.4,
218
+ color: "var(--rg-accent, #2563eb)"
219
+ },
220
+ bulletText: {
221
+ flex: 1,
222
+ wordBreak: "break-word",
223
+ color: "var(--rg-body-fg, #374151)"
224
+ },
225
+ bulletTextDark: {
226
+ color: "var(--rg-body-fg-dark, #d1d5db)"
227
+ },
228
+ fallbackText: {
229
+ margin: 0,
230
+ fontSize: 13,
231
+ lineHeight: 1.55,
232
+ color: "var(--rg-body-fg, #4b5563)"
233
+ },
234
+ fallbackTextDark: {
235
+ color: "var(--rg-body-fg-dark, #d1d5db)"
236
+ },
237
+ remaining: {
238
+ marginTop: 6,
239
+ paddingLeft: 12,
240
+ fontSize: 11.5,
241
+ color: "var(--rg-muted-fg, #6b7280)"
242
+ },
243
+ footer: {
244
+ display: "flex",
245
+ alignItems: "center",
246
+ justifyContent: "space-between",
247
+ gap: 12,
248
+ paddingTop: 10,
249
+ borderTop: "1px solid var(--rg-card-border, #f3f4f6)"
250
+ },
251
+ footerDark: {
252
+ borderTopColor: "#374151"
253
+ },
254
+ link: {
255
+ fontSize: 12.5,
256
+ fontWeight: 500,
257
+ color: "var(--rg-accent, #2563eb)",
258
+ textDecoration: "none",
259
+ cursor: "pointer"
260
+ },
261
+ refreshBtn: {
262
+ padding: "6px 14px",
263
+ border: "none",
264
+ borderRadius: "var(--rg-btn-radius, 8px)",
265
+ background: "var(--rg-accent, #2563eb)",
266
+ color: "var(--rg-accent-fg, #ffffff)",
267
+ fontSize: 12.5,
268
+ fontWeight: 600,
269
+ cursor: "pointer",
270
+ whiteSpace: "nowrap"
271
+ }
272
+ };
273
+ export {
274
+ UpdateCard
275
+ };
@@ -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
  * React hook wrapping the framework-agnostic UpdateChecker. Mounts/unmounts the checker with
5
7
  * 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: boolean;
11
13
  latestInfo: VersionInfo | null;
14
+ changelog: 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,12 +1,18 @@
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/react/useVersionCheck.ts
6
11
  import { useEffect, useRef, useState } from "react";
7
12
  function useVersionCheck(currentVersion, options = {}) {
8
13
  const [hasUpdate, setHasUpdate] = useState(false);
9
14
  const [latestInfo, setLatestInfo] = useState(null);
15
+ const [changelog, setChangelog] = useState(null);
10
16
  const checkerRef = useRef(null);
11
17
  if (!checkerRef.current) {
12
18
  checkerRef.current = new UpdateChecker(currentVersion, options);
@@ -17,24 +23,33 @@ function useVersionCheck(currentVersion, options = {}) {
17
23
  setHasUpdate(true);
18
24
  setLatestInfo(info);
19
25
  });
26
+ const offChangelog = checker2.on("changelog-loaded", (parsed) => {
27
+ setChangelog(parsed);
28
+ });
20
29
  checker2.start();
21
30
  return () => {
22
31
  checker2.stop();
23
32
  off();
33
+ offChangelog();
24
34
  };
25
35
  }, []);
26
36
  const checker = checkerRef.current;
27
37
  return {
28
38
  hasUpdate,
29
39
  latestInfo,
40
+ changelog,
30
41
  mode: options.mode ?? "toast-auto",
31
42
  applyUpdate: () => checker.applyUpdate(),
32
43
  checkNow: () => checker.checkNow(),
44
+ fetchChangelog: (url) => checker.fetchChangelog(url),
33
45
  /** Feed a vite-plugin-pwa `onNeedRefresh` (or any other out-of-band) signal into the same mode logic. */
34
46
  notifyExternalUpdate: (info) => checker.notifyExternalUpdate(info),
35
47
  checker
36
48
  };
37
49
  }
38
50
  export {
51
+ extractBulletPoints,
52
+ fetchChangelog,
53
+ parseChangelog,
39
54
  useVersionCheck
40
55
  };