mirror of
https://gitee.com/yiming_chang/vue-pure-admin.git
synced 2024-11-30 02:07:38 +08:00
chore: 优化build/info.ts
文件中的一些函数,使其友好支持esm
This commit is contained in:
parent
a9257d3085
commit
2a3fa34eac
@ -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
34
build/utils.ts
Normal 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);
|
||||
});
|
||||
};
|
Loading…
Reference in New Issue
Block a user