index.ts 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141
  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-ng'
  17. import { createSvgIconsPlugin as createSvgIconsPlugin1 } from 'vite-plugin-svg-icons'
  18. import UnoCSS from 'unocss/vite'
  19. import type { Plugin } from 'vite'
  20. const createAppVersionPlugin = (): Plugin => {
  21. return {
  22. name: 'app-version',
  23. apply: 'build',
  24. generateBundle() {
  25. const buildTime = new Date().toISOString()
  26. this.emitFile({
  27. type: 'asset',
  28. fileName: 'version.json',
  29. source: JSON.stringify(
  30. {
  31. version: buildTime,
  32. buildTime
  33. },
  34. null,
  35. 2
  36. )
  37. })
  38. }
  39. }
  40. }
  41. export function createVitePlugins() {
  42. const root = process.cwd()
  43. // 路径查找
  44. function pathResolve(dir: string) {
  45. return resolve(root, '.', dir)
  46. }
  47. return [
  48. Vue(),
  49. VueJsx(),
  50. UnoCSS(),
  51. progress(),
  52. PurgeIcons(),
  53. ElementPlus({}),
  54. createSvgIconsPlugin1({
  55. // 指定需要缓存的图标文件夹
  56. iconDirs: [resolve(process.cwd(), 'src/assets/icons')],
  57. // 指定symbolId格式
  58. symbolId: 'mt-edit-[name]',
  59. // 禁用压缩 否则想要修改无状态组件的stroke或者fill会影响到预设样式 例如stroke-width
  60. svgoOptions: false,
  61. customDomId: '___mt__edit__icons__dom__'
  62. }),
  63. AutoImport({
  64. include: [
  65. /\.[tj]sx?$/, // .ts, .tsx, .js, .jsx
  66. /\.vue$/,
  67. /\.vue\?vue/, // .vue
  68. /\.md$/ // .md
  69. ],
  70. imports: [
  71. 'vue',
  72. 'vue-router',
  73. // 可额外添加需要 autoImport 的组件
  74. {
  75. '@/hooks/web/useI18n': ['useI18n'],
  76. '@/hooks/web/useMessage': ['useMessage'],
  77. '@/hooks/web/useTable': ['useTable'],
  78. '@/hooks/web/useCrudSchemas': ['useCrudSchemas'],
  79. '@/utils/formRules': ['required'],
  80. '@/utils/dict': ['DICT_TYPE']
  81. }
  82. ],
  83. dts: 'src/types/auto-imports.d.ts',
  84. resolvers: [ElementPlusResolver()],
  85. eslintrc: {
  86. enabled: false, // Default `false`
  87. filepath: './.eslintrc-auto-import.json', // Default `./.eslintrc-auto-import.json`
  88. globalsPropValue: true // Default `true`, (true | false | 'readonly' | 'readable' | 'writable' | 'writeable')
  89. }
  90. }),
  91. Components({
  92. // 生成自定义 `auto-components.d.ts` 全局声明
  93. dts: 'src/types/auto-components.d.ts',
  94. // 自定义组件的解析器
  95. resolvers: [ElementPlusResolver()],
  96. globs: [
  97. 'src/components/**/**.{vue, md}',
  98. '!src/components/DiyEditor/components/mobile/**',
  99. '!src/components/mt-dzr/**',
  100. '!src/components/mt-preview/**',
  101. '!src/components/mt-edit/**',
  102. '!src/components/custom-components/**'
  103. ]
  104. }),
  105. EslintPlugin({
  106. cache: false,
  107. include: ['src/**/*.vue', 'src/**/*.ts', 'src/**/*.tsx'] // 检查的文件
  108. }),
  109. VueI18nPlugin({
  110. runtimeOnly: true,
  111. compositionOnly: true,
  112. include: [resolve(__dirname, 'src/locales/**')]
  113. }),
  114. createSvgIconsPlugin({
  115. iconDirs: [pathResolve('src/assets/svgs')],
  116. symbolId: 'icon-[dir]-[name]'
  117. }),
  118. viteCompression({
  119. verbose: true, // 是否在控制台输出压缩结果
  120. disable: false, // 是否禁用
  121. threshold: 10240, // 体积大于 threshold 才会被压缩,单位 b
  122. algorithm: 'gzip', // 压缩算法,可选 [ 'gzip' , 'brotliCompress' ,'deflate' , 'deflateRaw']
  123. ext: '.gz', // 生成的压缩包后缀
  124. deleteOriginFile: false //压缩后是否删除源文件
  125. }),
  126. ViteEjsPlugin(),
  127. createAppVersionPlugin(),
  128. topLevelAwait({
  129. // https://juejin.cn/post/7152191742513512485
  130. // The export name of top-level await promise for each chunk module
  131. promiseExportName: '__tla',
  132. // The function to generate import names of top-level await promise in each chunk module
  133. promiseImportName: (i) => `__tla_${i}`
  134. })
  135. ]
  136. }