index.ts 2.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475
  1. import { PluginOption } 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 VitePluginCertificate from 'vite-plugin-mkcert';
  8. import vueSetupExtend from 'vite-plugin-vue-setup-extend';
  9. import { configHtmlPlugin } from './html';
  10. import { configPwaConfig } from './pwa';
  11. import { configMockPlugin } from './mock';
  12. import { configCompressPlugin } from './compress';
  13. import { configStyleImportPlugin } from './styleImport';
  14. import { configVisualizerConfig } from './visualizer';
  15. import { configThemePlugin } from './theme';
  16. import { configImageminPlugin } from './imagemin';
  17. import { configSvgIconsPlugin } from './svgSprite';
  18. export function createVitePlugins(viteEnv: ViteEnv, isBuild: boolean) {
  19. const { VITE_USE_IMAGEMIN, VITE_USE_MOCK, VITE_LEGACY, VITE_BUILD_COMPRESS, VITE_BUILD_COMPRESS_DELETE_ORIGIN_FILE } = viteEnv;
  20. const vitePlugins: (PluginOption | PluginOption[])[] = [
  21. // have to
  22. vue(),
  23. // have to
  24. vueJsx(),
  25. // support name
  26. vueSetupExtend(),
  27. // @ts-ignore
  28. VitePluginCertificate({
  29. source: 'coding',
  30. }),
  31. ];
  32. // vite-plugin-windicss
  33. vitePlugins.push(windiCSS());
  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. return vitePlugins;
  60. }