vite.config.js 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114
  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/Cesium/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: 80,
  61. host: true,
  62. open: true,
  63. // 优化开发服务器性能
  64. hmr: {
  65. overlay: true
  66. },
  67. // 增加服务器响应超时时间
  68. timeout: 60000,
  69. // 增加最大请求体大小
  70. maxBodySize: '100mb',
  71. proxy: {
  72. // https://cn.vitejs.dev/config/#server-proxy
  73. '/dev-api': {
  74. target: baseUrl,
  75. changeOrigin: true,
  76. rewrite: (p) => p.replace(/^\/dev-api/, '')
  77. },
  78. // 直接/api请求代理 - 处理baseURL为空的情况
  79. '/api': {
  80. target: baseUrl,
  81. changeOrigin: true
  82. },
  83. // 上传文件代理
  84. '/uploads': {
  85. target: baseUrl,
  86. changeOrigin: true
  87. },
  88. // springdoc proxy
  89. '^\/v3/api-docs/(.*)': {
  90. target: baseUrl,
  91. changeOrigin: true,
  92. }
  93. }
  94. },
  95. css: {
  96. postcss: {
  97. plugins: [
  98. {
  99. postcssPlugin: 'internal:charset-removal',
  100. AtRule: {
  101. charset: (atRule) => {
  102. if (atRule.name === 'charset') {
  103. atRule.remove()
  104. }
  105. }
  106. }
  107. }
  108. ]
  109. }
  110. }
  111. }
  112. })