dfs-page-config 0.8.15 → 0.9.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.
@@ -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,11 @@
1
1
  {
2
2
  "name": "dfs-page-config",
3
- "version": "0.8.15",
3
+ "version": "0.9.1",
4
4
  "type": "module",
5
5
  "files": [
6
- "dist"
6
+ "dist",
7
+ "vite-plugin-app-update.js",
8
+ "vite-plugin-app-update.cjs"
7
9
  ],
8
10
  "main": "./dist/dfs-page-config.umd.cjs",
9
11
  "module": "./dist/dfs-page-config.js",
@@ -12,11 +14,19 @@
12
14
  "import": "./dist/dfs-page-config.js",
13
15
  "require": "./dist/dfs-page-config.umd.cjs"
14
16
  },
17
+ "./vite-plugin-app-update": {
18
+ "import": "./vite-plugin-app-update.js",
19
+ "require": "./vite-plugin-app-update.cjs"
20
+ },
21
+ "./app-update": {
22
+ "import": "./dist/app-update.js",
23
+ "require": "./dist/app-update.cjs"
24
+ },
15
25
  "./css": "./dist/style.css"
16
26
  },
17
27
  "scripts": {
18
28
  "dev": "vite",
19
- "build": "vite build",
29
+ "build": "vite build && vite build --config vite.config.app-update.ts",
20
30
  "package": "vue-tsc && build",
21
31
  "preview": "vite preview"
22
32
  },
@@ -51,6 +61,7 @@
51
61
  "peerDependencies": {
52
62
  "axios": ">=0.21.1",
53
63
  "vue": ">=3.2.22",
64
+ "vue-router": ">=4.0.0",
54
65
  "vuex": ">=4.0.0"
55
66
  }
56
67
  }
