解决 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<{ }): Promise<{
service: {}; service: {};
list: Eps.Entity[]; list: Eps.Entity[];
isUpdate: boolean;
}>; }>;

View File

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

View File

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

View File

@ -1,7 +1,7 @@
import { join } from "path"; import { join } from "path";
import { readFile, rootDir, writeFile } from "../utils"; import { readFile, rootDir, writeFile } from "../utils";
import { glob } from "glob"; import { glob } from "glob";
import { assign, cloneDeep, isEqual } from "lodash"; import { assign, cloneDeep, isEqual, orderBy } from "lodash";
import type { Ctx } from "../../types"; import type { Ctx } from "../../types";
import { config } from "../config"; import { config } from "../config";
import fs from "fs"; 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 // 是否需要更新 pages.json
if (!isEqual(ctxData, ctx)) { if (!isEqual(order(ctxData), order(ctx))) {
console.log("[cool-ctx] pages updated"); console.log("[cool-ctx] pages updated");
writeFile(ctxPath, JSON.stringify(ctx, null, 4)); writeFile(ctxPath, JSON.stringify(ctx, null, 4));
} }

View File

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

View File

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