dfs-page-config 0.9.0 → 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.
package/package.json CHANGED
@@ -1,10 +1,11 @@
1
1
  {
2
2
  "name": "dfs-page-config",
3
- "version": "0.9.0",
3
+ "version": "0.9.1",
4
4
  "type": "module",
5
5
  "files": [
6
6
  "dist",
7
- "vite-plugin-app-update.js"
7
+ "vite-plugin-app-update.js",
8
+ "vite-plugin-app-update.cjs"
8
9
  ],
9
10
  "main": "./dist/dfs-page-config.umd.cjs",
10
11
  "module": "./dist/dfs-page-config.js",
@@ -13,12 +14,15 @@
13
14
  "import": "./dist/dfs-page-config.js",
14
15
  "require": "./dist/dfs-page-config.umd.cjs"
15
16
  },
17
+ "./vite-plugin-app-update": {
18
+ "import": "./vite-plugin-app-update.js",
19
+ "require": "./vite-plugin-app-update.cjs"
20
+ },
16
21
  "./app-update": {
17
22
  "import": "./dist/app-update.js",
18
23
  "require": "./dist/app-update.cjs"
19
24
  },
20
- "./css": "./dist/style.css",
21
- "./vite-plugin-app-update": "./vite-plugin-app-update.js"
25
+ "./css": "./dist/style.css"
22
26
  },
23
27
  "scripts": {
24
28
  "dev": "vite",
@@ -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;
@@ -1,13 +1,8 @@
1
1
  /**
2
2
  * Vite 插件:根据业务项目根目录 app-update.config.json 生成 /version.json
3
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 })
4
+ * import generateAppUpdateVersion from 'dfs-page-config/vite-plugin-app-update'
5
+ * plugins: [generateAppUpdateVersion()]
11
6
  *
12
7
  * app-update.config.json:
13
8
  * { "breaking": 0, "forceReloadRoutes": [] }