@@ -0,0 +1,80 @@
1
+ /**
2
+ * Vite 插件(CJS):根据业务项目根目录 app-update.config.json 生成 /version.json
3
+
4
+ * import generateAppUpdateVersion from 'dfs-page-config/vite-plugin-app-update'
5
+ * plugins: [generateAppUpdateVersion()]
6
+ */
7
+ const fs = require("fs");
8
+ const path = require("path");
9
+
10
+ /**
11
+ * @param {object} [options]
12
+ * @param {string} [options.configFile='app-update.config.json'] 相对 cwd 的配置路径
13
+ * @param {string} [options.outFile='version.json'] 写入 dist 的文件名
14
+ * @returns {import('vite').Plugin}
15
+ */
16
+ function generateAppUpdateVersion(options = {}) {
17
+ const configFile = options.configFile || "app-update.config.json";
18
+ const outFile = options.outFile || "version.json";
19
+
20
+ function readConfig() {
21
+ const configPath = path.resolve(process.cwd(), configFile);
22
+ if (!fs.existsSync(configPath)) {
23
+ return { breaking: 0, forceReloadRoutes: [] };
24
+ }
25
+ const raw = JSON.parse(fs.readFileSync(configPath, "utf-8"));
26
+ return {
27
+ breaking: Number(raw.breaking) || 0,
28
+ forceReloadRoutes: Array.isArray(raw.forceReloadRoutes)
29
+ ? raw.forceReloadRoutes.map(String)
30
+ : [],
31
+ };
32
+ }
33
+
34
+ function buildPolicy() {
35
+ const config = readConfig();
36
+ return {
37
+ version: `${Date.now()}`,
38
+ breaking: config.breaking,
39
+ forceReloadRoutes: config.forceReloadRoutes,
40
+ };
41
+ }
42
+
43
+ return {
44
+ name: "dfs-generate-app-update-version",
45
+ configureServer(server) {
46
+ server.middlewares.use((req, res, next) => {
47
+ const url = req.url?.split("?")[0];
48
+ if (url !== `/${outFile}`) {
49
+ next();
50
+ return;
51
+ }
52
+ try {
53
+ const body = JSON.stringify(buildPolicy());
54
+ res.setHeader("Content-Type", "application/json; charset=utf-8");
55
+ res.setHeader("Cache-Control", "no-store, no-cache, must-revalidate");
56
+ res.end(body);
57
+ } catch (err) {
58
+ res.statusCode = 500;
59
+ res.end(JSON.stringify({ message: String(err) }));
60
+ }
61
+ });
62
+ },
63
+ closeBundle() {
64
+ const outDir = path.resolve(process.cwd(), "dist");
65
+ if (!fs.existsSync(outDir)) return;
66
+ const policy = buildPolicy();
67
+ fs.writeFileSync(
68
+ path.join(outDir, outFile),
69
+ JSON.stringify(policy, null, 2),
70
+ "utf-8"
71
+ );
72
+ console.log(
73
+ `[dfs-app-update] wrote ${outFile} version=${policy.version} breaking=${policy.breaking}`
74
+ );
75
+ },
76
+ };
77
+ }
78
+
79
+ module.exports = generateAppUpdateVersion;
80
+ module.exports.default = generateAppUpdateVersion;
@@ -0,0 +1,80 @@
1
+ /**
2
+ * Vite 插件:根据业务项目根目录 app-update.config.json 生成 /version.json
3
+ *
4
+ * import generateAppUpdateVersion from 'dfs-page-config/vite-plugin-app-update'
5
+ * plugins: [generateAppUpdateVersion()]
6
+ *
7
+ * app-update.config.json:
8
+ * { "breaking": 0, "forceReloadRoutes": [] }
9
+ */
10
+ import fs from "fs";
11
+ import path from "path";
12
+
13
+ /**
14
+ * @param {object} [options]
15
+ * @param {string} [options.configFile='app-update.config.json'] 相对 cwd 的配置路径
16
+ * @param {string} [options.outFile='version.json'] 写入 dist 的文件名
17
+ * @returns {import('vite').Plugin}
18
+ */
19
+ export default function generateAppUpdateVersion(options = {}) {
20
+ const configFile = options.configFile || "app-update.config.json";
21
+ const outFile = options.outFile || "version.json";
22
+
23
+ function readConfig() {
24
+ const configPath = path.resolve(process.cwd(), configFile);
25
+ if (!fs.existsSync(configPath)) {
26
+ return { breaking: 0, forceReloadRoutes: [] };
27
+ }
28
+ const raw = JSON.parse(fs.readFileSync(configPath, "utf-8"));
29
+ return {
30
+ breaking: Number(raw.breaking) || 0,
31
+ forceReloadRoutes: Array.isArray(raw.forceReloadRoutes)
32
+ ? raw.forceReloadRoutes.map(String)
33
+ : [],
34
+ };
35
+ }
36
+
37
+ function buildPolicy() {
38
+ const config = readConfig();
39
+ return {
40
+ version: `${Date.now()}`,
41
+ breaking: config.breaking,
42
+ forceReloadRoutes: config.forceReloadRoutes,
43
+ };
44
+ }
45
+
46
+ return {
47
+ name: "dfs-generate-app-update-version",
48
+ configureServer(server) {
49
+ server.middlewares.use((req, res, next) => {
50
+ const url = req.url?.split("?")[0];
51
+ if (url !== `/${outFile}`) {
52
+ next();
53
+ return;
54
+ }
55
+ try {
56
+ const body = JSON.stringify(buildPolicy());
57
+ res.setHeader("Content-Type", "application/json; charset=utf-8");
58
+ res.setHeader("Cache-Control", "no-store, no-cache, must-revalidate");
59
+ res.end(body);
60
+ } catch (err) {
61
+ res.statusCode = 500;
62
+ res.end(JSON.stringify({ message: String(err) }));
63
+ }
64
+ });
65
+ },
66
+ closeBundle() {
67
+ const outDir = path.resolve(process.cwd(), "dist");
68
+ if (!fs.existsSync(outDir)) return;
69
+ const policy = buildPolicy();
70
+ fs.writeFileSync(
71
+ path.join(outDir, outFile),
72
+ JSON.stringify(policy, null, 2),
73
+ "utf-8"
74
+ );
75
+ console.log(
76
+ `[dfs-app-update] wrote ${outFile} version=${policy.version} breaking=${policy.breaking}`
77
+ );
78
+ },
79
+ };
80
+ }