index.ts 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102
  1. import { resolve } from 'path'
  2. import Vue from '@vitejs/plugin-vue'
  3. import VueJsx from '@vitejs/plugin-vue-jsx'
  4. import progress from 'vite-plugin-progress'
  5. import EslintPlugin from 'vite-plugin-eslint'
  6. import PurgeIcons from 'vite-plugin-purge-icons'
  7. import { ViteEjsPlugin } from 'vite-plugin-ejs'
  8. // @ts-ignore
  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 topLevelAwait from 'vite-plugin-top-level-await'
  15. import VueI18nPlugin from '@intlify/unplugin-vue-i18n/vite'
  16. import { createSvgIconsPlugin } from 'vite-plugin-svg-icons'
  17. import UnoCSS from 'unocss/vite'
  18. import { VantResolver } from 'unplugin-vue-components/resolvers';
  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. UnoCSS(),
  29. progress(),
  30. PurgeIcons(),
  31. ElementPlus({}),
  32. VantResolver({}),
  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. // 生成自定义 `auto-components.d.ts` 全局声明
  63. dts: 'src/types/auto-components.d.ts',
  64. // 自定义组件的解析器
  65. resolvers: [ElementPlusResolver(),VantResolver()],
  66. globs: ["src/components/**/**.{vue, md}", '!src/components/DiyEditor/components/mobile/**']
  67. }),
  68. EslintPlugin({
  69. cache: false,
  70. include: ['src/**/*.vue', 'src/**/*.ts', 'src/**/*.tsx'] // 检查的文件
  71. }),
  72. VueI18nPlugin({
  73. runtimeOnly: true,
  74. compositionOnly: true,
  75. include: [resolve(__dirname, 'src/locales/**')]
  76. }),
  77. createSvgIconsPlugin({
  78. iconDirs: [pathResolve('src/assets/svgs')],
  79. symbolId: 'icon-[dir]-[name]',
  80. svgoOptions: true
  81. }),
  82. viteCompression({
  83. verbose: true, // 是否在控制台输出压缩结果
  84. disable: false, // 是否禁用
  85. threshold: 10240, // 体积大于 threshold 才会被压缩,单位 b
  86. algorithm: 'gzip', // 压缩算法,可选 [ 'gzip' , 'brotliCompress' ,'deflate' , 'deflateRaw']
  87. ext: '.gz', // 生成的压缩包后缀
  88. deleteOriginFile: false //压缩后是否删除源文件
  89. }),
  90. ViteEjsPlugin(),
  91. topLevelAwait({
  92. // https://juejin.cn/post/7152191742513512485
  93. // The export name of top-level await promise for each chunk module
  94. promiseExportName: '__tla',
  95. // The function to generate import names of top-level await promise in each chunk module
  96. promiseImportName: (i) => `__tla_${i}`
  97. })
  98. ]
  99. }