index.ts 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109
  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/useTable': ['useTable'],
  48. '@/hooks/web/useCrudSchemas': ['useCrudSchemas'],
  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. EslintPlugin({
  76. cache: false,
  77. include: ['src/**/*.vue', 'src/**/*.ts', 'src/**/*.tsx'] // 检查的文件
  78. }),
  79. VueI18nPlugin({
  80. runtimeOnly: true,
  81. compositionOnly: true,
  82. include: [resolve(__dirname, 'src/locales/**')]
  83. }),
  84. createSvgIconsPlugin({
  85. iconDirs: [pathResolve('src/assets/svgs')],
  86. symbolId: 'icon-[dir]-[name]',
  87. svgoOptions: true
  88. }),
  89. viteCompression({
  90. verbose: true, // 是否在控制台输出压缩结果
  91. disable: false, // 是否禁用
  92. threshold: 10240, // 体积大于 threshold 才会被压缩,单位 b
  93. algorithm: 'gzip', // 压缩算法,可选 [ 'gzip' , 'brotliCompress' ,'deflate' , 'deflateRaw']
  94. ext: '.gz', // 生成的压缩包后缀
  95. deleteOriginFile: false //压缩后是否删除源文件
  96. }),
  97. ViteEjsPlugin(),
  98. topLevelAwait({
  99. // https://juejin.cn/post/7152191742513512485
  100. // The export name of top-level await promise for each chunk module
  101. promiseExportName: '__tla',
  102. // The function to generate import names of top-level await promise in each chunk module
  103. promiseImportName: (i) => `__tla_${i}`
  104. })
  105. ]
  106. }