From bc1c18b4158b2ced97659814f4d9e2acaaedd4f5 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: Mon, 30 Oct 2023 21:30:29 +0800 Subject: [PATCH] =?UTF-8?q?=E8=A7=A3=E5=86=B3eps=E6=9C=AC=E5=9C=B0?= =?UTF-8?q?=E4=B8=8D=E5=88=B7=E6=96=B0=E9=97=AE=E9=A2=98?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- build/cool/virtual.ts | 39 +++++++++++++++++++-------------------- package.json | 2 +- src/cool/bootstrap/eps.ts | 5 ++++- yarn.lock | 8 ++++---- 4 files changed, 28 insertions(+), 26 deletions(-) diff --git a/build/cool/virtual.ts b/build/cool/virtual.ts index b149f59..d747754 100644 --- a/build/cool/virtual.ts +++ b/build/cool/virtual.ts @@ -1,43 +1,42 @@ -import type { Plugin, ViteDevServer } from "vite"; +import type { Plugin } from "vite"; import { createEps } from "./eps"; import { createModule } from "./module"; export function virtual(): Plugin { const virtualModuleIds = ["virtual:eps", "virtual:module"]; - // 使虚拟模块失效,重新加载 - function buildEps(server: ViteDevServer) { - virtualModuleIds.forEach((vm) => { - const mod = server.moduleGraph.getModuleById(`\0${vm}`); - - if (mod) { - server.moduleGraph.invalidateModule(mod); - } - }); - } - return { name: "vite-cool-virtual", enforce: "pre", configureServer(server) { server.middlewares.use(async (req, res, next) => { - // 页面刷新时触发 eps 刷新 + // 页面刷新时触发 if (req.url == "/@vite/client") { - buildEps(server); + // 重新加载虚拟模块 + virtualModuleIds.forEach((vm) => { + const mod = server.moduleGraph.getModuleById(`\0${vm}`); + + if (mod) { + server.moduleGraph.invalidateModule(mod); + } + }); } next(); }); }, - handleHotUpdate({ file, server }) { - // 代码保存时触发 eps 刷新 + async handleHotUpdate({ file, server }) { + // 代码保存时触发 if (!file.includes("build/cool/dist")) { - buildEps(server); + const { service } = await createEps(); // 通知客户端刷新 server.ws.send({ type: "custom", - event: "eps-update" + event: "eps-update", + data: { + service + } }); } }, @@ -48,10 +47,10 @@ export function virtual(): Plugin { }, async load(id) { if (id === "\0virtual:eps") { - const { service, list } = await createEps(); + const { service } = await createEps(); return ` - export const eps = ${JSON.stringify({ service, list })} + export const eps = ${JSON.stringify({ service })} `; } diff --git a/package.json b/package.json index 9da2efd..98c5d2b 100644 --- a/package.json +++ b/package.json @@ -61,7 +61,7 @@ "sass": "^1.66.1", "terser": "^5.19.2", "typescript": "^5.2.2", - "vite": "^4.4.9", + "vite": "^4.5.0", "vite-plugin-compression": "^0.5.1" } } diff --git a/src/cool/bootstrap/eps.ts b/src/cool/bootstrap/eps.ts index 695d8bb..531cc01 100644 --- a/src/cool/bootstrap/eps.ts +++ b/src/cool/bootstrap/eps.ts @@ -120,7 +120,10 @@ export function createEps(modules: Module[]) { // 监听 vite 触发事件 if (import.meta.hot) { - import.meta.hot.on("eps-update", () => { + import.meta.hot.on("eps-update", ({ service }) => { + if (service) { + eps.service = service; + } onUpdate(); }); } diff --git a/yarn.lock b/yarn.lock index ca127f5..839a7d4 100644 --- a/yarn.lock +++ b/yarn.lock @@ -3806,10 +3806,10 @@ vite-plugin-compression@^0.5.1: debug "^4.3.3" fs-extra "^10.0.0" -vite@^4.4.9: - version "4.4.9" - resolved "https://registry.yarnpkg.com/vite/-/vite-4.4.9.tgz#1402423f1a2f8d66fd8d15e351127c7236d29d3d" - integrity sha512-2mbUn2LlUmNASWwSCNSJ/EG2HuSRTnVNaydp6vMCm5VIqJsjMfbIWtbH2kDuwUVW5mMUKKZvGPX/rqeqVvv1XA== +vite@^4.5.0: + version "4.5.0" + resolved "https://registry.yarnpkg.com/vite/-/vite-4.5.0.tgz#ec406295b4167ac3bc23e26f9c8ff559287cff26" + integrity sha512-ulr8rNLA6rkyFAlVWw2q5YJ91v098AFQ2R0PRFwPzREXOUJQPtFUG0t+/ZikhaOCDqFoDhN6/v8Sq0o4araFAw== dependencies: esbuild "^0.18.10" postcss "^8.4.27"