dfs-page-config 0.8.15 → 0.9.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 @@
1
+ "use strict";const h=require("element-plus"),i="dfs_app_update:breaking",a="dfs_app_update:pending",f="dfs_app_update:routes";function g(t,r){return!r||r.length===0?!0:r.some(o=>{if(o.endsWith("/**")){const s=o.slice(0,-3);return s?t===s||t.startsWith(`${s}/`):!0}return t===o})}async function S(){try{const t=await fetch(`/version.json?t=${Date.now()}`,{cache:"no-store"});if(!t.ok)return null;const r=await t.json();return{version:String(r.version??""),breaking:Number(r.breaking)||0,forceReloadRoutes:Array.isArray(r.forceReloadRoutes)?r.forceReloadRoutes.map(String):[]}}catch{return null}}let d=!1;async function m(){if(!d){d=!0;try{await h.ElMessageBox.alert("系统已更新,请刷新页面后继续使用","版本更新",{confirmButtonText:"刷新",showClose:!1,closeOnClickModal:!1,closeOnPressEscape:!1,showCancelButton:!1,type:"warning",lockScroll:!0})}catch{}finally{location.reload()}}}function y(t){const r=t.checkIntervalMs??6e4,o=(e,n)=>{if(g(e,n)){sessionStorage.removeItem(a),m();return}sessionStorage.setItem(a,"1"),sessionStorage.setItem(f,JSON.stringify(n))},s=e=>{if(sessionStorage.getItem(a)!=="1")return;let n=[];try{n=JSON.parse(sessionStorage.getItem(f)||"[]")}catch{n=[]}g(e,n)&&(sessionStorage.removeItem(a),m())},c=async()=>{const e=await S();if(!e)return;const n=localStorage.getItem(i);if(n===null){localStorage.setItem(i,String(e.breaking));return}const p=Number(n)||0;e.breaking<=p||(localStorage.setItem(i,String(e.breaking)),o(t.router.currentRoute.value.path,e.forceReloadRoutes))};let l;const u=()=>{window.clearInterval(l),document.visibilityState==="visible"&&(c(),l=window.setInterval(c,r))};document.addEventListener("visibilitychange",u),u(),t.router.afterEach(e=>{s(e.path)})}module.exports=y;
@@ -0,0 +1,91 @@
1
+ import { ElMessageBox as h } from "element-plus";
2
+ const i = "dfs_app_update:breaking", s = "dfs_app_update:pending", f = "dfs_app_update:routes";
3
+ function g(t, r) {
4
+ return !r || r.length === 0 ? !0 : r.some((o) => {
5
+ if (o.endsWith("/**")) {
6
+ const a = o.slice(0, -3);
7
+ return a ? t === a || t.startsWith(`${a}/`) : !0;
8
+ }
9
+ return t === o;
10
+ });
11
+ }
12
+ async function S() {
13
+ try {
14
+ const t = await fetch(`/version.json?t=${Date.now()}`, {
15
+ cache: "no-store"
16
+ });
17
+ if (!t.ok)
18
+ return null;
19
+ const r = await t.json();
20
+ return {
21
+ version: String(r.version ?? ""),
22
+ breaking: Number(r.breaking) || 0,
23
+ forceReloadRoutes: Array.isArray(r.forceReloadRoutes) ? r.forceReloadRoutes.map(String) : []
24
+ };
25
+ } catch {
26
+ return null;
27
+ }
28
+ }
29
+ let d = !1;
30
+ async function m() {
31
+ if (!d) {
32
+ d = !0;
33
+ try {
34
+ await h.alert("系统已更新,请刷新页面后继续使用", "版本更新", {
35
+ confirmButtonText: "刷新",
36
+ showClose: !1,
37
+ closeOnClickModal: !1,
38
+ closeOnPressEscape: !1,
39
+ showCancelButton: !1,
40
+ type: "warning",
41
+ lockScroll: !0
42
+ });
43
+ } catch {
44
+ } finally {
45
+ location.reload();
46
+ }
47
+ }
48
+ }
49
+ function R(t) {
50
+ const r = t.checkIntervalMs ?? 6e4, o = (e, n) => {
51
+ if (g(e, n)) {
52
+ sessionStorage.removeItem(s), m();
53
+ return;
54
+ }
55
+ sessionStorage.setItem(s, "1"), sessionStorage.setItem(f, JSON.stringify(n));
56
+ }, a = (e) => {
57
+ if (sessionStorage.getItem(s) !== "1")
58
+ return;
59
+ let n = [];
60
+ try {
61
+ n = JSON.parse(sessionStorage.getItem(f) || "[]");
62
+ } catch {
63
+ n = [];
64
+ }
65
+ g(e, n) && (sessionStorage.removeItem(s), m());
66
+ }, c = async () => {
67
+ const e = await S();
68
+ if (!e)
69
+ return;
70
+ const n = localStorage.getItem(i);
71
+ if (n === null) {
72
+ localStorage.setItem(i, String(e.breaking));
73
+ return;
74
+ }
75
+ const p = Number(n) || 0;
76
+ e.breaking <= p || (localStorage.setItem(i, String(e.breaking)), o(
77
+ t.router.currentRoute.value.path,
78
+ e.forceReloadRoutes
79
+ ));
80
+ };
81
+ let l;
82
+ const u = () => {
83
+ window.clearInterval(l), document.visibilityState === "visible" && (c(), l = window.setInterval(c, r));
84
+ };
85
+ document.addEventListener("visibilitychange", u), u(), t.router.afterEach((e) => {
86
+ a(e.path);
87
+ });
88
+ }
89
+ export {
90
+ R as default
91
+ };
package/package.json CHANGED
@@ -1,9 +1,10 @@
1
1
  {
2
2
  "name": "dfs-page-config",
3
- "version": "0.8.15",
3
+ "version": "0.9.0",
4
4
  "type": "module",
5
5
  "files": [
6
- "dist"
6
+ "dist",
7
+ "vite-plugin-app-update.js"
7
8
  ],
8
9
  "main": "./dist/dfs-page-config.umd.cjs",
9
10
  "module": "./dist/dfs-page-config.js",
@@ -12,11 +13,16 @@
12
13
  "import": "./dist/dfs-page-config.js",
13
14
  "require": "./dist/dfs-page-config.umd.cjs"
14
15
  },
