index.ts 3.4 KB

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