cool-admin-vue/build/cool/virtual.ts

70 lines
1.4 KiB
TypeScript
Raw Normal View History

2023-10-30 21:30:29 +08:00
import type { Plugin } from "vite";
import { createEps } from "./eps";
import { createModule } from "./module";
export function virtual(): Plugin {
const virtualModuleIds = ["virtual:eps", "virtual:module"];
2023-11-24 15:08:24 +08:00
// 首次启动加载 Eps
createEps();
return {
name: "vite-cool-virtual",
enforce: "pre",
configureServer(server) {
server.middlewares.use(async (req, res, next) => {
2023-10-30 21:30:29 +08:00
// 页面刷新时触发
if (req.url == "/@vite/client") {
2023-10-30 21:30:29 +08:00
// 重新加载虚拟模块
virtualModuleIds.forEach((vm) => {
const mod = server.moduleGraph.getModuleById(`\0${vm}`);
if (mod) {
server.moduleGraph.invalidateModule(mod);
}
});
}
next();
});
},
2023-10-30 21:30:29 +08:00
async handleHotUpdate({ file, server }) {
// 代码保存时触发
if (!file.includes("build/cool/dist")) {
2023-10-30 21:30:29 +08:00
const { service } = await createEps();
// 通知客户端刷新
server.ws.send({
type: "custom",
2023-10-30 21:30:29 +08:00
event: "eps-update",
data: {
service
}
});
}
},
resolveId(id) {
if (virtualModuleIds.includes(id)) {
return "\0" + id;
}
},
async load(id) {
if (id === "\0virtual:eps") {
2023-10-30 21:30:29 +08:00
const { service } = await createEps();
return `
2023-10-30 21:30:29 +08:00
export const eps = ${JSON.stringify({ service })}
`;
}
if (id === "\0virtual:module") {
const { dirs } = createModule();
return `
export const dirs = ${JSON.stringify(dirs)}
`;
}
}
};
}