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 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. 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. UnoCSS(),
  28. progress(),
  29. PurgeIcons(),
  30. ElementPlus({}),
  31. AutoImport({
  32. include: [
  33. /\.[tj]sx?$/, // .ts, .tsx, .js, .jsx
  34. /\.vue$/,
  35. /\.vue\?vue/, // .vue
  36. /\.md$/ // .md
  37. ],
  38. imports: [
  39. 'vue',
  40. 'vue-router',
  41. // 可额外添加需要 autoImport 的组件
  42. {
  43. '@/hooks/web/useI18n': ['useI18n'],
  44. '@/hooks/web/useMessage': ['useMessage'],
  45. '@/hooks/web/useTable': ['useTable'],
  46. '@/hooks/web/useCrudSchemas': ['useCrudSchemas'],
  47. '@/utils/formRules': ['required'],
  48. '@/utils/dict': ['DICT_TYPE']
  49. }
  50. ],
  51. dts: 'src/types/auto-imports.d.ts',
  52. resolvers: [ElementPlusResolver()],
  53. eslintrc: {
  54. enabled: false, // Default `false`
  55. filepath: './.eslintrc-auto-import.json', // Default `./.eslintrc-auto-import.json`
  56. globalsPropValue: true // Default `true`, (true | false | 'readonly' | 'readable' | 'writable' | 'writeable')
  57. }
  58. }),
  59. Components({
  60. // 生成自定义 `auto-components.d.ts` 全局声明
  61. dts: 'src/types/auto-components.d.ts',
  62. // 自定义组件的解析器
  63. resolvers: [ElementPlusResolver()],
  64. globs: ["src/components/**/**.{vue, md}", '!src/components/DiyEditor/components/mobile/**']
  65. }),
  66. EslintPlugin({
  67. cache: false,
  68. include: ['src/**/*.vue', 'src/**/*.ts', 'src/**/*.tsx'] // 检查的文件
  69. }),
  70. VueI18nPlugin({
  71. runtimeOnly: true,
  72. compositionOnly: true,
  73. include: [resolve(__dirname, 'src/locales/**')]
  74. }),
  75. createSvgIconsPlugin({
  76. iconDirs: [pathResolve('src/assets/svgs')],
  77. symbolId: 'icon-[dir]-[name]',
  78. svgoOptions: true
  79. }),
  80. viteCompression({
  81. verbose: true, // 是否在控制台输出压缩结果
  82. disable: false, // 是否禁用
  83. threshold: 10240, // 体积大于 threshold 才会被压缩,单位 b
  84. algorithm: 'gzip', // 压缩算法,可选 [ 'gzip' , 'brotliCompress' ,'deflate' , 'deflateRaw']
  85. ext: '.gz', // 生成的压缩包后缀
  86. deleteOriginFile: false //压缩后是否删除源文件
  87. }),
  88. ViteEjsPlugin(),
  89. topLevelAwait({
  90. // https://juejin.cn/post/7152191742513512485
  91. // The export name of top-level await promise for each chunk module
  92. promiseExportName: '__tla',
  93. // The function to generate import names of top-level await promise in each chunk module
  94. promiseImportName: (i) => `__tla_${i}`
  95. })
  96. ]
  97. }