plugins.ts 3.2 KB

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