vite.config.js 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899
  1. import { defineConfig, loadEnv } from 'vite'
  2. import path from 'path'
  3. import createVitePlugins from './vite/plugins'
  4. const baseUrl = 'http://localhost:8089' // 后端接口
  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. // 部署生产环境和开发环境下的URL。
  11. // 默认情况下,vite 会假设你的应用是被部署在一个域名的根路径上
  12. // 例如 https://www.ruoyi.vip/。如果应用被部署在一个子路径上,你就需要用这个选项指定这个子路径。例如,如果你的应用被部署在 https://www.ruoyi.vip/admin/,则设置 baseUrl 为 /admin/。
  13. base: VITE_APP_ENV === 'production' ? '/' : '/',
  14. plugins: createVitePlugins(env, command === 'build'),
  15. resolve: {
  16. // https://cn.vitejs.dev/config/#resolve-alias
  17. alias: {
  18. // 设置路径
  19. '~': path.resolve(__dirname, './'),
  20. // 设置别名
  21. '@': path.resolve(__dirname, './src')
  22. },
  23. // https://cn.vitejs.dev/config/#resolve-extensions
  24. extensions: ['.mjs', '.js', '.ts', '.jsx', '.tsx', '.json', '.vue']
  25. },
  26. define: {
  27. // 解决 sockjs-client / stompjs 在浏览器中引用 global 未定义的问题
  28. 'global': 'globalThis'
  29. },
  30. // 打包配置
  31. build: {
  32. // https://vite.dev/config/build-options.html
  33. sourcemap: command === 'build' ? false : 'inline',
  34. outDir: 'dist',
  35. assetsDir: 'assets',
  36. chunkSizeWarningLimit: 2000,
  37. rollupOptions: {
  38. output: {
  39. chunkFileNames: 'static/js/[name]-[hash].js',
  40. entryFileNames: 'static/js/[name]-[hash].js',
  41. assetFileNames: 'static/[ext]/[name]-[hash].[ext]'
  42. }
  43. }
  44. },
  45. // vite 相关配置
  46. server: {
  47. port: 80,
  48. host: true,
  49. open: true,
  50. proxy: {
  51. // https://cn.vitejs.dev/config/#server-proxy
  52. '/dev-api': {
  53. target: baseUrl,
  54. changeOrigin: true,
  55. rewrite: (p) => p.replace(/^\/dev-api/, '')
  56. },
  57. // 后端API代理 - 处理 /v1/* 请求(如瓦片地图接口)
  58. '/v1': {
  59. target: baseUrl,
  60. changeOrigin: true
  61. },
  62. // WebSocket/SockJS proxy - 保持 /ws/robot 路径不变,因为后端端点就是 /ws/robot
  63. '/ws/robot': {
  64. target: baseUrl,
  65. changeOrigin: true,
  66. ws: true
  67. },
  68. '/ws': {
  69. target: baseUrl,
  70. changeOrigin: true,
  71. ws: true
  72. },
  73. // springdoc proxy
  74. '^/v3/api-docs/(.*)': {
  75. target: baseUrl,
  76. changeOrigin: true,
  77. }
  78. }
  79. },
  80. css: {
  81. postcss: {
  82. plugins: [
  83. {
  84. postcssPlugin: 'internal:charset-removal',
  85. AtRule: {
  86. charset: (atRule) => {
  87. if (atRule.name === 'charset') {
  88. atRule.remove()
  89. }
  90. }
  91. }
  92. }
  93. ]
  94. }
  95. }
  96. }
  97. })