vite.config.js 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138
  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. ],
  41. exclude: ['@supermap/iclient3d-vue-for-webgl']
  42. },
  43. // 打包配置
  44. build: {
  45. // https://vite.dev/config/build-options.html
  46. sourcemap: command === 'build' ? false : 'inline',
  47. outDir: 'dist',
  48. assetsDir: 'assets',
  49. chunkSizeWarningLimit: 2000,
  50. rollupOptions: {
  51. output: {
  52. chunkFileNames: 'static/js/[name]-[hash].js',
  53. entryFileNames: 'static/js/[name]-[hash].js',
  54. assetFileNames: 'static/[ext]/[name]-[hash].[ext]'
  55. }
  56. }
  57. },
  58. // vite 相关配置
  59. server: {
  60. port: 5173,
  61. host: true,
  62. open: true,
  63. // 优化开发服务器性能
  64. hmr: {
  65. overlay: true,
  66. // 优化 HMR 性能
  67. protocol: 'ws',
  68. host: 'localhost',
  69. port: 5173
  70. },
  71. // 优化文件监听
  72. watch: {
  73. // 使用轮询方式监听文件变化(Windows 下更稳定)
  74. usePolling: true,
  75. // 减少轮询间隔
  76. interval: 1000,
  77. // 忽略 node_modules
  78. ignored: ['**/node_modules/**', '**/.git/**']
  79. },
  80. // 增加服务器响应超时时间
  81. timeout: 60000,
  82. // 增加最大请求体大小
  83. maxBodySize: '100mb',
  84. // 优化 fs.cachedChecks
  85. fs: {
  86. strict: false,
  87. // 允许访问项目根目录
  88. allow: ['..']
  89. },
  90. proxy: {
  91. // https://cn.vitejs.dev/config/#server-proxy
  92. '/dev-api': {
  93. target: baseUrl,
  94. changeOrigin: true,
  95. rewrite: (p) => p.replace(/^\/dev-api/, '')
  96. },
  97. // 直接/api请求代理 - 处理baseURL为空的情况
  98. '/api': {
  99. target: baseUrl,
  100. changeOrigin: true
  101. },
  102. // 上传文件代理
  103. '/uploads': {
  104. target: baseUrl,
  105. changeOrigin: true
  106. },
  107. // profile 静态文件代理
  108. '/profile': {
  109. target: baseUrl,
  110. changeOrigin: true
  111. },
  112. // springdoc proxy
  113. '^\/v3/api-docs/(.*)': {
  114. target: baseUrl,
  115. changeOrigin: true,
  116. }
  117. }
  118. },
  119. css: {
  120. postcss: {
  121. plugins: [
  122. {
  123. postcssPlugin: 'internal:charset-removal',
  124. AtRule: {
  125. charset: (atRule) => {
  126. if (atRule.name === 'charset') {
  127. atRule.remove()
  128. }
  129. }
  130. }
  131. }
  132. ]
  133. }
  134. }
  135. }
  136. })