plugins.ts 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123
  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 viteCompression from "vite-plugin-compression";
  10. import vueSetupExtend from "unplugin-vue-setup-extend-plus/vite";
  11. import NextDevTools from "vite-plugin-vue-devtools";
  12. import {codeInspectorPlugin} from "code-inspector-plugin";
  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, VITE_CODEINSPECTOR} = viteEnv;
  19. return [
  20. vue(),
  21. // vue 可以使用 jsx/tsx 语法
  22. vueJsx(),
  23. // devTools
  24. VITE_DEVTOOLS && NextDevTools({launchEditor: "code"}),
  25. // esLint 报错信息显示在浏览器界面上
  26. // name 可以写在 script 标签上
  27. vueSetupExtend({}),
  28. // 创建打包压缩配置
  29. createCompression(viteEnv),
  30. // 注入变量到 html 文件
  31. createHtmlPlugin({
  32. minify: true,
  33. inject: {
  34. data: {title: VITE_GLOB_APP_TITLE}
  35. }
  36. }),
  37. // 使用 svg 图标
  38. createSvgIconsPlugin({
  39. iconDirs: [resolve(process.cwd(), "src/assets/icons")],
  40. symbolId: "icon-[dir]-[name]"
  41. }),
  42. // vitePWA
  43. VITE_PWA && createVitePwa(viteEnv),
  44. // 是否生成包预览,分析依赖包大小做优化处理
  45. // VITE_REPORT && (visualizer({filename: "stats.html", gzipSize: true, brotliSize: true}) as PluginOption),
  46. // 自动 IDE 并将光标定位到 DOM 对应的源代码位置。see: https://inspector.fe-dev.cn/guide/start.html
  47. VITE_CODEINSPECTOR &&
  48. codeInspectorPlugin({
  49. bundler: "vite"
  50. })
  51. ];
  52. };
  53. /**
  54. * @description 根据 compress 配置,生成不同的压缩规则
  55. * @param viteEnv
  56. */
  57. const createCompression = (viteEnv: ViteEnv): PluginOption | PluginOption[] => {
  58. const {VITE_BUILD_COMPRESS = "none", VITE_BUILD_COMPRESS_DELETE_ORIGIN_FILE} = viteEnv;
  59. const compressList = VITE_BUILD_COMPRESS.split(",");
  60. const plugins: PluginOption[] = [];
  61. if (compressList.includes("gzip")) {
  62. plugins.push(
  63. viteCompression({
  64. ext: ".gz",
  65. algorithm: "gzip",
  66. deleteOriginFile: VITE_BUILD_COMPRESS_DELETE_ORIGIN_FILE
  67. })
  68. );
  69. }
  70. if (compressList.includes("brotli")) {
  71. plugins.push(
  72. viteCompression({
  73. ext: ".br",
  74. algorithm: "brotliCompress",
  75. deleteOriginFile: VITE_BUILD_COMPRESS_DELETE_ORIGIN_FILE
  76. })
  77. );
  78. }
  79. return plugins;
  80. };
  81. /**
  82. * @description VitePwa
  83. * @param viteEnv
  84. */
  85. const createVitePwa = (viteEnv: ViteEnv): PluginOption | PluginOption[] => {
  86. const {VITE_GLOB_APP_TITLE} = viteEnv;
  87. return VitePWA({
  88. strategies: 'generateSW', // 明确指定使用 generateSW
  89. registerType: 'autoUpdate',
  90. workbox: {
  91. maximumFileSizeToCacheInBytes: 10 * 1024 * 1024, // 提升缓存限制至10MB
  92. // 禁止缓存 CSV(包含 404)
  93. runtimeCaching: [
  94. {
  95. urlPattern: /\/admin\/api\/static\/.*\.csv$/,
  96. handler: "NetworkOnly",
  97. method: "GET",
  98. options: {
  99. cacheName: "no-cache-csv",
  100. expiration: { maxEntries: 0 }
  101. }
  102. }
  103. ]
  104. },
  105. manifest: {
  106. name: VITE_GLOB_APP_TITLE,
  107. short_name: VITE_GLOB_APP_TITLE,
  108. theme_color: "#ffffff",
  109. icons: [
  110. {
  111. src: "/logo.svg",
  112. sizes: "24x24",
  113. type: "image/svg+xml"
  114. },
  115. ]
  116. }
  117. });
  118. };