解决 vite-plugin 重新刷新问题

This commit is contained in:
神仙都没用 2024-05-17 14:03:57 +08:00
parent d17e1a960b
commit d5e918c0fc
6 changed files with 82 additions and 35 deletions

View File

@ -4,4 +4,5 @@ export declare function createEps(query?: {
}): Promise<{
service: {};
list: Eps.Entity[];
isUpdate: boolean;
}>;

View File

@ -192,7 +192,7 @@
}
// 创建 json 文件
function createJson() {
const d = list.map((e) => {
const arr = list.map((e) => {
return {
prefix: e.prefix,
name: e.name || "",
@ -205,9 +205,16 @@
}),
};
});
fs.createWriteStream(getEpsPath("eps.json"), {
flags: "w",
}).write(JSON.stringify(d));
const content = JSON.stringify(arr);
const local_content = readFile(getEpsPath("eps.json"));
// 是否需要更新
const isUpdate = content != local_content;
if (isUpdate) {
fs.createWriteStream(getEpsPath("eps.json"), {
flags: "w",
}).write(content);
}
return isUpdate;
}
// 创建描述文件
async function createDescribe({ list, service }) {
@ -411,10 +418,14 @@
printWidth: 100,
trailingComma: "none",
});
// 创建 eps 描述文件
fs.createWriteStream(getEpsPath("eps.d.ts"), {
flags: "w",
}).write(content);
const local_content = readFile(getEpsPath("eps.d.ts"));
// 是否需要更新
if (content != local_content) {
// 创建 eps 描述文件
fs.createWriteStream(getEpsPath("eps.d.ts"), {
flags: "w",
}).write(content);
}
}
// 创建 service
function createService() {
@ -473,12 +484,13 @@
// 创建目录
createDir(getEpsPath(), true);
// 创建 json 文件
createJson();
const isUpdate = createJson();
// 创建描述文件
createDescribe({ service, list });
return {
service,
list,
isUpdate,
};
}
@ -707,8 +719,15 @@
});
}
}
// 排序后检测,避免加载顺序问题
function order(d) {
return {
pages: lodash.orderBy(d.pages, "path"),
subPackages: lodash.orderBy(d.subPackages, "root"),
};
}
// 是否需要更新 pages.json
if (!lodash.isEqual(ctxData, ctx)) {
if (!lodash.isEqual(order(ctxData), order(ctx))) {
console.log("[cool-ctx] pages updated");
writeFile(ctxPath, JSON.stringify(ctx, null, 4));
}
@ -747,12 +766,14 @@
if (!["pages.json", "dist", "build/cool", "eps.json", "eps.d.ts"].some((e) => file.includes(e))) {
createCtx();
createEps().then((data) => {
// 通知客户端刷新
(server.hot || server.ws).send({
type: "custom",
event: "eps-update",
data,
});
if (data.isUpdate) {
// 通知客户端刷新
(server.hot || server.ws).send({
type: "custom",
event: "eps-update",
data,
});
}
});
}
},

View File

@ -1,6 +1,6 @@
{
"name": "@cool-vue/vite-plugin",
"version": "7.1.2",
"version": "7.1.3",
"description": "cool-admin/cool-uni builder",
"main": "/dist/index.js",
"scripts": {

View File

@ -1,7 +1,7 @@
import { join } from "path";
import { readFile, rootDir, writeFile } from "../utils";
import { glob } from "glob";
import { assign, cloneDeep, isEqual } from "lodash";
import { assign, cloneDeep, isEqual, orderBy } from "lodash";
import type { Ctx } from "../../types";
import { config } from "../config";
import fs from "fs";
@ -61,8 +61,16 @@ export async function createCtx() {
}
}
// 排序后检测,避免加载顺序问题
function order(d: Ctx.Data) {
return {
pages: orderBy(d.pages, "path"),
subPackages: orderBy(d.subPackages, "root"),
};
}
// 是否需要更新 pages.json
if (!isEqual(ctxData, ctx)) {
if (!isEqual(order(ctxData), order(ctx))) {
console.log("[cool-ctx] pages updated");
writeFile(ctxPath, JSON.stringify(ctx, null, 4));
}

View File

@ -105,7 +105,7 @@ async function getData(data?: Eps.Entity[]) {
// 创建 json 文件
function createJson() {
const d = list.map((e) => {
const arr = list.map((e) => {
return {
prefix: e.prefix,
name: e.name || "",
@ -119,9 +119,19 @@ function createJson() {
};
});
createWriteStream(getEpsPath("eps.json"), {
flags: "w",
}).write(JSON.stringify(d));
const content = JSON.stringify(arr);
const local_content = readFile(getEpsPath("eps.json"));
// 是否需要更新
const isUpdate = content != local_content;
if (isUpdate) {
createWriteStream(getEpsPath("eps.json"), {
flags: "w",
}).write(content);
}
return isUpdate;
}
// 创建描述文件
@ -372,10 +382,15 @@ async function createDescribe({ list, service }: { list: Eps.Entity[]; service:
trailingComma: "none",
});
// 创建 eps 描述文件
createWriteStream(getEpsPath("eps.d.ts"), {
flags: "w",
}).write(content);
const local_content = readFile(getEpsPath("eps.d.ts"));
// 是否需要更新
if (content != local_content) {
// 创建 eps 描述文件
createWriteStream(getEpsPath("eps.d.ts"), {
flags: "w",
}).write(content);
}
}
// 创建 service
@ -448,7 +463,7 @@ export async function createEps(query?: { list: any[] }) {
createDir(getEpsPath(), true);
// 创建 json 文件
createJson();
const isUpdate = createJson();
// 创建描述文件
createDescribe({ service, list });
@ -456,5 +471,6 @@ export async function createEps(query?: { list: any[] }) {
return {
service,
list,
isUpdate,
};
}

View File

@ -33,14 +33,15 @@ export async function virtual(): Promise<Plugin> {
)
) {
createCtx();
createEps().then((data) => {
// 通知客户端刷新
(server.hot || server.ws).send({
type: "custom",
event: "eps-update",
data,
});
if (data.isUpdate) {
// 通知客户端刷新
(server.hot || server.ws).send({
type: "custom",
event: "eps-update",
data,
});
}
});
}
},