vite.config.ts 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151
  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-02 16:34:08
  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. exclude: undefined,
  25. include: undefined,
  26. includePublic: true,
  27. logStats: true,
  28. ansiColors: true,
  29. svg: {
  30. multipass: true,
  31. plugins: [
  32. {
  33. name: 'preset-default',
  34. params: {
  35. overrides: {
  36. cleanupNumericValues: false,
  37. removeViewBox: false // https://github.com/svg/svgo/issues/1128
  38. },
  39. cleanupIDs: {
  40. minify: false,
  41. remove: false
  42. },
  43. convertPathData: false
  44. }
  45. },
  46. 'sortAttrs',
  47. {
  48. name: 'addAttributesToSVGElement',
  49. params: {
  50. attributes: [{ xmlns: 'http://www.w3.org/2000/svg' }]
  51. }
  52. }
  53. ]
  54. },
  55. png: {
  56. // https://sharp.pixelplumbing.com/api-output#png
  57. quality: 100
  58. },
  59. jpeg: {
  60. // https://sharp.pixelplumbing.com/api-output#jpeg
  61. quality: 100
  62. },
  63. jpg: {
  64. // https://sharp.pixelplumbing.com/api-output#jpeg
  65. quality: 100
  66. },
  67. tiff: {
  68. // https://sharp.pixelplumbing.com/api-output#tiff
  69. quality: 100
  70. },
  71. // gif does not support lossless compression
  72. // https://sharp.pixelplumbing.com/api-output#gif
  73. gif: {},
  74. webp: {
  75. // https://sharp.pixelplumbing.com/api-output#webp
  76. lossless: true
  77. },
  78. avif: {
  79. // https://sharp.pixelplumbing.com/api-output#avif
  80. lossless: true
  81. },
  82. cache: false,
  83. cacheLocation: undefined
  84. }
  85. export default defineConfig(({ mode }) => {
  86. return {
  87. build: {
  88. rollupOptions: {
  89. output: {
  90. manualChunks: {
  91. echarts: ['echarts']
  92. },
  93. chunkFileNames: 'js/[name]-[hash].js', // 引入文件名的名称
  94. entryFileNames: 'js/[name]-[hash].js', // 包的入口文件名称
  95. assetFileNames: '[ext]/[name]-[hash].[ext]' // 资源文件像 字体,图片等
  96. }
  97. },
  98. terserOptions: {
  99. compress: {
  100. drop_console: true,
  101. drop_debugger: true
  102. }
  103. },
  104. esbuild: {
  105. drop: mode === 'production' ? ['console', 'debugger'] : []
  106. }
  107. },
  108. esbuild: {
  109. drop: ['console', 'debugger']
  110. },
  111. plugins: [
  112. vue(),
  113. ViteImageOptimizer(DEFAULT_OPTIONS),
  114. viteCompression({
  115. verbose: true, // 默认即可
  116. disable: false, // 开启压缩(不禁用),默认即可
  117. deleteOriginFile: false, // 删除源文件
  118. threshold: 5120, // 压缩前最小文件大小
  119. algorithm: 'gzip', // 压缩算法
  120. ext: '.gz' // 文件类型
  121. }),
  122. visualizer({ open: true }),
  123. AutoImport({
  124. resolvers: [ElementPlusResolver()]
  125. }),
  126. Components({
  127. resolvers: [
  128. ElementPlusResolver(),
  129. IconsResolver({
  130. prefix: 'icon'
  131. })
  132. ]
  133. }),
  134. Icons({
  135. autoInstall: true
  136. })
  137. ],
  138. resolve: {
  139. alias: {
  140. '@': fileURLToPath(new URL('./src', import.meta.url))
  141. }
  142. }
  143. }
  144. })