index.ts 3.9 KB

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