index.ts 2.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576
  1. import { PluginOption } from 'vite';
  2. import vue from '@vitejs/plugin-vue';
  3. import vueJsx from '@vitejs/plugin-vue-jsx';
  4. import progress from 'vite-plugin-progress';
  5. import windiCSS from 'vite-plugin-windicss';
  6. import purgeIcons from 'vite-plugin-purge-icons';
  7. import VitePluginCertificate from 'vite-plugin-mkcert';
  8. import vueSetupExtend from 'unplugin-vue-setup-extend-plus/vite';
  9. import { configPwaConfig } from './pwa';
  10. import { configMockPlugin } from './mock';
  11. import { configHtmlPlugin } from './html';
  12. import { configCompressPlugin } from './compress';
  13. import { configStyleImportPlugin } from './styleImport';
  14. import { configVisualizerConfig } from './visualizer';
  15. import { configThemePlugin } from './theme';
  16. import { configSvgIconsPlugin } from './svgSprite';
  17. import topLevelAwait from 'vite-plugin-top-level-await';
  18. export function createVitePlugins(viteEnv: ViteEnv, isBuild: boolean) {
  19. const { VITE_USE_MOCK, 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. // 打包进度条
  26. progress(),
  27. // support name
  28. vueSetupExtend({}),
  29. VitePluginCertificate({
  30. source: 'coding',
  31. }),
  32. ];
  33. vitePlugins.push(
  34. topLevelAwait({
  35. promiseExportName: '__tla',
  36. promiseImportName: (i) => `__tla_${i}`,
  37. }),
  38. );
  39. // windiCSS
  40. vitePlugins.push(windiCSS());
  41. // vite-plugin-html
  42. vitePlugins.push(configHtmlPlugin(viteEnv, isBuild));
  43. // vite-plugin-svg-icons
  44. vitePlugins.push(configSvgIconsPlugin(isBuild));
  45. // vite-plugin-mock
  46. VITE_USE_MOCK && vitePlugins.push(configMockPlugin(isBuild));
  47. // vite-plugin-purge-icons
  48. vitePlugins.push(purgeIcons());
  49. // rollup-plugin-visualizer
  50. vitePlugins.push(configVisualizerConfig());
  51. // vite-plugin-vben-theme
  52. vitePlugins.push(configThemePlugin(isBuild));
  53. // The following plugins only work in the production environment
  54. if (isBuild) {
  55. // vite-vue-plugin-style-import
  56. vitePlugins.push(configStyleImportPlugin(isBuild));
  57. // rollup-plugin-gzip
  58. vitePlugins.push(
  59. configCompressPlugin(VITE_BUILD_COMPRESS, VITE_BUILD_COMPRESS_DELETE_ORIGIN_FILE),
  60. );
  61. // vite-plugin-pwa
  62. vitePlugins.push(configPwaConfig(viteEnv));
  63. }
  64. return vitePlugins;
  65. }