aboutsummaryrefslogtreecommitdiffstats
path: root/subprojects/frontend/config/detectDevModeOptions.ts
diff options
context:
space:
mode:
authorLibravatar Kristóf Marussy <kristof@marussy.com>2022-12-09 23:49:07 +0100
committerLibravatar Kristóf Marussy <kristof@marussy.com>2022-12-11 12:18:43 +0100
commit280a3fab74348697429b7bab56b3436968113d79 (patch)
treee1603153db18f7f35c1bcceb03462409f75002db /subprojects/frontend/config/detectDevModeOptions.ts
parentrefactor(frontend): lazy load XtextClient (diff)
downloadrefinery-280a3fab74348697429b7bab56b3436968113d79.tar.gz
refinery-280a3fab74348697429b7bab56b3436968113d79.tar.zst
refinery-280a3fab74348697429b7bab56b3436968113d79.zip
refactor(frontend): split vite config
Also introduces tsconfig.shared.json to keep track of source files used both and build time and in the browser.
Diffstat (limited to 'subprojects/frontend/config/detectDevModeOptions.ts')
-rw-r--r--subprojects/frontend/config/detectDevModeOptions.ts94
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 @@
1import type { PluginOption, ServerOptions } from 'vite';
2
3import backendConfigVitePlugin, {
4 type BackendConfig,
5} from './backendConfigVitePlugin';
6
7export const API_ENDPOINT = 'xtext-service';
8
9export interface DevModeOptions {
10 mode: string;
11 isDevelopment: boolean;
12 devModePlugins: PluginOption[];
13 serverOptions: ServerOptions;
14}
15
16interface ListenOptions {
17 host: string;
18 port: number;
19 secure: boolean;
20}
21
22function 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
34function listenURL(
35 { host, port, secure }: ListenOptions,
36 protocol = 'http',
37): string {
38 return `${secure ? `${protocol}s` : protocol}://${host}:${port}`;
39}
40
41export 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}