vite.config.ts 4.4 KB

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