储能智慧云平台web端
Du kannst nicht mehr als 25 Themen auswählen Themen müssen mit entweder einem Buchstaben oder einer Ziffer beginnen. Sie können Bindestriche („-“) enthalten und bis zu 35 Zeichen lang sein.

plugins.ts 3.1KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110
  1. import { resolve } from "path";
  2. import { PluginOption } from "vite";
  3. import { VitePWA } from "vite-plugin-pwa";
  4. import { visualizer } from "rollup-plugin-visualizer";
  5. import simpleHtmlPlugin from "vite-plugin-simple-html";
  6. import { createSvgIconsPlugin } from "vite-plugin-svg-icons";
  7. import vue from "@vitejs/plugin-vue";
  8. import vueJsx from "@vitejs/plugin-vue-jsx";
  9. import eslintPlugin from "vite-plugin-eslint";
  10. import viteCompression from "vite-plugin-compression";
  11. import vueSetupExtend from "unplugin-vue-setup-extend-plus/vite";
  12. /**
  13. * 创建 vite 插件
  14. * @param viteEnv
  15. */
  16. export const createVitePlugins = (viteEnv: ViteEnv): (PluginOption | PluginOption[])[] => {
  17. const { VITE_GLOB_APP_TITLE, VITE_REPORT, VITE_PWA } = viteEnv;
  18. return [
  19. vue(),
  20. // vue 可以使用 jsx/tsx 语法
  21. vueJsx(),
  22. // esLint 报错信息显示在浏览器界面上
  23. eslintPlugin(),
  24. // name 可以写在 script 标签上
  25. vueSetupExtend({}),
  26. // 创建打包压缩配置
  27. createCompression(viteEnv),
  28. // 注入变量到 html 文件
  29. simpleHtmlPlugin({
  30. minify: true,
  31. inject: {
  32. data: { title: VITE_GLOB_APP_TITLE }
  33. }
  34. }),
  35. // 使用 svg 图标
  36. createSvgIconsPlugin({
  37. iconDirs: [resolve(process.cwd(), "src/assets/icons")],
  38. symbolId: "icon-[dir]-[name]"
  39. }),
  40. // vitePWA
  41. VITE_PWA && createVitePwa(viteEnv),
  42. // 是否生成包预览,分析依赖包大小做优化处理
  43. VITE_REPORT && (visualizer({ filename: "stats.html", gzipSize: true, brotliSize: true }) as PluginOption)
  44. ];
  45. };
  46. /**
  47. * @description 根据 compress 配置,生成不同的压缩规则
  48. * @param viteEnv
  49. */
  50. const createCompression = (viteEnv: ViteEnv): PluginOption | PluginOption[] => {
  51. const { VITE_BUILD_COMPRESS = "none", VITE_BUILD_COMPRESS_DELETE_ORIGIN_FILE } = viteEnv;
  52. const compressList = VITE_BUILD_COMPRESS.split(",");
  53. const plugins: PluginOption[] = [];
  54. if (compressList.includes("gzip")) {
  55. plugins.push(
  56. viteCompression({
  57. ext: ".gz",
  58. algorithm: "gzip",
  59. deleteOriginFile: VITE_BUILD_COMPRESS_DELETE_ORIGIN_FILE
  60. })
  61. );
  62. }
  63. if (compressList.includes("brotli")) {
  64. plugins.push(
  65. viteCompression({
  66. ext: ".br",
  67. algorithm: "brotliCompress",
  68. deleteOriginFile: VITE_BUILD_COMPRESS_DELETE_ORIGIN_FILE
  69. })
  70. );
  71. }
  72. return plugins;
  73. };
  74. /**
  75. * @description VitePwa
  76. * @param viteEnv
  77. */
  78. const createVitePwa = (viteEnv: ViteEnv): PluginOption | PluginOption[] => {
  79. const { VITE_GLOB_APP_TITLE } = viteEnv;
  80. return VitePWA({
  81. registerType: "autoUpdate",
  82. manifest: {
  83. name: VITE_GLOB_APP_TITLE,
  84. short_name: VITE_GLOB_APP_TITLE,
  85. theme_color: "#ffffff",
  86. icons: [
  87. {
  88. src: "/logo.png",
  89. sizes: "192x192",
  90. type: "image/png"
  91. },
  92. {
  93. src: "/logo.png",
  94. sizes: "512x512",
  95. type: "image/png"
  96. },
  97. {
  98. src: "/logo.png",
  99. sizes: "512x512",
  100. type: "image/png",
  101. purpose: "any maskable"
  102. }
  103. ]
  104. }
  105. });
  106. };