-
Notifications
You must be signed in to change notification settings - Fork 6
Expand file tree
/
Copy pathvite.config.mjs
More file actions
142 lines (122 loc) Β· 3.49 KB
/
vite.config.mjs
File metadata and controls
142 lines (122 loc) Β· 3.49 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
import { defineConfig, loadEnv } from 'vite';
import react from '@vitejs/plugin-react';
import tsconfigPaths from 'vite-tsconfig-paths';
import monacoEditorPlugin from 'vite-plugin-monaco-editor';
import path from 'path';
import svgr from '@svgr/rollup';
import buildDashboardConfig from './server/dashboardConfig.mjs';
/**
* Utils
*/
const resolvePlugin = plugin => (plugin?.default ? plugin.default : plugin);
const toBoolean = value => {
if (typeof value === 'string') {
return value.toLowerCase() === 'true';
}
return Boolean(value);
};
/**
* π DEV-ONLY runtime endpoint
*/
const dashboardConfigPlugin = {
name: 'dashboard-config-endpoint',
configureServer(server) {
server.middlewares.use('/dashboard-config.json', (req, res) => {
const config = buildDashboardConfig();
res.statusCode = 200;
res.setHeader('Content-Type', 'application/json');
res.end(JSON.stringify({ config }));
});
},
};
export default defineConfig(({ mode }) => {
const env = loadEnv(mode, process.cwd(), '');
const isSecure = toBoolean(env.isSecure || process.env.isSecure);
const backendHost =
env.API_SERVER_BACKEND_HOST || process.env.API_SERVER_BACKEND_HOST;
const backendPort =
env.API_SERVER_BACKEND_PORT || process.env.API_SERVER_BACKEND_PORT;
const backendSchema =
env.API_SERVER_BACKEND_SCHEMA || process.env.API_SERVER_BACKEND_SCHEMA;
const backendOrigin = backendHost
? `${backendSchema || (isSecure ? 'https' : 'http')}://${backendHost}${
backendPort ? `:${backendPort}` : ''
}`
: null;
const apiPath =
env.API_SERVER_BACKEND_PATH ||
process.env.API_SERVER_BACKEND_PATH ||
'/hkube/api-server';
const socketPath =
env.API_SERVER_BACKEND_PATH_SOCKETIO ||
process.env.API_SERVER_BACKEND_PATH_SOCKETIO ||
'/hkube/monitor-server/socket.io';
const monacoPlugin = resolvePlugin(monacoEditorPlugin);
const svgrPlugin = resolvePlugin(svgr);
return {
define: {
global: 'globalThis',
},
base: './',
plugins: [
dashboardConfigPlugin,
react({
include: ['**/*.jsx', '**/*.tsx', '**/*.js', '**/*.ts'],
}),
svgrPlugin ? svgrPlugin({ include: '**/*.svg' }) : null,
tsconfigPaths(),
monacoPlugin ? monacoPlugin({}) : null,
].filter(Boolean),
envPrefix: ['VITE_', 'REACT_APP_'],
resolve: {
alias: {
src: path.resolve(process.cwd(), 'src'),
},
},
publicDir: 'public',
esbuild: {
loader: 'jsx',
include: /src\/.*\.jsx?$/,
},
build: {
outDir: 'build',
sourcemap: true,
emptyOutDir: true,
assetInlineLimit: 0,
rollupOptions: {
output: {
manualChunks: {
react: ['react', 'react-dom'],
monaco: ['monaco-editor'],
graph: ['react-graph-vis'],
},
},
},
},
server: {
host: true,
port: Number(process.env.PORT) || 9050,
proxy: backendOrigin
? {
[apiPath]: {
target: backendOrigin,
changeOrigin: true,
secure: isSecure,
ws: true,
},
[socketPath]: {
target: backendOrigin,
changeOrigin: true,
secure: isSecure,
ws: true,
},
}
: undefined,
},
transformIndexHtml: {
enforce: 'pre',
transform: (html, ctx) =>
ctx?.server ? html.replace(/__BASE_URL_TOKEN__/g, '/') : html,
},
};
});