vite.config.js 3.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495
  1. import path from 'path'
  2. import { defineConfig, loadEnv } from 'vite'
  3. import react from '@vitejs/plugin-react'
  4. import { fileURLToPath } from 'url'
  5. import { getConnectableHost, normalizeLoopbackHost } from './shared/networkHosts.js'
  6. const __filename = fileURLToPath(import.meta.url)
  7. const __dirname = path.dirname(__filename)
  8. const repoRoot = path.resolve(__dirname, '..')
  9. export default defineConfig(({ mode }) => {
  10. // Load the single root .env and let exported shell vars override file values.
  11. const env = {
  12. ...loadEnv(mode, repoRoot, ''),
  13. ...process.env,
  14. }
  15. const configuredHost = env.HOST || '0.0.0.0'
  16. // if the host is not a loopback address, it should be used directly.
  17. // This allows the vite server to EXPOSE all interfaces when the host
  18. // is set to '0.0.0.0' or '::', while still using 'localhost' for browser
  19. // URLs and proxy targets.
  20. const host = normalizeLoopbackHost(configuredHost)
  21. const proxyHost = env.PROXY_HOST || getConnectableHost(configuredHost)
  22. // TODO: Remove support for legacy PORT variables in all locations in a future major release, leaving only SERVER_PORT.
  23. const serverPort = env.SERVER_PORT || env.PORT || 3001
  24. const localNodeModules = (...segments) =>
  25. path.resolve(process.cwd(), 'node_modules', ...segments)
  26. const disableLocalAuth =
  27. env.PILOTDECK_DISABLE_LOCAL_AUTH !== '0' &&
  28. env.PILOTDECK_DISABLE_LOCAL_AUTH !== 'false'
  29. return {
  30. define: {
  31. 'import.meta.env.VITE_DISABLE_LOCAL_AUTH': JSON.stringify(disableLocalAuth ? 'true' : 'false'),
  32. },
  33. plugins: [react()],
  34. resolve: {
  35. alias: {
  36. react: localNodeModules('react'),
  37. 'react-dom': localNodeModules('react-dom'),
  38. 'react/jsx-runtime': localNodeModules('react', 'jsx-runtime.js'),
  39. 'react/jsx-dev-runtime': localNodeModules('react', 'jsx-dev-runtime.js'),
  40. }
  41. },
  42. server: {
  43. host,
  44. port: parseInt(env.VITE_PORT) || 5173,
  45. proxy: {
  46. '/api': `http://${proxyHost}:${serverPort}`,
  47. '/memory-dashboard': `http://${proxyHost}:${serverPort}`,
  48. '/ws': {
  49. target: `ws://${proxyHost}:${serverPort}`,
  50. ws: true
  51. },
  52. '/shell': {
  53. target: `ws://${proxyHost}:${serverPort}`,
  54. ws: true
  55. }
  56. }
  57. },
  58. build: {
  59. outDir: 'dist',
  60. chunkSizeWarningLimit: 1000,
  61. rollupOptions: {
  62. output: {
  63. manualChunks: {
  64. 'vendor-react': ['react', 'react-dom', 'react-router-dom'],
  65. 'vendor-codemirror': [
  66. '@uiw/react-codemirror',
  67. '@codemirror/lang-css',
  68. '@codemirror/lang-html',
  69. '@codemirror/lang-javascript',
  70. '@codemirror/lang-json',
  71. '@codemirror/lang-markdown',
  72. '@codemirror/lang-python',
  73. '@codemirror/theme-one-dark'
  74. ],
  75. 'vendor-xterm': ['@xterm/xterm', '@xterm/addon-fit', '@xterm/addon-clipboard', '@xterm/addon-webgl']
  76. }
  77. }
  78. }
  79. },
  80. test: {
  81. environment: 'jsdom',
  82. server: {
  83. deps: {
  84. inline: ['react', 'react-dom']
  85. }
  86. }
  87. }
  88. }
  89. })