index.ts 2.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081
  1. import type { Plugin } from 'vite';
  2. import vue from '@vitejs/plugin-vue';
  3. import vueJsx from '@vitejs/plugin-vue-jsx';
  4. import legacy from '@vitejs/plugin-legacy';
  5. import purgeIcons from 'vite-plugin-purge-icons';
  6. import windiCSS from 'vite-plugin-windicss';
  7. import { configHtmlPlugin } from './html';
  8. import { configPwaConfig } from './pwa';
  9. import { configMockPlugin } from './mock';
  10. import { configCompressPlugin } from './compress';
  11. import { configStyleImportPlugin } from './styleImport';
  12. import { configVisualizerConfig } from './visualizer';
  13. import { configThemePlugin } from './theme';
  14. import { configImageminPlugin } from './imagemin';
  15. import { configSvgIconsPlugin } from './svgSprite';
  16. import { configHmrPlugin } from './hmr';
  17. export function createVitePlugins(viteEnv: ViteEnv, isBuild: boolean) {
  18. const {
  19. VITE_GLOB_USE_IMAGEMIN,
  20. VITE_GLOB_USE_MOCK,
  21. VITE_LEGACY,
  22. VITE_GLOB_BUILD_COMPRESS,
  23. VITE_GLOB_BUILD_COMPRESS_DELETE_ORIGIN_FILE,
  24. } = viteEnv;
  25. const vitePlugins: (Plugin | Plugin[])[] = [
  26. // have to
  27. vue(),
  28. // have to
  29. vueJsx(),
  30. ];
  31. // vite-plugin-windicss
  32. vitePlugins.push(windiCSS());
  33. // TODO
  34. !isBuild && vitePlugins.push(configHmrPlugin());
  35. // @vitejs/plugin-legacy
  36. VITE_LEGACY && isBuild && vitePlugins.push(legacy());
  37. // vite-plugin-html
  38. vitePlugins.push(configHtmlPlugin(viteEnv, isBuild));
  39. // vite-plugin-svg-icons
  40. vitePlugins.push(configSvgIconsPlugin(isBuild));
  41. // vite-plugin-mock
  42. VITE_GLOB_USE_MOCK && vitePlugins.push(configMockPlugin(isBuild));
  43. // vite-plugin-purge-icons
  44. vitePlugins.push(purgeIcons());
  45. // vite-plugin-style-import
  46. vitePlugins.push(configStyleImportPlugin(isBuild));
  47. // rollup-plugin-visualizer
  48. vitePlugins.push(configVisualizerConfig());
  49. //vite-plugin-theme
  50. vitePlugins.push(configThemePlugin(isBuild));
  51. // The following plugins only work in the production environment
  52. if (isBuild) {
  53. //vite-plugin-imagemin
  54. VITE_GLOB_USE_IMAGEMIN && vitePlugins.push(configImageminPlugin());
  55. // rollup-plugin-gzip
  56. vitePlugins.push(
  57. configCompressPlugin(VITE_GLOB_BUILD_COMPRESS, VITE_GLOB_BUILD_COMPRESS_DELETE_ORIGIN_FILE)
  58. );
  59. // vite-plugin-pwa
  60. vitePlugins.push(configPwaConfig(viteEnv));
  61. }
  62. return vitePlugins;
  63. }