plugins.ts 3.7 KB

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