vite.config.ts 3.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798
  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. // API endpoints - proxy all backend routes
  56. '/send_home': 'http://localhost:8080',
  57. '/send_coordinate': 'http://localhost:8080',
  58. '/stop_execution': 'http://localhost:8080',
  59. '/move_to_center': 'http://localhost:8080',
  60. '/move_to_perimeter': 'http://localhost:8080',
  61. '/set_speed': 'http://localhost:8080',
  62. '/run_theta_rho': 'http://localhost:8080',
  63. '/pause_execution': 'http://localhost:8080',
  64. '/resume_execution': 'http://localhost:8080',
  65. '/skip_pattern': 'http://localhost:8080',
  66. '/serial_status': 'http://localhost:8080',
  67. '/list_serial_ports': 'http://localhost:8080',
  68. '/connect': 'http://localhost:8080',
  69. '/disconnect': 'http://localhost:8080',
  70. '/get_speed': 'http://localhost:8080',
  71. '/list_theta_rho_files': 'http://localhost:8080',
  72. '/list_theta_rho_files_with_metadata': 'http://localhost:8080',
  73. '/list_all_playlists': 'http://localhost:8080',
  74. '/get_playlist': 'http://localhost:8080',
  75. '/create_playlist': 'http://localhost:8080',
  76. '/modify_playlist': 'http://localhost:8080',
  77. '/delete_playlist': 'http://localhost:8080',
  78. '/rename_playlist': 'http://localhost:8080',
  79. '/run_playlist': 'http://localhost:8080',
  80. '/add_to_playlist': 'http://localhost:8080',
  81. '/preview_thr': 'http://localhost:8080',
  82. '/preview_thr_batch': 'http://localhost:8080',
  83. '/preview': 'http://localhost:8080',
  84. '/get_theta_rho_coordinates': 'http://localhost:8080',
  85. '/get_led_config': 'http://localhost:8080',
  86. '/set_led_config': 'http://localhost:8080',
  87. '/api': 'http://localhost:8080',
  88. '/static': 'http://localhost:8080',
  89. },
  90. },
  91. build: {
  92. outDir: '../static/dist',
  93. emptyOutDir: true,
  94. },
  95. })