vite.config.js 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178
  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. // 外部 API 反向代理(开发模式,生产环境由 Nginx 转发)
  96. // 必须放在 /dev-api 等通用前缀前面,更长前缀优先匹配
  97. '/proxy/tianditu-api': {
  98. target: 'https://api.tianditu.gov.cn',
  99. changeOrigin: true,
  100. secure: false,
  101. rewrite: (p) => p.replace(/^\/proxy\/tianditu-api/, '')
  102. },
  103. '/proxy/tianditu': {
  104. target: 'https://t0.tianditu.gov.cn',
  105. changeOrigin: true,
  106. secure: false,
  107. rewrite: (p) => p.replace(/^\/proxy\/tianditu/, '')
  108. },
  109. '/proxy/slt': {
  110. target: 'https://typhoon.slt.zj.gov.cn',
  111. changeOrigin: true,
  112. secure: false,
  113. rewrite: (p) => p.replace(/^\/proxy\/slt/, '')
  114. },
  115. // https://cn.vitejs.dev/config/#server-proxy
  116. '/dev-api': {
  117. target: baseUrl,
  118. changeOrigin: true,
  119. rewrite: (p) => p.replace(/^\/dev-api/, '')
  120. },
  121. // 直接/api请求代理 - 处理baseURL为空的情况
  122. '/api': {
  123. target: baseUrl,
  124. changeOrigin: true
  125. },
  126. // 上传文件代理
  127. '/uploads': {
  128. target: baseUrl,
  129. changeOrigin: true
  130. },
  131. // profile 静态文件代理
  132. '/profile': {
  133. target: baseUrl,
  134. changeOrigin: true
  135. },
  136. // springdoc proxy
  137. '^\/v3/api-docs/(.*)': {
  138. target: baseUrl,
  139. changeOrigin: true,
  140. }
  141. }
  142. },
  143. // 预览服务器配置(生产环境预览)
  144. preview: {
  145. port: 4173,
  146. host: true,
  147. proxy: {
  148. '/prod-api': {
  149. target: 'http://localhost:8080',
  150. changeOrigin: true,
  151. rewrite: (p) => p.replace(/^\/prod-api/, '/prod-api')
  152. },
  153. '/uploads': {
  154. target: 'http://localhost:8080',
  155. changeOrigin: true
  156. }
  157. }
  158. },
  159. css: {
  160. postcss: {
  161. plugins: [
  162. {
  163. postcssPlugin: 'internal:charset-removal',
  164. AtRule: {
  165. charset: (atRule) => {
  166. if (atRule.name === 'charset') {
  167. atRule.remove()
  168. }
  169. }
  170. }
  171. }
  172. ]
  173. }
  174. }
  175. }
  176. })