vite.config.ts 4.5 KB

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