qy-vue-plugins 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 ADDED
@@ -0,0 +1,5 @@
1
+ # Vue 3 + TypeScript + Vite
2
+
3
+ This template should help get you started developing with Vue 3 and TypeScript in Vite. The template uses Vue 3 `<script setup>` SFCs, check out the [script setup docs](https://v3.vuejs.org/api/sfc-script-setup.html#sfc-script-setup) to learn more.
4
+
5
+ Learn more about the recommended Project Setup and IDE Support in the [Vue Docs TypeScript Guide](https://vuejs.org/guide/typescript/overview.html#project-setup).
@@ -0,0 +1,71 @@
1
+ import { ref as g } from "vue";
2
+ function m(t = "build-time.json") {
3
+ let e = /* @__PURE__ */ new Date();
4
+ return {
5
+ name: "vite-plugin-build-time",
6
+ enforce: "post",
7
+ config: () => (console.log("vite-plugin-build-time: config"), {
8
+ define: {
9
+ // 将时间注入到环境变量中
10
+ "import.meta.env.BUILD_TIME": JSON.stringify(e.getTime())
11
+ }
12
+ }),
13
+ generateBundle() {
14
+ console.log("vite-plugin-build-time: generateBundle");
15
+ const i = JSON.stringify(
16
+ {
17
+ timestamp: e.getTime(),
18
+ isoString: e.toISOString(),
19
+ localeString: e.toLocaleString()
20
+ },
21
+ null,
22
+ 2
23
+ );
24
+ this.emitFile({
25
+ type: "asset",
26
+ fileName: t,
27
+ source: i
28
+ });
29
+ }
30
+ };
31
+ }
32
+ const n = g(!1), f = async (t, e = 1e3, i) => {
33
+ let l = `//${window.location.host}/version.json?t=${(/* @__PURE__ */ new Date()).getTime()}`;
34
+ if (n.value) return { stop: () => {
35
+ } };
36
+ n.value = !0;
37
+ const s = new AbortController(), c = s.signal;
38
+ try {
39
+ const o = async () => {
40
+ if (!c.aborted)
41
+ try {
42
+ const r = await fetch(l, { method: "GET" });
43
+ if (r.status === 200) {
44
+ let a = (await r.json()).timestamp || 0;
45
+ if (!t)
46
+ return;
47
+ t < a && i(a);
48
+ }
49
+ } catch {
50
+ }
51
+ };
52
+ await o();
53
+ const u = setInterval(o, e);
54
+ return {
55
+ stop: () => {
56
+ clearInterval(u), s.abort(), n.value = !1;
57
+ }
58
+ };
59
+ } catch {
60
+ return n.value = !1, { stop: () => {
61
+ } };
62
+ }
63
+ }, v = {
64
+ compileTimePlugin: m,
65
+ isNewVersion: f
66
+ };
67
+ export {
68
+ m as compileTimePlugin,
69
+ v as default,
70
+ f as isNewVersion
71
+ };
@@ -0,0 +1 @@
1
+ (function(e,i){typeof exports=="object"&&typeof module<"u"?i(exports,require("vue")):typeof define=="function"&&define.amd?define(["exports","vue"],i):(e=typeof globalThis<"u"?globalThis:e||self,i(e["qy-vue-plugins"]={},e.Vue))})(this,function(e,i){"use strict";function u(o="build-time.json"){let t=new Date;return{name:"vite-plugin-build-time",enforce:"post",config:()=>(console.log("vite-plugin-build-time: config"),{define:{"import.meta.env.BUILD_TIME":JSON.stringify(t.getTime())}}),generateBundle(){console.log("vite-plugin-build-time: generateBundle");const r=JSON.stringify({timestamp:t.getTime(),isoString:t.toISOString(),localeString:t.toLocaleString()},null,2);this.emitFile({type:"asset",fileName:o,source:r})}}}const n=i.ref(!1),a=async(o,t=1e3,r)=>{let g=`//${window.location.host}/version.json?t=${new Date().getTime()}`;if(n.value)return{stop:()=>{}};n.value=!0;const c=new AbortController,m=c.signal;try{const s=async()=>{if(!m.aborted)try{const l=await fetch(g,{method:"GET"});if(l.status===200){let f=(await l.json()).timestamp||0;if(!o)return;o<f&&r(f)}}catch{}};await s();const p=setInterval(s,t);return{stop:()=>{clearInterval(p),c.abort(),n.value=!1}}}catch{return n.value=!1,{stop:()=>{}}}},d={compileTimePlugin:u,isNewVersion:a};e.compileTimePlugin=u,e.default=d,e.isNewVersion=a,Object.defineProperties(e,{__esModule:{value:!0},[Symbol.toStringTag]:{value:"Module"}})});
@@ -0,0 +1,39 @@
1
+ import type { PluginOption } from "vite";
2
+
3
+ export default function compileTimePlugin(DEFAULT_FILENAME: string = "build-time.json"): PluginOption {
4
+ let buildTime: Date = new Date();
5
+
6
+ return {
7
+ name: "vite-plugin-build-time",
8
+ enforce: "post",
9
+ config: () => {
10
+ console.log("vite-plugin-build-time: config");
11
+ // 获取当前时间
12
+ return {
13
+ define: {
14
+ // 将时间注入到环境变量中
15
+ "import.meta.env.BUILD_TIME": JSON.stringify(buildTime.getTime()),
16
+ },
17
+ };
18
+ },
19
+
20
+ generateBundle() {
21
+ console.log("vite-plugin-build-time: generateBundle");
22
+ const content = JSON.stringify(
23
+ {
24
+ timestamp: buildTime.getTime(),
25
+ isoString: buildTime.toISOString(),
26
+ localeString: buildTime.toLocaleString(),
27
+ },
28
+ null,
29
+ 2
30
+ );
31
+
32
+ this.emitFile({
33
+ type: "asset",
34
+ fileName: DEFAULT_FILENAME,
35
+ source: content,
36
+ });
37
+ },
38
+ } as PluginOption;
39
+ }
@@ -0,0 +1,61 @@
1
+ import { ref } from "vue";
2
+
3
+ const isCheckingForUpdate = ref(false);
4
+ // 检查是否有新版本
5
+ export const isNewVersion = async (localVueVersion: number, interval: number = 1000, cb: (newVersion: number) => void): Promise<{ stop: () => void }> => {
6
+ // @ts-ignore
7
+ let url = `//${window.location.host}/version.json?t=${new Date().getTime()}`;
8
+
9
+ if (isCheckingForUpdate.value) return { stop: () => {} };
10
+
11
+ isCheckingForUpdate.value = true;
12
+
13
+ // 使用 AbortController 来管理定时器
14
+ const controller = new AbortController();
15
+ const signal = controller.signal;
16
+
17
+ try {
18
+ // 检查版本的函数
19
+ const checkVersion = async () => {
20
+ if (signal.aborted) {
21
+ return;
22
+ }
23
+
24
+ try {
25
+ const res = await fetch(url, { method: "GET" });
26
+
27
+ if (res.status === 200) {
28
+ const data = await res.json();
29
+ let vueVersion = data.timestamp || 0;
30
+
31
+ // 如果没有本地版本记录,则保存当前版本号并返回
32
+ if (!localVueVersion) {
33
+ return;
34
+ }
35
+
36
+ if (localVueVersion < vueVersion) {
37
+ cb(vueVersion); // 发现新版本,执行回调
38
+ }
39
+ }
40
+ } catch (error) {}
41
+ };
42
+
43
+ // 先执行一次检查
44
+ await checkVersion();
45
+
46
+ // 创建定时器,定期检查
47
+ const intervalId = setInterval(checkVersion, interval);
48
+
49
+ // 返回停止函数
50
+ return {
51
+ stop: () => {
52
+ clearInterval(intervalId);
53
+ controller.abort();
54
+ isCheckingForUpdate.value = false;
55
+ },
56
+ };
57
+ } catch (error) {
58
+ isCheckingForUpdate.value = false;
59
+ return { stop: () => {} };
60
+ }
61
+ };
@@ -0,0 +1,11 @@
1
+ import compileTimePlugin from "./buildTime";
2
+ import { isNewVersion } from "./checkBuildTime";
3
+
4
+ // 导出为命名导出
5
+ export { compileTimePlugin, isNewVersion };
6
+
7
+ // 同时为 UMD 格式添加默认导出
8
+ export default {
9
+ compileTimePlugin,
10
+ isNewVersion,
11
+ };
package/package.json ADDED
@@ -0,0 +1,36 @@
1
+ {
2
+ "name": "qy-vue-plugins",
3
+ "private": false,
4
+ "version": "0.0.1",
5
+ "type": "module",
6
+ "scripts": {
7
+ "dev": "vite",
8
+ "build": "vue-tsc -b && vite build",
9
+ "preview": "vite preview",
10
+ "test": "jest"
11
+ },
12
+ "dependencies": {
13
+ "vue": "^3.5.13"
14
+ },
15
+ "files": [
16
+ "dist/*",
17
+ "package/*"
18
+ ],
19
+ "devDependencies": {
20
+ "@types/node": "^22.14.1",
21
+ "@vitejs/plugin-vue": "^5.2.1",
22
+ "@vue/tsconfig": "^0.7.0",
23
+ "typescript": "~5.7.2",
24
+ "vite": "^6.2.0",
25
+ "vue-tsc": "^2.2.4"
26
+ },
27
+ "main": "./dist/vue-plugins.umd.js",
28
+ "module": "./dist/vue-plugins.es.js",
29
+ "exports": {
30
+ ".": {
31
+ "import": "./dist/vue-plugins.es.js",
32
+ "require": "./dist/vue-plugins.umd.js"
33
+ }
34
+ },
35
+ "types": "./dist/index.d.ts"
36
+ }