index.ts 3.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192
  1. import { PluginOption } from 'vite';
  2. import vue from '@vitejs/plugin-vue';
  3. import vueJsx from '@vitejs/plugin-vue-jsx';
  4. import purgeIcons from 'vite-plugin-purge-icons';
  5. import UnoCSS from 'unocss/vite';
  6. import { presetTypography, presetUno } from 'unocss';
  7. import VitePluginCertificate from 'vite-plugin-mkcert';
  8. //[issues/555]开发环境,vscode断点调试,文件或行数对不上
  9. import vueSetupExtend from 'vite-plugin-vue-setup-extend-plus';
  10. import { configHtmlPlugin } from './html';
  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 { configSvgIconsPlugin } from './svgSprite';
  17. import { configServerPlugin } from './serveStatic';
  18. // //预编译加载插件(不支持vite3作废)
  19. // import OptimizationPersist from 'vite-plugin-optimize-persist';
  20. import { resolve } from 'path';
  21. import { createSvgIconsPlugin } from 'vite-plugin-svg-icons';
  22. import topLevelAwait from 'vite-plugin-top-level-await';
  23. export function createVitePlugins(viteEnv: ViteEnv, isBuild: boolean) {
  24. const { VITE_USE_MOCK, VITE_BUILD_COMPRESS, VITE_BUILD_COMPRESS_DELETE_ORIGIN_FILE } = viteEnv;
  25. const vitePlugins: (PluginOption | PluginOption[])[] = [
  26. // have to
  27. vue(),
  28. // have to
  29. vueJsx(),
  30. // support name
  31. vueSetupExtend(),
  32. // @ts-ignore
  33. VitePluginCertificate({
  34. source: 'coding',
  35. }),
  36. createSvgIconsPlugin({
  37. // 指定需要缓存的图标文件夹
  38. iconDirs: [resolve(process.cwd(), 'src/assets/icons')],
  39. // 指定symbolId格式
  40. symbolId: 'svg-[name]',
  41. // 禁用压缩 否则想要修改无状态组件的stroke或者fill会影响到预设样式 例如stroke-width
  42. svgoOptions: false,
  43. customDomId: '__webtopo__svgpreview__icons__dom__',
  44. }),
  45. topLevelAwait({
  46. // The export name of top-level await promise for each chunk module
  47. promiseExportName: '__tla',
  48. // The function to generate import names of top-level await promise in each chunk module
  49. promiseImportName: (i) => `__tla_${i}`,
  50. }),
  51. ];
  52. vitePlugins.push(UnoCSS({ presets: [presetUno(), presetTypography()] }));
  53. // vite-plugin-html
  54. vitePlugins.push(configHtmlPlugin(viteEnv, isBuild));
  55. // vite-plugin-svg-icons
  56. vitePlugins.push(configSvgIconsPlugin(isBuild));
  57. // vite-plugin-mock
  58. VITE_USE_MOCK && vitePlugins.push(configMockPlugin(isBuild));
  59. // vite-plugin-purge-icons
  60. vitePlugins.push(purgeIcons());
  61. // vite-plugin-style-import
  62. vitePlugins.push(configStyleImportPlugin(isBuild));
  63. // rollup-plugin-visualizer
  64. vitePlugins.push(configVisualizerConfig());
  65. // vite-plugin-theme
  66. vitePlugins.push(configThemePlugin(isBuild));
  67. // vite-plugin-serve-static
  68. vitePlugins.push(configServerPlugin());
  69. // The following plugins only work in the production environment
  70. if (isBuild) {
  71. // rollup-plugin-gzip
  72. vitePlugins.push(configCompressPlugin(VITE_BUILD_COMPRESS, VITE_BUILD_COMPRESS_DELETE_ORIGIN_FILE));
  73. }
  74. // //vite-plugin-theme【预编译加载插件,解决vite首次打开界面加载慢问题】
  75. // vitePlugins.push(PkgConfig());
  76. // vitePlugins.push(OptimizationPersist());
  77. return vitePlugins;
  78. }