index.ts 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899
  1. import { resolve } from 'path'
  2. import Vue from '@vitejs/plugin-vue'
  3. import VueJsx from '@vitejs/plugin-vue-jsx'
  4. import WindiCSS from 'vite-plugin-windicss'
  5. import progress from 'vite-plugin-progress'
  6. import EslintPlugin from 'vite-plugin-eslint'
  7. import PurgeIcons from 'vite-plugin-purge-icons'
  8. import { ViteEjsPlugin } from 'vite-plugin-ejs'
  9. import ElementPlus from 'unplugin-element-plus/vite'
  10. import AutoImport from 'unplugin-auto-import/vite'
  11. import Components from 'unplugin-vue-components/vite'
  12. import { ElementPlusResolver } from 'unplugin-vue-components/resolvers'
  13. import viteCompression from 'vite-plugin-compression'
  14. import vueSetupExtend from 'vite-plugin-vue-setup-extend'
  15. import VueI18nPlugin from '@intlify/unplugin-vue-i18n/vite'
  16. import { createSvgIconsPlugin } from 'vite-plugin-svg-icons'
  17. export function createVitePlugins(VITE_APP_TITLE: string) {
  18. const root = process.cwd()
  19. // 路径查找
  20. function pathResolve(dir: string) {
  21. return resolve(root, '.', dir)
  22. }
  23. return [
  24. Vue(),
  25. VueJsx(),
  26. WindiCSS(),
  27. progress(),
  28. PurgeIcons(),
  29. vueSetupExtend(),
  30. ElementPlus({}),
  31. AutoImport({
  32. include: [
  33. /\.[tj]sx?$/, // .ts, .tsx, .js, .jsx
  34. /\.vue$/,
  35. /\.vue\?vue/, // .vue
  36. /\.md$/ // .md
  37. ],
  38. imports: [
  39. 'vue',
  40. 'vue-router',
  41. {
  42. '@/hooks/web/useI18n': ['useI18n'],
  43. '@/hooks/web/useXTable': ['useXTable'],
  44. '@/hooks/web/useMessage': ['useMessage'],
  45. '@/hooks/web/useVxeCrudSchemas': ['useVxeCrudSchemas'],
  46. '@/utils/formRules': ['required'],
  47. '@/utils/dict': ['DICT_TYPE']
  48. }
  49. ],
  50. dts: 'src/types/auto-imports.d.ts',
  51. resolvers: [ElementPlusResolver()],
  52. eslintrc: {
  53. enabled: false, // Default `false`
  54. filepath: './.eslintrc-auto-import.json', // Default `./.eslintrc-auto-import.json`
  55. globalsPropValue: true // Default `true`, (true | false | 'readonly' | 'readable' | 'writable' | 'writeable')
  56. }
  57. }),
  58. Components({
  59. // 要搜索组件的目录的相对路径
  60. dirs: ['src/components'],
  61. // 组件的有效文件扩展名
  62. extensions: ['vue', 'md'],
  63. // 搜索子目录
  64. deep: true,
  65. include: [/\.vue$/, /\.vue\?vue/],
  66. // 生成自定义 `auto-components.d.ts` 全局声明
  67. dts: 'src/types/auto-components.d.ts',
  68. // 自定义组件的解析器
  69. resolvers: [ElementPlusResolver()],
  70. exclude: [/[\\/]node_modules[\\/]/]
  71. }),
  72. EslintPlugin({
  73. cache: false,
  74. include: ['src/**/*.vue', 'src/**/*.ts', 'src/**/*.tsx'] // 检查的文件
  75. }),
  76. VueI18nPlugin({
  77. runtimeOnly: true,
  78. compositionOnly: true,
  79. include: [resolve(__dirname, 'src/locales/**')]
  80. }),
  81. createSvgIconsPlugin({
  82. iconDirs: [pathResolve('src/assets/svgs')],
  83. symbolId: 'icon-[dir]-[name]',
  84. svgoOptions: true
  85. }),
  86. viteCompression({
  87. verbose: true, // 是否在控制台输出压缩结果
  88. disable: false, // 是否禁用
  89. threshold: 10240, // 体积大于 threshold 才会被压缩,单位 b
  90. algorithm: 'gzip', // 压缩算法,可选 [ 'gzip' , 'brotliCompress' ,'deflate' , 'deflateRaw']
  91. ext: '.gz', // 生成的压缩包后缀
  92. deleteOriginFile: false //压缩后是否删除源文件
  93. }),
  94. ViteEjsPlugin({
  95. title: VITE_APP_TITLE
  96. })
  97. ]
  98. }