You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

57 lines
1.7 KiB

  1. import type { Plugin } from "vite";
  2. import { getPackageSize } from "./utils";
  3. import dayjs, { type Dayjs } from "dayjs";
  4. import duration from "dayjs/plugin/duration";
  5. import gradientString from "gradient-string";
  6. import boxen, { type Options as BoxenOptions } from "boxen";
  7. dayjs.extend(duration);
  8. const welcomeMessage = gradientString("cyan", "magenta").multiline(
  9. `您好! 欢迎使用 pure-admin 开源项目\n我们为您精心准备了下面两个贴心的保姆级文档\nhttps://yiming_chang.gitee.io/pure-admin-doc\nhttps://pure-admin-utils.netlify.app`
  10. );
  11. const boxenOptions: BoxenOptions = {
  12. padding: 0.5,
  13. borderColor: "cyan",
  14. borderStyle: "round"
  15. };
  16. export function viteBuildInfo(): Plugin {
  17. let config: { command: string };
  18. let startTime: Dayjs;
  19. let endTime: Dayjs;
  20. let outDir: string;
  21. return {
  22. name: "vite:buildInfo",
  23. configResolved(resolvedConfig) {
  24. config = resolvedConfig;
  25. outDir = resolvedConfig.build?.outDir ?? "dist";
  26. },
  27. buildStart() {
  28. console.log(boxen(welcomeMessage, boxenOptions));
  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. boxen(
  41. gradientString("cyan", "magenta").multiline(
  42. `🎉 恭喜打包完成(总用时${dayjs
  43. .duration(endTime.diff(startTime))
  44. .format("mm分ss秒")}${size}`
  45. ),
  46. boxenOptions
  47. )
  48. );
  49. }
  50. });
  51. }
  52. }
  53. };
  54. }