15
- "./css": "./dist/style.css"
16
+ "./app-update": {
17
+ "import": "./dist/app-update.js",
18
+ "require": "./dist/app-update.cjs"
19
+ },
20
+ "./css": "./dist/style.css",
21
+ "./vite-plugin-app-update": "./vite-plugin-app-update.js"
16
22
  },
17
23
  "scripts": {
18
24
  "dev": "vite",
19
- "build": "vite build",
25
+ "build": "vite build && vite build --config vite.config.app-update.ts",
20
26
  "package": "vue-tsc && build",
21
27
  "preview": "vite preview"
22
28
  },
@@ -51,6 +57,7 @@
51
57
  "peerDependencies": {
52
58
  "axios": ">=0.21.1",
53
59
  "vue": ">=3.2.22",
60
+ "vue-router": ">=4.0.0",
54
61
  "vuex": ">=4.0.0"
55
62
  }
56
63
  }
@@ -0,0 +1,85 @@
1
+ /**
2
+ * Vite 插件:根据业务项目根目录 app-update.config.json 生成 /version.json
3
+ *
4
+ * 构建侧(业务项目 vite.config.ts):
5
+ * import generateAppUpdateVersion from 'dfs-page-config/vite-plugin-app-update'
6
+ * plugins: [generateAppUpdateVersion()]
7
+ *
8
+ * 运行侧(业务项目 main.ts,轻量入口,不拉整包):
9
+ * import setupAppUpdateWatcher from 'dfs-page-config/app-update'
10
+ * setupAppUpdateWatcher({ router })
11
+ *
12
+ * app-update.config.json:
13
+ * { "breaking": 0, "forceReloadRoutes": [] }
14
+ */
15
+ import fs from "fs";
16
+ import path from "path";
17
+
18
+ /**
19
+ * @param {object} [options]
20
+ * @param {string} [options.configFile='app-update.config.json'] 相对 cwd 的配置路径
21
+ * @param {string} [options.outFile='version.json'] 写入 dist 的文件名
22
+ * @returns {import('vite').Plugin}
23
+ */
24
+ export default function generateAppUpdateVersion(options = {}) {
25
+ const configFile = options.configFile || "app-update.config.json";
26
+ const outFile = options.outFile || "version.json";
27
+
28
+ function readConfig() {
29
+ const configPath = path.resolve(process.cwd(), configFile);
30
+ if (!fs.existsSync(configPath)) {
31
+ return { breaking: 0, forceReloadRoutes: [] };
32
+ }
33
+ const raw = JSON.parse(fs.readFileSync(configPath, "utf-8"));
34
+ return {
35
+ breaking: Number(raw.breaking) || 0,
36
+ forceReloadRoutes: Array.isArray(raw.forceReloadRoutes)
37
+ ? raw.forceReloadRoutes.map(String)
38
+ : [],
39
+ };
40
+ }
41
+
42
+ function buildPolicy() {
43
+ const config = readConfig();
44
+ return {
45
+ version: `${Date.now()}`,
46
+ breaking: config.breaking,
47
+ forceReloadRoutes: config.forceReloadRoutes,
48
+ };
49
+ }
50
+
51
+ return {
52
+ name: "dfs-generate-app-update-version",
53
+ configureServer(server) {
54
+ server.middlewares.use((req, res, next) => {
55
+ const url = req.url?.split("?")[0];
56
+ if (url !== `/${outFile}`) {
57
+ next();
58
+ return;
59
+ }
60
+ try {
61
+ const body = JSON.stringify(buildPolicy());
62
+ res.setHeader("Content-Type", "application/json; charset=utf-8");
63
+ res.setHeader("Cache-Control", "no-store, no-cache, must-revalidate");
64
+ res.end(body);
65
+ } catch (err) {
66
+ res.statusCode = 500;
67
+ res.end(JSON.stringify({ message: String(err) }));
68
+ }
69
+ });
70
+ },
71
+ closeBundle() {
72
+ const outDir = path.resolve(process.cwd(), "dist");
73
+ if (!fs.existsSync(outDir)) return;
74
+ const policy = buildPolicy();
75
+ fs.writeFileSync(
76
+ path.join(outDir, outFile),
77
+ JSON.stringify(policy, null, 2),
78
+ "utf-8"
79
+ );
80
+ console.log(
81
+ `[dfs-app-update] wrote ${outFile} version=${policy.version} breaking=${policy.breaking}`
82
+ );
83
+ },
84
+ };
85
+ }