financial-web/build/info.ts

61 lines
1.5 KiB
TypeScript
Raw Permalink Normal View History

2024-12-20 15:02:51 +08:00
import type { Plugin } from 'vite';
import { getPackageSize } from './utils';
import dayjs, { type Dayjs } from 'dayjs';
import duration from 'dayjs/plugin/duration';
import gradientString from 'gradient-string';
import boxen, { type Options as BoxenOptions } from 'boxen';
2024-11-11 12:04:24 +08:00
dayjs.extend(duration);
const welcomeMessage = (VITE_PORT: number) => {
2024-12-20 15:02:51 +08:00
return gradientString('cyan', 'magenta').multiline(
`您好! 欢迎使用 bunny 系列开发模板
2024-11-11 12:04:24 +08:00
访
2024-12-20 15:02:51 +08:00
http://localhost:${VITE_PORT}`,
);
2024-11-11 12:04:24 +08:00
};
const boxenOptions: BoxenOptions = {
2024-12-20 15:02:51 +08:00
padding: 0.5,
borderColor: 'cyan',
borderStyle: 'round',
2024-11-11 12:04:24 +08:00
};
export function viteBuildInfo(VITE_PORT: number): Plugin {
2024-12-20 15:02:51 +08:00
let config: { command: string };
let startTime: Dayjs;
let endTime: Dayjs;
let outDir: string;
return {
name: 'vite:buildInfo',
configResolved(resolvedConfig) {
config = resolvedConfig;
outDir = resolvedConfig.build?.outDir ?? 'dist';
},
buildStart() {
console.log(boxen(welcomeMessage(VITE_PORT), boxenOptions));
if (config.command === 'build') {
startTime = dayjs(new Date());
}
},
closeBundle() {
if (config.command === 'build') {
endTime = dayjs(new Date());
getPackageSize({
folder: outDir,
callback: (size: string) => {
console.log(
boxen(
gradientString('cyan', 'magenta').multiline(
`🎉 恭喜打包完成(总用时${dayjs.duration(endTime.diff(startTime)).format('mm分ss秒')},打包后的大小为${size}`,
),
boxenOptions,
),
);
},
});
}
},
};
2024-11-11 12:04:24 +08:00
}