auth-web/build/info.ts

58 lines
1.7 KiB
TypeScript
Raw Normal View History

2025-04-29 23:21:40 +08:00
import boxen, { type Options as BoxenOptions } from 'boxen';
import dayjs, { type Dayjs } from 'dayjs';
import duration from 'dayjs/plugin/duration';
import gradient from 'gradient-string';
import type { Plugin } from 'vite';
import { getPackageSize } from './utils';
2024-09-26 09:38:02 +08:00
dayjs.extend(duration);
2025-04-29 23:21:40 +08:00
const welcomeMessage = (VITE_PORT: number) =>
gradient(['cyan', 'magenta']).multiline(
`您好! 欢迎使用 bunny 系列开发模板项目访问地址如下:\nhttp://localhost:${VITE_PORT}
pure-admin \nhttps://pure-admin.cn`
);
2024-09-26 09:38:02 +08:00
const boxenOptions: BoxenOptions = {
2025-04-25 16:21:15 +08:00
padding: 0.5,
2025-04-29 23:21:40 +08:00
borderColor: 'cyan',
borderStyle: 'round',
2024-09-26 09:38:02 +08:00
};
2025-04-29 23:21:40 +08:00
export function viteBuildInfo(VITE_PORT: number): Plugin {
2025-04-25 16:21:15 +08:00
let config: { command: string };
let startTime: Dayjs;
let endTime: Dayjs;
let outDir: string;
return {
2025-04-29 23:21:40 +08:00
name: 'vite:buildInfo',
2025-04-25 16:21:15 +08:00
configResolved(resolvedConfig) {
config = resolvedConfig;
2025-04-29 23:21:40 +08:00
outDir = resolvedConfig.build?.outDir ?? 'dist';
2025-04-25 16:21:15 +08:00
},
buildStart() {
2025-04-29 23:21:40 +08:00
console.log(boxen(welcomeMessage(VITE_PORT), boxenOptions));
if (config.command === 'build') {
2025-04-25 16:21:15 +08:00
startTime = dayjs(new Date());
}
},
closeBundle() {
2025-04-29 23:21:40 +08:00
if (config.command === 'build') {
2025-04-25 16:21:15 +08:00
endTime = dayjs(new Date());
getPackageSize({
folder: outDir,
callback: (size: string) => {
console.log(
boxen(
2025-04-29 23:21:40 +08:00
gradient(['cyan', 'magenta']).multiline(
`🎉 恭喜打包完成(总用时${dayjs.duration(endTime.diff(startTime)).format('mm分ss秒')},打包后的大小为${size}`
2025-04-25 16:21:15 +08:00
),
boxenOptions
)
);
2025-04-29 23:21:40 +08:00
},
2025-04-25 16:21:15 +08:00
});
}
2025-04-29 23:21:40 +08:00
},
2025-04-25 16:21:15 +08:00
};
2024-09-26 09:38:02 +08:00
}