info.ts 1.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445
  1. import type { Plugin } from "vite";
  2. import dayjs, { Dayjs } from "dayjs";
  3. import duration from "dayjs/plugin/duration";
  4. import { green, blue, bold } from "picocolors";
  5. import { getPackageSize } from "@pureadmin/utils";
  6. dayjs.extend(duration);
  7. export function viteBuildInfo(): Plugin {
  8. let config: { command: string };
  9. let startTime: Dayjs;
  10. let endTime: Dayjs;
  11. let outDir: string;
  12. return {
  13. name: "vite:buildInfo",
  14. configResolved(resolvedConfig) {
  15. config = resolvedConfig;
  16. outDir = resolvedConfig.build?.outDir ?? "dist";
  17. },
  18. buildStart() {
  19. console.log(bold(green(`👏欢迎使用${blue("[tel-admin]")}`)));
  20. if (config.command === "build") {
  21. startTime = dayjs(new Date());
  22. }
  23. },
  24. closeBundle() {
  25. if (config.command === "build") {
  26. endTime = dayjs(new Date());
  27. getPackageSize({
  28. folder: outDir,
  29. callback: (size: string) => {
  30. console.log(
  31. bold(
  32. green(
  33. `🎉恭喜打包完成(总用时${dayjs
  34. .duration(endTime.diff(startTime))
  35. .format("mm分ss秒")},打包后的大小为${size})`
  36. )
  37. )
  38. );
  39. }
  40. });
  41. }
  42. }
  43. };
  44. }