import boxen from 'boxen'; import dayjs from 'dayjs'; import duration from 'dayjs/plugin/duration'; import gradient from 'gradient-string'; import { getPackageSize } from './utils'; import type { Options as BoxenOptions } from 'boxen'; import type { Dayjs } from 'dayjs'; import type { Plugin } from 'vite'; dayjs.extend(duration); const welcomeMessage = gradient(['cyan', 'magenta']).multiline(`正在打包...`); const boxenOptions: BoxenOptions = { padding: 0.5, borderColor: 'cyan', borderStyle: 'round', }; export function viteBuildInfo(): Plugin { 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() { if (config.command === 'build') { console.log('\n' + boxen(welcomeMessage, boxenOptions)); startTime = dayjs(new Date()); } }, closeBundle() { if (config.command === 'build') { endTime = dayjs(new Date()); getPackageSize({ folder: outDir, callback: (size: string | number) => { console.log( '\n' + boxen( gradient(['cyan', 'magenta']).multiline( `🎉 恭喜打包完成\n` + `总用时:${dayjs.duration(endTime.diff(startTime)).format('mm分ss秒')}\n` + `打包后的大小为:${size}` ), boxenOptions ) ); }, }); } }, }; }