index.ts 2.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980
  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 vueSetupExtend from 'vite-plugin-vue-setup-extend';
  8. import { configHtmlPlugin } from './html';
  9. import { configPwaConfig } from './pwa';
  10. import { configMockPlugin } from './mock';
  11. import { configCompressPlugin } from './compress';
  12. import { configStyleImportPlugin } from './styleImport';
  13. import { configVisualizerConfig } from './visualizer';
  14. import { configThemePlugin } from './theme';
  15. import { configImageminPlugin } from './imagemin';
  16. import { configSvgIconsPlugin } from './svgSprite';
  17. import { configHmrPlugin } from './hmr';
  18. import OptimizationPersist from 'vite-plugin-optimize-persist';
  19. import PkgConfig from 'vite-plugin-package-config';
  20. export function createVitePlugins(viteEnv: ViteEnv, isBuild: boolean) {
  21. const { VITE_USE_IMAGEMIN, VITE_USE_MOCK, VITE_LEGACY, VITE_BUILD_COMPRESS, VITE_BUILD_COMPRESS_DELETE_ORIGIN_FILE } = viteEnv;
  22. const vitePlugins: (Plugin | Plugin[])[] = [
  23. // have to
  24. vue(),
  25. // have to
  26. vueJsx(),
  27. // support name
  28. vueSetupExtend(),
  29. ];
  30. // vite-plugin-windicss
  31. vitePlugins.push(windiCSS());
  32. // TODO
  33. !isBuild && vitePlugins.push(configHmrPlugin());
  34. // @vitejs/plugin-legacy
  35. VITE_LEGACY && isBuild && vitePlugins.push(legacy());
  36. // vite-plugin-html
  37. vitePlugins.push(configHtmlPlugin(viteEnv, isBuild));
  38. // vite-plugin-svg-icons
  39. vitePlugins.push(configSvgIconsPlugin(isBuild));
  40. // vite-plugin-mock
  41. VITE_USE_MOCK && vitePlugins.push(configMockPlugin(isBuild));
  42. // vite-plugin-purge-icons
  43. vitePlugins.push(purgeIcons());
  44. // vite-plugin-style-import
  45. vitePlugins.push(configStyleImportPlugin(isBuild));
  46. // rollup-plugin-visualizer
  47. vitePlugins.push(configVisualizerConfig());
  48. //vite-plugin-theme
  49. vitePlugins.push(configThemePlugin(isBuild));
  50. // The following plugins only work in the production environment
  51. if (isBuild) {
  52. //vite-plugin-imagemin
  53. VITE_USE_IMAGEMIN && vitePlugins.push(configImageminPlugin());
  54. // rollup-plugin-gzip
  55. vitePlugins.push(configCompressPlugin(VITE_BUILD_COMPRESS, VITE_BUILD_COMPRESS_DELETE_ORIGIN_FILE));
  56. // vite-plugin-pwa
  57. vitePlugins.push(configPwaConfig(viteEnv));
  58. }
  59. //vite-plugin-theme【解决vite首次打开界面加载慢问题】
  60. vitePlugins.push(PkgConfig());
  61. vitePlugins.push(OptimizationPersist());
  62. return vitePlugins;
  63. }