chore: 优化build/info.ts文件中的一些函数,使其友好支持esm

This commit is contained in:
xiaoxian521 2023-10-22 15:53:32 +08:00
parent a9257d3085
commit 2a3fa34eac
2 changed files with 38 additions and 3 deletions

View File

@ -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(

34
build/utils.ts Normal file
View File

@ -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);
});
};