index.ts 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121
  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 { visualizer } from 'rollup-plugin-visualizer'
  19. export function createVitePlugins() {
  20. const root = process.cwd()
  21. // 路径查找
  22. function pathResolve(dir: string) {
  23. return resolve(root, '.', dir)
  24. }
  25. const isBuild = process.argv.slice(2).includes('build')
  26. const plugins = [
  27. Vue(),
  28. VueJsx(),
  29. UnoCSS(),
  30. progress(),
  31. PurgeIcons(),
  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. // 生成自定义 `auto-components.d.ts` 全局声明
  63. dts: 'src/types/auto-components.d.ts',
  64. // 自定义组件的解析器
  65. resolvers: [ElementPlusResolver()],
  66. globs: ["src/components/**/**.{vue, md}", '!src/components/DiyEditor/components/mobile/**']
  67. }),
  68. VueI18nPlugin({
  69. runtimeOnly: true,
  70. compositionOnly: true,
  71. include: [pathResolve('src/locales/**')]
  72. }),
  73. createSvgIconsPlugin({
  74. iconDirs: [pathResolve('src/assets/svgs')],
  75. symbolId: 'icon-[dir]-[name]',
  76. svgoOptions: true
  77. }),
  78. viteCompression({
  79. verbose: true,
  80. disable: false,
  81. threshold: 10240,
  82. algorithm: 'gzip',
  83. ext: '.gz',
  84. deleteOriginFile: false
  85. }),
  86. viteCompression({
  87. verbose: false,
  88. disable: false,
  89. threshold: 10240,
  90. algorithm: 'brotliCompress',
  91. ext: '.br',
  92. deleteOriginFile: false
  93. }),
  94. ViteEjsPlugin(),
  95. topLevelAwait({
  96. promiseExportName: '__tla',
  97. promiseImportName: (i) => `__tla_${i}`
  98. })
  99. ]
  100. // ESLint 插件在开发和构建模式都禁用,避免阻塞热更新和构建(使用 IDE 内的 ESLint 即可)
  101. // if (!isBuild) {
  102. // plugins.push(
  103. // EslintPlugin({
  104. // cache: true,
  105. // include: ['src/**/*.vue', 'src/**/*.ts', 'src/**/*.tsx']
  106. // })
  107. // )
  108. // }
  109. // 仅在分析模式下启用 visualizer: ANALYZE=true pnpm build
  110. if (process.env.ANALYZE === 'true') {
  111. plugins.push(visualizer({ open: true, filename: 'visualizer.html' }))
  112. }
  113. return plugins
  114. }