info.ts 1.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354
  1. import type { Plugin } from "vite";
  2. import picocolors from "picocolors";
  3. import { getPackageSize } from "./utils";
  4. import dayjs, { type Dayjs } from "dayjs";
  5. import duration from "dayjs/plugin/duration";
  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. const { green, blue, bold } = picocolors;
  13. return {
  14. name: "vite:buildInfo",
  15. configResolved(resolvedConfig) {
  16. config = resolvedConfig;
  17. outDir = resolvedConfig.build?.outDir ?? "dist";
  18. },
  19. buildStart() {
  20. console.log(
  21. bold(
  22. green(
  23. `👏欢迎使用${blue(
  24. "[vue-pure-admin]"
  25. )},如果您感觉不错,记得点击后面链接给个star哦💖 https://github.com/pure-admin/vue-pure-admin`
  26. )
  27. )
  28. );
  29. if (config.command === "build") {
  30. startTime = dayjs(new Date());
  31. }
  32. },
  33. closeBundle() {
  34. if (config.command === "build") {
  35. endTime = dayjs(new Date());
  36. getPackageSize({
  37. folder: outDir,
  38. callback: (size: string) => {
  39. console.log(
  40. bold(
  41. green(
  42. `🎉恭喜打包完成(总用时${dayjs
  43. .duration(endTime.diff(startTime))
  44. .format("mm分ss秒")},打包后的大小为${size})`
  45. )
  46. )
  47. );
  48. }
  49. });
  50. }
  51. }
  52. };
  53. }