xc-web-update-notice-vite 0.0.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/README.md +105 -0
- package/package.json +44 -0
package/README.md
ADDED
|
@@ -0,0 +1,105 @@
|
|
|
1
|
+
# xc-web-update-notice-vite
|
|
2
|
+
|
|
3
|
+
Vite 版本的前端版本检测通知插件。
|
|
4
|
+
|
|
5
|
+
## 📥 安装
|
|
6
|
+
|
|
7
|
+
```bash
|
|
8
|
+
npm install xc-web-update-notice-vite
|
|
9
|
+
```
|
|
10
|
+
|
|
11
|
+
## ⚙️ 使用方式
|
|
12
|
+
|
|
13
|
+
在 `vite.config.ts` 中添加:
|
|
14
|
+
|
|
15
|
+
```typescript
|
|
16
|
+
import { defineConfig } from 'vite';
|
|
17
|
+
import vue from '@vitejs/plugin-vue';
|
|
18
|
+
import XcUpdateNoticeVitePlugin from 'xc-web-update-notice-vite';
|
|
19
|
+
|
|
20
|
+
export default defineConfig({
|
|
21
|
+
plugins: [
|
|
22
|
+
vue(),
|
|
23
|
+
XcUpdateNoticeVitePlugin({
|
|
24
|
+
interval: 10000,
|
|
25
|
+
// 两小时后再次弹窗(毫秒)
|
|
26
|
+
laterInterval: 200 * 60 * 1000,
|
|
27
|
+
versionDir: "/dist/",
|
|
28
|
+
checkerDir: "/dist/",
|
|
29
|
+
isLogout: true,
|
|
30
|
+
publishDescription: "修改用户权限",
|
|
31
|
+
isProd: process.env.NODE_ENV === "production",
|
|
32
|
+
}),
|
|
33
|
+
],
|
|
34
|
+
});
|
|
35
|
+
```
|
|
36
|
+
|
|
37
|
+
## 🧠 运行逻辑说明
|
|
38
|
+
|
|
39
|
+
插件在打包时会自动注入一段脚本:
|
|
40
|
+
- 在浏览器中创建 `window._xcUpdate`
|
|
41
|
+
- 启动一个定时器,定期 fetch 版本文件(默认 `_version.json`)
|
|
42
|
+
- 检测到新版本时触发所有注册的回调
|
|
43
|
+
- 可通过 `window._xcUpdate.updateLater()` 延迟下一次检测
|
|
44
|
+
- 支持"稍后更新"中再次发布新版本时自动更新
|
|
45
|
+
|
|
46
|
+
## 📋 配置选项
|
|
47
|
+
|
|
48
|
+
| 名称 | 类型 | 默认值 | 说明 | 选项 |
|
|
49
|
+
| ------------------------- | --------- | ---------------- | -------------------------------------- | --------------- |
|
|
50
|
+
| `filename` | `string` | `_version.json` | 版本文件名 | |
|
|
51
|
+
| `interval` | `number` | `5000` | 检测间隔(毫秒) | |
|
|
52
|
+
| `laterInterval` | `number` | `1000 * 60 * 10` | 用户点击"稍后更新"后的延迟时间(毫秒) | |
|
|
53
|
+
| `isLogout` | `boolean` | `false` | 本次构建是否需要退出登录 | |
|
|
54
|
+
| `versionDir` | `string` | `""` | 版本文件指向(./\_version.json) | |
|
|
55
|
+
| `checkerDir` | `string` | `""` | 检测脚本指向(update-checker.js) | |
|
|
56
|
+
| `publishDescription` | `string` | | 本次的发布描述 | |
|
|
57
|
+
| `keepVersions` | `number` | `10` | 保留历史版本数量 | |
|
|
58
|
+
| `versionMode` | `string` | `hash` | 版本号生成模式 | `hash`,`custom` |
|
|
59
|
+
| `version` | `string` | `1.0.0` | 版本号(versionMode 为 custom 时生效) | |
|
|
60
|
+
| `isProd` | `boolean` | `true` | 是否是生产环境,非生产环境不检测 | |
|
|
61
|
+
|
|
62
|
+
## 🎯 API
|
|
63
|
+
|
|
64
|
+
### window._xcUpdate
|
|
65
|
+
|
|
66
|
+
运行时会在全局注入一个对象
|
|
67
|
+
|
|
68
|
+
| 名称 | 类型 | 说明 |
|
|
69
|
+
| ------------- | ---------- | ------------------------------------------------------------------------------------- |
|
|
70
|
+
| `onUpdate` | `function` | 开始检测版本 `onUpdate((info) => { console.log('🚀 发现新版本', info) }, '您当前版本')` |
|
|
71
|
+
| `updateLater` | `function` | 延迟下次检测更新 |
|
|
72
|
+
|
|
73
|
+
## 📝 示例
|
|
74
|
+
|
|
75
|
+
```javascript
|
|
76
|
+
// 在你的页面或组件中
|
|
77
|
+
document.addEventListener('DOMContentLoaded', () => {
|
|
78
|
+
window._xcUpdate.onUpdate((info) => {
|
|
79
|
+
console.log('🚀 监测到新版本', info);
|
|
80
|
+
const { isLogout, newHash, publishDescription } = info;
|
|
81
|
+
|
|
82
|
+
// 显示更新提示
|
|
83
|
+
if (isLogout) {
|
|
84
|
+
// 需要退出登录
|
|
85
|
+
alert(`发现新版本:${publishDescription},请重新登录`);
|
|
86
|
+
// 清除登录信息
|
|
87
|
+
localStorage.clear();
|
|
88
|
+
window.location.reload();
|
|
89
|
+
} else {
|
|
90
|
+
// 只需刷新页面
|
|
91
|
+
alert(`发现新版本:${publishDescription},请刷新页面`);
|
|
92
|
+
window.location.reload();
|
|
93
|
+
}
|
|
94
|
+
}, localStorage.getItem("version"));
|
|
95
|
+
});
|
|
96
|
+
```
|
|
97
|
+
|
|
98
|
+
## 📦 相关包
|
|
99
|
+
|
|
100
|
+
- [xc-web-update-notice-webpack](https://www.npmjs.com/package/xc-web-update-notice-webpack) - Webpack 版本
|
|
101
|
+
- [xc-web-update-notice-umijs](https://www.npmjs.com/package/xc-web-update-notice-umijs) - UmiJS 版本
|
|
102
|
+
|
|
103
|
+
## 📄 License
|
|
104
|
+
|
|
105
|
+
ISC
|
package/package.json
ADDED
|
@@ -0,0 +1,44 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "xc-web-update-notice-vite",
|
|
3
|
+
"version": "0.0.1",
|
|
4
|
+
"description": "前端版本更新通知 - Vite 插件",
|
|
5
|
+
"main": "../../dist/cjs/vite/index.js",
|
|
6
|
+
"module": "../../dist/esm/vite/index.js",
|
|
7
|
+
"exports": {
|
|
8
|
+
".": {
|
|
9
|
+
"require": "../../dist/cjs/vite/index.js",
|
|
10
|
+
"import": "../../dist/esm/vite/index.js"
|
|
11
|
+
}
|
|
12
|
+
},
|
|
13
|
+
"files": [
|
|
14
|
+
"../../dist/cjs/vite",
|
|
15
|
+
"../../dist/esm/vite"
|
|
16
|
+
],
|
|
17
|
+
"keywords": [
|
|
18
|
+
"vite",
|
|
19
|
+
"web版本更新通知",
|
|
20
|
+
"version-update"
|
|
21
|
+
],
|
|
22
|
+
"homepage": "https://github.com/futureyn/xc-web-update-notice",
|
|
23
|
+
"repository": {
|
|
24
|
+
"type": "git",
|
|
25
|
+
"url": "git+https://github.com/futureyn/xc-web-update-notice.git",
|
|
26
|
+
"directory": "packages/vite"
|
|
27
|
+
},
|
|
28
|
+
"bugs": {
|
|
29
|
+
"url": "https://github.com/futureyn/xc-web-update-notice/issues"
|
|
30
|
+
},
|
|
31
|
+
"author": "nnn",
|
|
32
|
+
"license": "ISC",
|
|
33
|
+
"scripts": {
|
|
34
|
+
"build": "babel index.js --out-dir ../../dist/cjs/vite --env-name cjs --out-file-extension .js && babel index.js --out-dir ../../dist/esm/vite --env-name esm --out-file-extension .js"
|
|
35
|
+
},
|
|
36
|
+
"peerDependencies": {
|
|
37
|
+
"vite": "^2.0.0 || ^3.0.0 || ^4.0.0 || ^5.0.0 || ^6.0.0 || ^7.0.0"
|
|
38
|
+
},
|
|
39
|
+
"peerDependenciesMeta": {
|
|
40
|
+
"vite": {
|
|
41
|
+
"optional": false
|
|
42
|
+
}
|
|
43
|
+
}
|
|
44
|
+
}
|