diff options
Diffstat (limited to 'subprojects/frontend/vite.config.ts')
-rw-r--r-- | subprojects/frontend/vite.config.ts | 92 |
1 files changed, 92 insertions, 0 deletions
diff --git a/subprojects/frontend/vite.config.ts b/subprojects/frontend/vite.config.ts new file mode 100644 index 00000000..9cb426cf --- /dev/null +++ b/subprojects/frontend/vite.config.ts | |||
@@ -0,0 +1,92 @@ | |||
1 | import { readFileSync } from 'node:fs'; | ||
2 | import path from 'node:path'; | ||
3 | import { fileURLToPath } from 'node:url'; | ||
4 | |||
5 | import { lezer } from '@lezer/generator/rollup'; | ||
6 | import react from '@vitejs/plugin-react'; | ||
7 | import { defineConfig } from 'vite'; | ||
8 | import injectPreload from 'vite-plugin-inject-preload'; | ||
9 | |||
10 | const thisDir = path.dirname(fileURLToPath(import.meta.url)); | ||
11 | |||
12 | const mode = process.env.MODE || 'development'; | ||
13 | const isDevelopment = mode === 'development'; | ||
14 | |||
15 | function portNumberOrElse(envName: string, fallback: number): number { | ||
16 | const value = process.env[envName]; | ||
17 | return value ? parseInt(value, 10) : fallback; | ||
18 | } | ||
19 | |||
20 | const listenHost = process.env.LISTEN_HOST || 'localhost'; | ||
21 | const listenPort = portNumberOrElse('LISTEN_PORT', 1313); | ||
22 | const apiHost = process.env.API_HOST || listenHost; | ||
23 | const apiPort = portNumberOrElse('API_PORT', 1312); | ||
24 | const apiSecure = apiPort === 443; | ||
25 | const publicHost = process.env.PUBLIC_HOST || listenHost; | ||
26 | const publicPort = portNumberOrElse('PUBLIC_PORT', listenPort); | ||
27 | |||
28 | const { name: packageName, version: packageVersion } = JSON.parse( | ||
29 | readFileSync(path.join(thisDir, 'package.json'), 'utf8'), | ||
30 | ) as { name: string; version: string }; | ||
31 | process.env.VITE_PACKAGE_NAME ??= packageName; | ||
32 | process.env.VITE_PACKAGE_VERSIOn ??= packageVersion; | ||
33 | |||
34 | export default defineConfig({ | ||
35 | logLevel: 'info', | ||
36 | mode, | ||
37 | root: thisDir, | ||
38 | cacheDir: path.join(thisDir, 'build/vite/cache'), | ||
39 | plugins: [ | ||
40 | react({ | ||
41 | babel: { | ||
42 | // Gets rid of deoptimization warnings for large chunks. | ||
43 | // We don't need to minify here, because the output of Babel | ||
44 | // will get passed to esbuild anyways. | ||
45 | compact: false, | ||
46 | minified: false, | ||
47 | }, | ||
48 | }), | ||
49 | injectPreload({ | ||
50 | files: [ | ||
51 | { | ||
52 | match: | ||
53 | /(?:jetbrains-mono-latin-variable-wghtOnly-(?:italic|normal)|roboto-latin-(400|500)-normal).+\.woff2/, | ||
54 | attributes: { | ||
55 | type: 'font/woff2', | ||
56 | as: 'font', | ||
57 | crossorigin: 'anonymous', | ||
58 | }, | ||
59 | }, | ||
60 | ], | ||
61 | }), | ||
62 | lezer(), | ||
63 | ], | ||
64 | base: '', | ||
65 | define: { | ||
66 | __DEV__: JSON.stringify(isDevelopment), // For MobX | ||
67 | }, | ||
68 | build: { | ||
69 | assetsDir: '.', | ||
70 | outDir: path.join('build/vite', mode), | ||
71 | emptyOutDir: true, | ||
72 | sourcemap: isDevelopment, | ||
73 | minify: !isDevelopment, | ||
74 | }, | ||
75 | server: { | ||
76 | host: listenHost, | ||
77 | port: listenPort, | ||
78 | strictPort: true, | ||
79 | proxy: { | ||
80 | '/xtext-service': { | ||
81 | target: `${apiSecure ? 'https' : 'http'}://${apiHost}:${apiPort}`, | ||
82 | ws: true, | ||
83 | secure: apiSecure, | ||
84 | }, | ||
85 | }, | ||
86 | hmr: { | ||
87 | host: publicHost, | ||
88 | clientPort: publicPort, | ||
89 | path: '/vite', | ||
90 | }, | ||
91 | }, | ||
92 | }); | ||