vite.config.ts 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177
  1. import { defineConfig, loadEnv } from 'vite'
  2. import vue from '@vitejs/plugin-vue'
  3. import path from 'path'
  4. import { fileURLToPath } from 'url'
  5. import vueDevTools from 'vite-plugin-vue-devtools'
  6. import viteCompression from 'vite-plugin-compression'
  7. import Components from 'unplugin-vue-components/vite'
  8. import AutoImport from 'unplugin-auto-import/vite'
  9. import ElementPlus from 'unplugin-element-plus/vite'
  10. import { ElementPlusResolver } from 'unplugin-vue-components/resolvers'
  11. import { VantResolver } from '@vant/auto-import-resolver'
  12. import tailwindcss from '@tailwindcss/vite'
  13. // import { visualizer } from 'rollup-plugin-visualizer'
  14. export default ({ mode }: { mode: string }) => {
  15. const root = process.cwd()
  16. const env = loadEnv(mode, root)
  17. const { VITE_VERSION, VITE_PORT, VITE_BASE_URL, VITE_API_URL, VITE_API_PROXY_URL } = env
  18. console.log(`🚀 API_URL = ${VITE_API_URL}`)
  19. console.log(`🚀 VERSION = ${VITE_VERSION}`)
  20. return defineConfig({
  21. define: {
  22. __APP_VERSION__: JSON.stringify(VITE_VERSION)
  23. },
  24. base: VITE_BASE_URL,
  25. server: {
  26. port: Number(VITE_PORT),
  27. proxy: {
  28. '/api': {
  29. target: VITE_API_PROXY_URL,
  30. changeOrigin: true
  31. },
  32. // Web端接口代理 - 转发到后端 /api 前缀
  33. '/web': {
  34. target: 'http://192.168.2.178:8000/api',
  35. changeOrigin: true,
  36. rewrite: (path) => `/api${path}`
  37. },
  38. // 小程序端接口代理
  39. '/mini': {
  40. target: 'http://192.168.2.178:8000/api',
  41. changeOrigin: true,
  42. rewrite: (path) => `/api${path}`
  43. }
  44. },
  45. host: true
  46. },
  47. // 路径别名
  48. resolve: {
  49. alias: {
  50. '@': fileURLToPath(new URL('./src', import.meta.url)),
  51. '@views': resolvePath('src/views'),
  52. '@imgs': resolvePath('src/assets/images'),
  53. '@icons': resolvePath('src/assets/icons'),
  54. '@utils': resolvePath('src/utils'),
  55. '@stores': resolvePath('src/store'),
  56. '@styles': resolvePath('src/assets/styles')
  57. }
  58. },
  59. build: {
  60. target: 'es2015',
  61. outDir: 'dist',
  62. chunkSizeWarningLimit: 2000,
  63. minify: 'terser',
  64. terserOptions: {
  65. compress: {
  66. // 生产环境去除 console
  67. drop_console: true,
  68. // 生产环境去除 debugger
  69. drop_debugger: true
  70. }
  71. },
  72. dynamicImportVarsOptions: {
  73. warnOnError: true,
  74. exclude: [],
  75. include: ['src/views/**/*.vue']
  76. }
  77. },
  78. plugins: [
  79. vue(),
  80. tailwindcss(),
  81. // 自动按需导入 API
  82. AutoImport({
  83. imports: ['vue', 'vue-router', 'pinia', '@vueuse/core'],
  84. dts: 'src/types/import/auto-imports.d.ts',
  85. resolvers: [ElementPlusResolver()],
  86. eslintrc: {
  87. enabled: true,
  88. filepath: './.auto-import.json',
  89. globalsPropValue: true
  90. }
  91. }),
  92. // 自动按需导入组件
  93. Components({
  94. dts: 'src/types/import/components.d.ts',
  95. resolvers: [
  96. (componentName) => {
  97. if (componentName === 'ElTag') {
  98. return { name: 'default', from: '@/utils/patch-el-tag-empty' }
  99. }
  100. },
  101. ElementPlusResolver(),
  102. VantResolver()
  103. ]
  104. }),
  105. // 按需定制主题配置
  106. ElementPlus({
  107. useSource: true
  108. }),
  109. // 压缩
  110. viteCompression({
  111. verbose: false, // 是否在控制台输出压缩结果
  112. disable: false, // 是否禁用
  113. algorithm: 'gzip', // 压缩算法
  114. ext: '.gz', // 压缩后的文件名后缀
  115. threshold: 10240, // 只有大小大于该值的资源会被处理 10240B = 10KB
  116. deleteOriginFile: false // 压缩后是否删除原文件
  117. }),
  118. vueDevTools()
  119. // 打包分析
  120. // visualizer({
  121. // open: true,
  122. // gzipSize: true,
  123. // brotliSize: true,
  124. // filename: 'dist/stats.html' // 分析图生成的文件名及路径
  125. // }),
  126. ],
  127. // 依赖预构建:避免运行时重复请求与转换,提升首次加载速度
  128. optimizeDeps: {
  129. include: [
  130. 'echarts/core',
  131. 'echarts/charts',
  132. 'echarts/components',
  133. 'echarts/renderers',
  134. 'xlsx',
  135. 'xgplayer',
  136. 'crypto-js',
  137. 'file-saver',
  138. 'vue-img-cutter',
  139. 'element-plus/es',
  140. 'element-plus/es/components/*/style/css',
  141. 'element-plus/es/components/*/style/index'
  142. ]
  143. },
  144. css: {
  145. preprocessorOptions: {
  146. // sass variable and mixin
  147. scss: {
  148. additionalData: `
  149. @use "@styles/core/el-light.scss" as *;
  150. @use "@styles/core/mixin.scss" as *;
  151. `
  152. }
  153. },
  154. postcss: {
  155. plugins: [
  156. {
  157. postcssPlugin: 'internal:charset-removal',
  158. AtRule: {
  159. charset: (atRule) => {
  160. if (atRule.name === 'charset') {
  161. atRule.remove()
  162. }
  163. }
  164. }
  165. }
  166. ]
  167. }
  168. }
  169. })
  170. }
  171. function resolvePath(paths: string) {
  172. return path.resolve(__dirname, paths)
  173. }