vite.config.js 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158
  1. import { defineConfig, loadEnv } from 'vite'
  2. import path from 'path'
  3. import createVitePlugins from './vite/plugins'
  4. const baseUrl = 'http://localhost:8448' // 后端接口
  5. // https://vitejs.dev/config/
  6. export default defineConfig(({ mode, command }) => {
  7. const env = loadEnv(mode, process.cwd())
  8. const { VITE_APP_ENV } = env
  9. return {
  10. // 设置Cesium基础路径
  11. define: {
  12. CESIUM_BASE_URL: JSON.stringify('/Cesium/')
  13. },
  14. // 部署生产环境和开发环境下的URL。
  15. // 默认情况下,vite 会假设你的应用是被部署在一个域名的根路径上
  16. // 例如 https://www.ruoyi.vip/。如果应用被部署在一个子路径上,你就需要用这个选项指定这个子路径。例如,如果你的应用被部署在 https://www.ruoyi.vip/admin/,则设置 baseUrl 为 /admin/。
  17. base: VITE_APP_ENV === 'production' ? '/' : '/',
  18. plugins: createVitePlugins(env, command === 'build'),
  19. resolve: {
  20. // https://cn.vitejs.dev/config/#resolve-alias
  21. alias: {
  22. // 设置路径
  23. '~': path.resolve(__dirname, './'),
  24. // 设置别名
  25. '@': path.resolve(__dirname, './src'),
  26. // Cesium别名
  27. 'cesium': path.resolve(__dirname, './node_modules/cesium/Build/CesiumUnminified/Cesium.js')
  28. },
  29. // https://cn.vitejs.dev/config/#resolve-extensions
  30. extensions: ['.mjs', '.js', '.ts', '.jsx', '.tsx', '.json', '.vue']
  31. },
  32. // 优化依赖预构建
  33. optimizeDeps: {
  34. include: [
  35. 'vue',
  36. 'vue-router',
  37. 'element-plus',
  38. '@element-plus/icons-vue',
  39. 'cesium',
  40. 'three',
  41. 'dat.gui'
  42. ],
  43. exclude: ['@supermap/icloud3d-vue-for-webgl']
  44. },
  45. // 二进制文件作为静态资源处理
  46. assetsInclude: ['**/*.bin'],
  47. // 打包配置
  48. build: {
  49. // https://vite.dev/config/build-options.html
  50. sourcemap: command === 'build' ? false : 'inline',
  51. outDir: 'dist',
  52. assetsDir: 'assets',
  53. chunkSizeWarningLimit: 2000,
  54. rollupOptions: {
  55. output: {
  56. chunkFileNames: 'static/js/[name]-[hash].js',
  57. entryFileNames: 'static/js/[name]-[hash].js',
  58. assetFileNames: 'static/[ext]/[name]-[hash].[ext]'
  59. }
  60. }
  61. },
  62. // vite 相关配置
  63. server: {
  64. port: 5173,
  65. host: true,
  66. open: true,
  67. // 优化开发服务器性能
  68. hmr: {
  69. overlay: true,
  70. // 优化 HMR 性能
  71. protocol: 'ws',
  72. host: 'localhost',
  73. port: 5173
  74. },
  75. // 优化文件监听
  76. watch: {
  77. // 使用轮询方式监听文件变化(Windows 下更稳定)
  78. usePolling: true,
  79. // 减少轮询间隔
  80. interval: 1000,
  81. // 忽略 node_modules
  82. ignored: ['**/node_modules/**', '**/.git/**']
  83. },
  84. // 增加服务器响应超时时间
  85. timeout: 60000,
  86. // 增加最大请求体大小
  87. maxBodySize: '100mb',
  88. // 优化 fs.cachedChecks
  89. fs: {
  90. strict: false,
  91. // 允许访问项目根目录
  92. allow: ['..']
  93. },
  94. proxy: {
  95. // https://cn.vitejs.dev/config/#server-proxy
  96. '/dev-api': {
  97. target: baseUrl,
  98. changeOrigin: true,
  99. rewrite: (p) => p.replace(/^\/dev-api/, '')
  100. },
  101. // 直接/api请求代理 - 处理baseURL为空的情况
  102. '/api': {
  103. target: baseUrl,
  104. changeOrigin: true
  105. },
  106. // 上传文件代理
  107. '/uploads': {
  108. target: baseUrl,
  109. changeOrigin: true
  110. },
  111. // profile 静态文件代理
  112. '/profile': {
  113. target: baseUrl,
  114. changeOrigin: true
  115. },
  116. // springdoc proxy
  117. '^\/v3/api-docs/(.*)': {
  118. target: baseUrl,
  119. changeOrigin: true,
  120. }
  121. }
  122. },
  123. // 预览服务器配置(生产环境预览)
  124. preview: {
  125. port: 4173,
  126. host: true,
  127. proxy: {
  128. '/prod-api': {
  129. target: 'http://localhost:8080',
  130. changeOrigin: true,
  131. rewrite: (p) => p.replace(/^\/prod-api/, '/prod-api')
  132. },
  133. '/uploads': {
  134. target: 'http://localhost:8080',
  135. changeOrigin: true
  136. }
  137. }
  138. },
  139. css: {
  140. postcss: {
  141. plugins: [
  142. {
  143. postcssPlugin: 'internal:charset-removal',
  144. AtRule: {
  145. charset: (atRule) => {
  146. if (atRule.name === 'charset') {
  147. atRule.remove()
  148. }
  149. }
  150. }
  151. }
  152. ]
  153. }
  154. }
  155. }
  156. })