info.ts 1.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051
  1. import type { Plugin } from "vite";
  2. import dayjs, { Dayjs } from "dayjs";
  3. import utils from "@pureadmin/utils";
  4. import duration from "dayjs/plugin/duration";
  5. import { green, blue, bold } from "picocolors";
  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(
  20. bold(
  21. green(
  22. `👏开始打包`
  23. )
  24. )
  25. );
  26. if (config.command === "build") {
  27. startTime = dayjs(new Date());
  28. }
  29. },
  30. closeBundle() {
  31. if (config.command === "build") {
  32. endTime = dayjs(new Date());
  33. utils.getPackageSize({
  34. folder: outDir,
  35. callback: (size: string) => {
  36. console.log(
  37. bold(
  38. green(
  39. `🎉恭喜打包完成(总用时${dayjs
  40. .duration(endTime.diff(startTime))
  41. .format("mm分ss秒")},打包后的大小为${size})`
  42. )
  43. )
  44. );
  45. }
  46. });
  47. }
  48. }
  49. };
  50. }