diff --git a/packages/vite-plugin/dist/eps/index.d.ts b/packages/vite-plugin/dist/eps/index.d.ts index 2bfb5f2..85e650c 100644 --- a/packages/vite-plugin/dist/eps/index.d.ts +++ b/packages/vite-plugin/dist/eps/index.d.ts @@ -4,4 +4,5 @@ export declare function createEps(query?: { }): Promise<{ service: {}; list: Eps.Entity[]; + isUpdate: boolean; }>; diff --git a/packages/vite-plugin/dist/index.js b/packages/vite-plugin/dist/index.js index 24db121..aaeba91 100644 --- a/packages/vite-plugin/dist/index.js +++ b/packages/vite-plugin/dist/index.js @@ -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, + }); + } }); } }, diff --git a/packages/vite-plugin/package.json b/packages/vite-plugin/package.json index 3242de1..5bcdb93 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.2", + "version": "7.1.3", "description": "cool-admin/cool-uni builder", "main": "/dist/index.js", "scripts": { diff --git a/packages/vite-plugin/src/ctx/index.ts b/packages/vite-plugin/src/ctx/index.ts index 1f95fbf..1bd6c18 100644 --- a/packages/vite-plugin/src/ctx/index.ts +++ b/packages/vite-plugin/src/ctx/index.ts @@ -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)); } diff --git a/packages/vite-plugin/src/eps/index.ts b/packages/vite-plugin/src/eps/index.ts index fbcfed2..44c00f4 100644 --- a/packages/vite-plugin/src/eps/index.ts +++ b/packages/vite-plugin/src/eps/index.ts @@ -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, }; } diff --git a/packages/vite-plugin/src/virtual.ts b/packages/vite-plugin/src/virtual.ts index 8957b8f..14603ac 100644 --- a/packages/vite-plugin/src/virtual.ts +++ b/packages/vite-plugin/src/virtual.ts @@ -33,14 +33,15 @@ export async function virtual(): Promise { ) ) { 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, + }); + } }); } },