index.ts 3.8 KB

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