plugins.ts 3.5 KB

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