diff options
Diffstat (limited to 'subprojects/frontend/config/detectDevModeOptions.ts')
-rw-r--r-- | subprojects/frontend/config/detectDevModeOptions.ts | 94 |
1 files changed, 94 insertions, 0 deletions
diff --git a/subprojects/frontend/config/detectDevModeOptions.ts b/subprojects/frontend/config/detectDevModeOptions.ts new file mode 100644 index 00000000..b3696241 --- /dev/null +++ b/subprojects/frontend/config/detectDevModeOptions.ts | |||
@@ -0,0 +1,94 @@ | |||
1 | import type { PluginOption, ServerOptions } from 'vite'; | ||
2 | |||
3 | import backendConfigVitePlugin, { | ||
4 | type BackendConfig, | ||
5 | } from './backendConfigVitePlugin'; | ||
6 | |||
7 | export const API_ENDPOINT = 'xtext-service'; | ||
8 | |||
9 | export interface DevModeOptions { | ||
10 | mode: string; | ||
11 | isDevelopment: boolean; | ||
12 | devModePlugins: PluginOption[]; | ||
13 | serverOptions: ServerOptions; | ||
14 | } | ||
15 | |||
16 | interface ListenOptions { | ||
17 | host: string; | ||
18 | port: number; | ||
19 | secure: boolean; | ||
20 | } | ||
21 | |||
22 | function detectListenOptions( | ||
23 | name: string, | ||
24 | fallbackHost: string, | ||
25 | fallbackPort: number, | ||
26 | ): ListenOptions { | ||
27 | const host = process.env[`${name}_HOST`] ?? fallbackHost; | ||
28 | const rawPort = process.env[`${name}_PORT`]; | ||
29 | const port = rawPort === undefined ? fallbackPort : parseInt(rawPort, 10); | ||
30 | const secure = port === 443; | ||
31 | return { host, port, secure }; | ||
32 | } | ||
33 | |||
34 | function listenURL( | ||
35 | { host, port, secure }: ListenOptions, | ||
36 | protocol = 'http', | ||
37 | ): string { | ||
38 | return `${secure ? `${protocol}s` : protocol}://${host}:${port}`; | ||
39 | } | ||
40 | |||
41 | export default function detectDevModeOptions(): DevModeOptions { | ||
42 | const mode = process.env['MODE'] || 'development'; | ||
43 | const isDevelopment = mode === 'development'; | ||
44 | |||
45 | if (!isDevelopment) { | ||
46 | return { | ||
47 | mode, | ||
48 | isDevelopment, | ||
49 | devModePlugins: [], | ||
50 | serverOptions: {}, | ||
51 | }; | ||
52 | } | ||
53 | |||
54 | const listen = detectListenOptions('LISTEN', 'localhost', 1313); | ||
55 | // Make sure we always use IPv4 to connect to the backend, | ||
56 | // because it doesn't listen on IPv6. | ||
57 | const api = detectListenOptions('API', '127.0.0.1', 1312); | ||
58 | const publicAddress = detectListenOptions('PUBLIC', listen.host, listen.port); | ||
59 | |||
60 | const backendConfig: BackendConfig = { | ||
61 | webSocketURL: `${listenURL(publicAddress, 'ws')}/${API_ENDPOINT}`, | ||
62 | }; | ||
63 | |||
64 | return { | ||
65 | mode, | ||
66 | isDevelopment, | ||
67 | devModePlugins: [backendConfigVitePlugin(backendConfig)], | ||
68 | serverOptions: { | ||
69 | host: listen.host, | ||
70 | port: listen.port, | ||
71 | strictPort: true, | ||
72 | https: listen.secure, | ||
73 | headers: { | ||
74 | // Enable strict origin isolation, see e.g., | ||
75 | // https://github.com/vitejs/vite/issues/3909#issuecomment-1065893956 | ||
76 | 'Cross-Origin-Opener-Policy': 'same-origin', | ||
77 | 'Cross-Origin-Embedder-Policy': 'require-corp', | ||
78 | 'Cross-Origin-Resource-Policy': 'cross-origin', | ||
79 | }, | ||
80 | proxy: { | ||
81 | [`/${API_ENDPOINT}`]: { | ||
82 | target: listenURL(api), | ||
83 | ws: true, | ||
84 | secure: api.secure, | ||
85 | }, | ||
86 | }, | ||
87 | hmr: { | ||
88 | host: publicAddress.host, | ||
89 | clientPort: publicAddress.port, | ||
90 | path: '/vite', | ||
91 | }, | ||
92 | }, | ||
93 | }; | ||
94 | } | ||