From f0e31aceb87eba1228a84dea66d54672ec1d4ed3 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E7=A5=9E=E4=BB=99=E9=83=BD=E6=B2=A1=E7=94=A8?= <615206459@qq.com> Date: Sat, 27 Apr 2024 18:42:29 +0800 Subject: [PATCH] =?UTF-8?q?=E4=BC=98=E5=8C=96?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- package.json | 2 +- packages/crud/src/utils/parse.tsx | 6 +++-- packages/vite-plugin/dist/index.js | 26 ++++++++++++++++----- packages/vite-plugin/package.json | 2 +- packages/vite-plugin/src/virtual.ts | 35 ++++++++++++++++++++++++----- pnpm-lock.yaml | 8 +++---- 6 files changed, 60 insertions(+), 19 deletions(-) diff --git a/package.json b/package.json index af58f99..a4a6a54 100644 --- a/package.json +++ b/package.json @@ -37,7 +37,7 @@ "xlsx": "^0.18.5" }, "devDependencies": { - "@cool-vue/vite-plugin": "^7.1.1", + "@cool-vue/vite-plugin": "^7.1.2", "@types/file-saver": "^2.0.7", "@types/lodash-es": "^4.17.8", "@types/mockjs": "^1.0.7", diff --git a/packages/crud/src/utils/parse.tsx b/packages/crud/src/utils/parse.tsx index 92a5ea4..d786afc 100644 --- a/packages/crud/src/utils/parse.tsx +++ b/packages/crud/src/utils/parse.tsx @@ -62,9 +62,11 @@ export function parseTableDict(value: any, item: ClTable.Column) { label: v, value: v }; - delete d.children; - return d; + return { + ...d, + children: [] + }; }); // 格式化返回 diff --git a/packages/vite-plugin/dist/index.js b/packages/vite-plugin/dist/index.js index 9aa7175..24db121 100644 --- a/packages/vite-plugin/dist/index.js +++ b/packages/vite-plugin/dist/index.js @@ -724,17 +724,31 @@ async function virtual() { const virtualModuleIds = ["virtual:eps", "virtual:ctx"]; - const eps = await createEps(); - const ctx = await createCtx(); return { name: "vite-cool-virtual", enforce: "pre", + configureServer(server) { + server.middlewares.use(async (req, res, next) => { + // 页面刷新时触发 + if (req.url == "/@vite/client") { + // 重新加载虚拟模块 + virtualModuleIds.forEach((vm) => { + const mod = server.moduleGraph.getModuleById(`\0${vm}`); + if (mod) { + server.moduleGraph.invalidateModule(mod); + } + }); + } + next(); + }); + }, handleHotUpdate({ file, server }) { - if (!["pages.json", "dist", "build/cool"].some((e) => file.includes(e))) { + // 文件修改时触发 + if (!["pages.json", "dist", "build/cool", "eps.json", "eps.d.ts"].some((e) => file.includes(e))) { createCtx(); createEps().then((data) => { // 通知客户端刷新 - server.ws.send({ + (server.hot || server.ws).send({ type: "custom", event: "eps-update", data, @@ -747,13 +761,15 @@ return "\0" + id; } }, - load(id) { + async load(id) { if (id === "\0virtual:eps") { + const eps = await createEps(); return ` export const eps = ${JSON.stringify(eps)} `; } if (id === "\0virtual:ctx") { + const ctx = await createCtx(); return ` export const ctx = ${JSON.stringify(ctx)} `; diff --git a/packages/vite-plugin/package.json b/packages/vite-plugin/package.json index 79e6761..3242de1 100644 --- a/packages/vite-plugin/package.json +++ b/packages/vite-plugin/package.json @@ -1,6 +1,6 @@ { "name": "@cool-vue/vite-plugin", - "version": "7.1.1", + "version": "7.1.2", "description": "cool-admin/cool-uni builder", "main": "/dist/index.js", "scripts": { diff --git a/packages/vite-plugin/src/virtual.ts b/packages/vite-plugin/src/virtual.ts index 764d436..8957b8f 100644 --- a/packages/vite-plugin/src/virtual.ts +++ b/packages/vite-plugin/src/virtual.ts @@ -5,19 +5,38 @@ import { createCtx } from "./ctx"; export async function virtual(): Promise { const virtualModuleIds: string[] = ["virtual:eps", "virtual:ctx"]; - const eps = await createEps(); - const ctx = await createCtx(); - return { name: "vite-cool-virtual", enforce: "pre", + configureServer(server) { + server.middlewares.use(async (req, res, next) => { + // 页面刷新时触发 + if (req.url == "/@vite/client") { + // 重新加载虚拟模块 + virtualModuleIds.forEach((vm) => { + const mod = server.moduleGraph.getModuleById(`\0${vm}`); + + if (mod) { + server.moduleGraph.invalidateModule(mod); + } + }); + } + + next(); + }); + }, handleHotUpdate({ file, server }) { - if (!["pages.json", "dist", "build/cool"].some((e) => file.includes(e))) { + // 文件修改时触发 + if ( + !["pages.json", "dist", "build/cool", "eps.json", "eps.d.ts"].some((e) => + file.includes(e), + ) + ) { createCtx(); createEps().then((data) => { // 通知客户端刷新 - server.ws.send({ + (server.hot || server.ws).send({ type: "custom", event: "eps-update", data, @@ -30,13 +49,17 @@ export async function virtual(): Promise { return "\0" + id; } }, - load(id) { + async load(id) { if (id === "\0virtual:eps") { + const eps = await createEps(); + return ` export const eps = ${JSON.stringify(eps)} `; } if (id === "\0virtual:ctx") { + const ctx = await createCtx(); + return ` export const ctx = ${JSON.stringify(ctx)} `; diff --git a/pnpm-lock.yaml b/pnpm-lock.yaml index e54ad2f..9740771 100644 --- a/pnpm-lock.yaml +++ b/pnpm-lock.yaml @@ -86,8 +86,8 @@ dependencies: devDependencies: '@cool-vue/vite-plugin': - specifier: ^7.1.1 - version: 7.1.1 + specifier: ^7.1.2 + version: 7.1.2 '@types/file-saver': specifier: ^2.0.7 version: 2.0.7 @@ -482,8 +482,8 @@ packages: - typescript dev: false - /@cool-vue/vite-plugin@7.1.1: - resolution: {integrity: sha512-bCf0+ICwjYWYdS3DOrgr74YulvQLVjzNKPiTeL32pTyTecmFSOqbiQ+PbVYxIPHtZjnNjupWOz5vYm1kmBscWQ==} + /@cool-vue/vite-plugin@7.1.2: + resolution: {integrity: sha512-VRkJWIJP3eMJod7gCqrSdpKUOHrJJYwW2jyuEo1LroFjgPqhf1qH2IvPFRreKDdiFfDlwwE4g67nP/VBI/CpOw==} dependencies: '@vue/compiler-sfc': 3.4.25 axios: 1.6.8