vite.config.ts 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139
  1. /*
  2. * @Author: fxs bjnsfxs@163.com
  3. * @Date: 2024-08-20 14:06:49
  4. * @LastEditors: fxs bjnsfxs@163.com
  5. * @LastEditTime: 2024-09-13 16:43:31
  6. * @FilePath: \Game-Backstage-Management-System\vite.config.ts
  7. * @Description:
  8. *
  9. */
  10. import { defineConfig } from 'vite'
  11. import vue from '@vitejs/plugin-vue'
  12. import { fileURLToPath, URL } from 'node:url'
  13. import AutoImport from 'unplugin-auto-import/vite'
  14. import Components from 'unplugin-vue-components/vite'
  15. import { ElementPlusResolver } from 'unplugin-vue-components/resolvers'
  16. import Icons from 'unplugin-icons/vite'
  17. import IconsResolver from 'unplugin-icons/resolver'
  18. import { visualizer } from 'rollup-plugin-visualizer'
  19. // 打包
  20. import viteCompression from 'vite-plugin-compression'
  21. import { ViteImageOptimizer } from 'vite-plugin-image-optimizer'
  22. const DEFAULT_OPTIONS = {
  23. test: /\.(jpe?g|png|gif|tiff|webp|svg|avif)$/i,
  24. includePublic: true,
  25. logStats: true,
  26. ansiColors: true,
  27. svg: {
  28. multipass: true,
  29. plugins: [
  30. {
  31. name: 'preset-default',
  32. params: {
  33. overrides: {
  34. cleanupNumericValues: false,
  35. removeViewBox: false // https://github.com/svg/svgo/issues/1128
  36. },
  37. cleanupIDs: {
  38. minify: false,
  39. remove: false
  40. },
  41. convertPathData: false
  42. }
  43. },
  44. 'sortAttrs',
  45. {
  46. name: 'addAttributesToSVGElement',
  47. params: {
  48. attributes: [{ xmlns: 'http://www.w3.org/2000/svg' }]
  49. }
  50. }
  51. ]
  52. },
  53. png: {
  54. // https://sharp.pixelplumbing.com/api-output#png
  55. quality: 100
  56. },
  57. jpeg: {
  58. // https://sharp.pixelplumbing.com/api-output#jpeg
  59. quality: 100
  60. },
  61. jpg: {
  62. // https://sharp.pixelplumbing.com/api-output#jpeg
  63. quality: 100
  64. },
  65. tiff: {
  66. // https://sharp.pixelplumbing.com/api-output#tiff
  67. quality: 100
  68. },
  69. // gif does not support lossless compression
  70. // https://sharp.pixelplumbing.com/api-output#gif
  71. gif: {},
  72. webp: {
  73. // https://sharp.pixelplumbing.com/api-output#webp
  74. lossless: true
  75. },
  76. avif: {
  77. // https://sharp.pixelplumbing.com/api-output#avif
  78. lossless: true
  79. },
  80. cache: false,
  81. cacheLocation: undefined
  82. }
  83. export default defineConfig(({ mode }) => {
  84. return {
  85. build: {
  86. rollupOptions: {
  87. output: {
  88. manualChunks: {
  89. echarts: ['echarts']
  90. },
  91. chunkFileNames: 'js/[name]-[hash].js', // 引入文件名的名称
  92. entryFileNames: 'js/[name]-[hash].js', // 包的入口文件名称
  93. assetFileNames: '[ext]/[name]-[hash].[ext]' // 资源文件像 字体,图片等
  94. }
  95. }
  96. },
  97. esbuild: {
  98. drop: mode === 'production' ? ['console', 'debugger'] : []
  99. },
  100. plugins: [
  101. vue(),
  102. ViteImageOptimizer(DEFAULT_OPTIONS),
  103. viteCompression({
  104. verbose: true, // 默认即可
  105. disable: false, // 开启压缩(不禁用),默认即可
  106. deleteOriginFile: false, // 删除源文件
  107. threshold: 5120, // 压缩前最小文件大小
  108. algorithm: 'gzip', // 压缩算法
  109. ext: '.gz' // 文件类型
  110. }),
  111. visualizer({ open: true }),
  112. AutoImport({
  113. resolvers: [ElementPlusResolver()]
  114. }),
  115. Components({
  116. resolvers: [
  117. ElementPlusResolver(),
  118. IconsResolver({
  119. prefix: 'icon'
  120. })
  121. ]
  122. }),
  123. Icons({
  124. autoInstall: true
  125. })
  126. ],
  127. resolve: {
  128. alias: {
  129. '@': fileURLToPath(new URL('./src', import.meta.url))
  130. }
  131. }
  132. }
  133. })