vite.config.ts 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108
  1. import { defineConfig } from 'vite'
  2. import react from '@vitejs/plugin-react'
  3. import path from 'path'
  4. // https://vite.dev/config/
  5. export default defineConfig({
  6. plugins: [react()],
  7. resolve: {
  8. alias: {
  9. '@': path.resolve(__dirname, './src'),
  10. },
  11. },
  12. server: {
  13. port: parseInt(process.env.PORT || '5173'),
  14. allowedHosts: true, // Allow all hosts for local network development
  15. proxy: {
  16. // WebSocket endpoints
  17. '/ws': {
  18. target: 'ws://localhost:8080',
  19. ws: true,
  20. // Suppress connection errors (common during backend restarts)
  21. configure: (proxy, _options) => {
  22. // Handle proxy errors silently for expected connection issues
  23. const isConnectionError = (err: Error & { code?: string }) => {
  24. const msg = err.message || ''
  25. const code = err.code || ''
  26. // Check error code (most reliable for AggregateError)
  27. if (['ECONNRESET', 'ECONNREFUSED', 'EPIPE', 'ETIMEDOUT'].includes(code)) {
  28. return true
  29. }
  30. // Check message as fallback
  31. if (msg.includes('ECONNRESET') || msg.includes('ECONNREFUSED') ||
  32. msg.includes('EPIPE') || msg.includes('ETIMEDOUT') ||
  33. msg.includes('AggregateError')) {
  34. return true
  35. }
  36. return false
  37. }
  38. const handleError = (err: Error) => {
  39. if (isConnectionError(err)) {
  40. return // Silently ignore connection errors
  41. }
  42. // Only log unexpected errors
  43. console.error('WebSocket proxy error:', err.message)
  44. }
  45. proxy.on('error', handleError)
  46. proxy.on('proxyReqWs', (_proxyReq, _req, socket) => {
  47. socket.on('error', (err) => {
  48. if (!isConnectionError(err)) {
  49. console.error('WebSocket socket error:', err.message)
  50. }
  51. })
  52. })
  53. },
  54. },
  55. // All /api endpoints
  56. '/api': 'http://localhost:8080',
  57. // Static assets
  58. '/static': 'http://localhost:8080',
  59. // Preview images
  60. '/preview': 'http://localhost:8080',
  61. // Legacy root-level API endpoints (for backwards compatibility)
  62. // Pattern execution
  63. '/send_home': 'http://localhost:8080',
  64. '/send_coordinate': 'http://localhost:8080',
  65. '/stop_execution': 'http://localhost:8080',
  66. '/pause_execution': 'http://localhost:8080',
  67. '/resume_execution': 'http://localhost:8080',
  68. '/skip_pattern': 'http://localhost:8080',
  69. '/run_theta_rho': 'http://localhost:8080',
  70. '/run_playlist': 'http://localhost:8080',
  71. // Movement
  72. '/move_to_center': 'http://localhost:8080',
  73. '/move_to_perimeter': 'http://localhost:8080',
  74. // Speed
  75. '/set_speed': 'http://localhost:8080',
  76. '/get_speed': 'http://localhost:8080',
  77. // Connection
  78. '/serial_status': 'http://localhost:8080',
  79. '/list_serial_ports': 'http://localhost:8080',
  80. '/connect': 'http://localhost:8080',
  81. '/disconnect': 'http://localhost:8080',
  82. // Patterns
  83. '/list_theta_rho_files': 'http://localhost:8080',
  84. '/list_theta_rho_files_with_metadata': 'http://localhost:8080',
  85. '/preview_thr': 'http://localhost:8080',
  86. '/preview_thr_batch': 'http://localhost:8080',
  87. '/get_theta_rho_coordinates': 'http://localhost:8080',
  88. // Playlists
  89. '/list_all_playlists': 'http://localhost:8080',
  90. '/get_playlist': 'http://localhost:8080',
  91. '/create_playlist': 'http://localhost:8080',
  92. '/modify_playlist': 'http://localhost:8080',
  93. '/delete_playlist': 'http://localhost:8080',
  94. '/rename_playlist': 'http://localhost:8080',
  95. '/add_to_playlist': 'http://localhost:8080',
  96. // LED
  97. '/get_led_config': 'http://localhost:8080',
  98. '/set_led_config': 'http://localhost:8080',
  99. },
  100. },
  101. build: {
  102. outDir: '../static/dist',
  103. emptyOutDir: true,
  104. },
  105. })