Skip to content

Commit

Permalink
chore: 优化build/info.ts文件中的一些函数,使其友好支持esm
Browse files Browse the repository at this point in the history
  • Loading branch information
xiaoxian521 committed Oct 22, 2023
1 parent a9257d3 commit 2a3fa34
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 3 deletions.
7 changes: 4 additions & 3 deletions build/info.ts
Original file line number Diff line number Diff line change
@@ -1,15 +1,16 @@
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 {
let config: { command: string };
let startTime: Dayjs;
let endTime: Dayjs;
let outDir: string;
const { green, blue, bold } = picocolors;
return {
name: "vite:buildInfo",
configResolved(resolvedConfig) {
Expand All @@ -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(
Expand Down
34 changes: 34 additions & 0 deletions build/utils.ts
Original file line number Diff line number Diff line change
@@ -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);
});
};

1 comment on commit 2a3fa34

@xiaoxian521
Copy link
Member Author

@xiaoxian521 xiaoxian521 commented on 2a3fa34 Oct 22, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

为后续升级vite5以及vite.config.ts文件名改为vite.config.mts做准备
#738 (comment)

Please sign in to comment.