diff --git a/build/info.ts b/build/info.ts index 7ef3f11f3..06006411d 100644 --- a/build/info.ts +++ b/build/info.ts @@ -1,8 +1,8 @@ import type { Plugin } from "vite"; +import picocolors from "picocolors"; import dayjs, { Dayjs } from "dayjs"; -import utils from "@pureadmin/utils"; +import { getPackageSize } from "./utils"; import duration from "dayjs/plugin/duration"; -import { green, blue, bold } from "picocolors"; dayjs.extend(duration); export function viteBuildInfo(): Plugin { @@ -10,6 +10,7 @@ export function viteBuildInfo(): Plugin { let startTime: Dayjs; let endTime: Dayjs; let outDir: string; + const { green, blue, bold } = picocolors; return { name: "vite:buildInfo", configResolved(resolvedConfig) { @@ -33,7 +34,7 @@ export function viteBuildInfo(): Plugin { closeBundle() { if (config.command === "build") { endTime = dayjs(new Date()); - utils.getPackageSize({ + getPackageSize({ folder: outDir, callback: (size: string) => { console.log( diff --git a/build/utils.ts b/build/utils.ts new file mode 100644 index 000000000..2f11e0f15 --- /dev/null +++ b/build/utils.ts @@ -0,0 +1,34 @@ +import { readdir, stat } from "node:fs"; +import { sum, formatBytes } from "@pureadmin/utils"; + +const fileListTotal: number[] = []; + +/** + * @description 获取指定文件夹中所有文件的总大小 + */ +export const getPackageSize = options => { + const { folder = "dist", callback, format = true } = options; + readdir(folder, (err, files: string[]) => { + if (err) throw err; + let count = 0; + const checkEnd = () => { + ++count == files.length && + callback(format ? formatBytes(sum(fileListTotal)) : sum(fileListTotal)); + }; + files.forEach((item: string) => { + stat(`${folder}/${item}`, async (err, stats) => { + if (err) throw err; + if (stats.isFile()) { + fileListTotal.push(stats.size); + checkEnd(); + } else if (stats.isDirectory()) { + getPackageSize({ + folder: `${folder}/${item}/`, + callback: checkEnd + }); + } + }); + }); + files.length === 0 && callback(0); + }); +};