vite.config.ts 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114
  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. host: true, // Listen on all interfaces (0.0.0.0) for mobile/network access
  15. allowedHosts: true, // Allow all hosts for local network development
  16. proxy: {
  17. // WebSocket endpoints
  18. '/ws': {
  19. target: 'ws://localhost:8080',
  20. ws: true,
  21. // Suppress connection errors (common during backend restarts)
  22. configure: (proxy, _options) => {
  23. // Handle proxy errors silently for expected connection issues
  24. const isConnectionError = (err: Error & { code?: string }) => {
  25. const msg = err.message || ''
  26. const code = err.code || ''
  27. // Check error code (most reliable for AggregateError)
  28. if (['ECONNRESET', 'ECONNREFUSED', 'EPIPE', 'ETIMEDOUT'].includes(code)) {
  29. return true
  30. }
  31. // Check message as fallback
  32. if (msg.includes('ECONNRESET') || msg.includes('ECONNREFUSED') ||
  33. msg.includes('EPIPE') || msg.includes('ETIMEDOUT') ||
  34. msg.includes('AggregateError')) {
  35. return true
  36. }
  37. return false
  38. }
  39. const handleError = (err: Error) => {
  40. if (isConnectionError(err)) {
  41. return // Silently ignore connection errors
  42. }
  43. // Only log unexpected errors
  44. console.error('WebSocket proxy error:', err.message)
  45. }
  46. proxy.on('error', handleError)
  47. proxy.on('proxyReqWs', (_proxyReq, _req, socket) => {
  48. socket.on('error', (err) => {
  49. if (!isConnectionError(err)) {
  50. console.error('WebSocket socket error:', err.message)
  51. }
  52. })
  53. })
  54. },
  55. },
  56. // All /api endpoints
  57. '/api': 'http://localhost:8080',
  58. // Static assets
  59. '/static': 'http://localhost:8080',
  60. // Preview images
  61. '/preview': 'http://localhost:8080',
  62. // Legacy root-level API endpoints (for backwards compatibility)
  63. // Pattern execution
  64. '/send_home': 'http://localhost:8080',
  65. '/send_coordinate': 'http://localhost:8080',
  66. '/stop_execution': 'http://localhost:8080',
  67. '/force_stop': 'http://localhost:8080',
  68. '/soft_reset': 'http://localhost:8080',
  69. '/pause_execution': 'http://localhost:8080',
  70. '/resume_execution': 'http://localhost:8080',
  71. '/skip_pattern': 'http://localhost:8080',
  72. '/reorder_playlist': 'http://localhost:8080',
  73. '/add_to_queue': 'http://localhost:8080',
  74. '/run_theta_rho': 'http://localhost:8080',
  75. '/run_playlist': 'http://localhost:8080',
  76. // Movement
  77. '/move_to_center': 'http://localhost:8080',
  78. '/move_to_perimeter': 'http://localhost:8080',
  79. // Speed
  80. '/set_speed': 'http://localhost:8080',
  81. '/get_speed': 'http://localhost:8080',
  82. // Connection
  83. '/serial_status': 'http://localhost:8080',
  84. '/list_serial_ports': 'http://localhost:8080',
  85. '/connect': 'http://localhost:8080',
  86. '/disconnect': 'http://localhost:8080',
  87. // Patterns
  88. '/list_theta_rho_files': 'http://localhost:8080',
  89. '/list_theta_rho_files_with_metadata': 'http://localhost:8080',
  90. '/preview_thr': 'http://localhost:8080',
  91. '/preview_thr_batch': 'http://localhost:8080',
  92. '/get_theta_rho_coordinates': 'http://localhost:8080',
  93. '/delete_theta_rho_file': 'http://localhost:8080',
  94. // Playlists
  95. '/list_all_playlists': 'http://localhost:8080',
  96. '/get_playlist': 'http://localhost:8080',
  97. '/create_playlist': 'http://localhost:8080',
  98. '/modify_playlist': 'http://localhost:8080',
  99. '/delete_playlist': 'http://localhost:8080',
  100. '/rename_playlist': 'http://localhost:8080',
  101. '/add_to_playlist': 'http://localhost:8080',
  102. // LED
  103. '/get_led_config': 'http://localhost:8080',
  104. '/set_led_config': 'http://localhost:8080',
  105. },
  106. },
  107. build: {
  108. outDir: '../static/dist',
  109. emptyOutDir: true,
  110. },
  111. })