forked from deephaven/web-client-ui
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathvite.config.ts
160 lines (151 loc) · 4.79 KB
/
vite.config.ts
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
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
// This is a dev dependency for building, so importing dev deps is fine
/* eslint-disable import/no-extraneous-dependencies */
import { defineConfig, loadEnv } from 'vite';
import browserslistToEsbuild from 'browserslist-to-esbuild';
import react from '@vitejs/plugin-react-swc';
import path from 'path';
// https://vitejs.dev/config/
export default defineConfig(({ mode }) => {
const env = loadEnv(mode, process.cwd(), '');
const packagesDir = path.resolve(__dirname, '..');
let port = Number.parseInt(env.PORT, 10);
if (Number.isNaN(port) || port <= 0) {
port = 4000;
}
const baseURL = new URL(env.BASE_URL, `http://localhost:${port}/`);
// These are paths which should be proxied to the core server
// https://vitejs.dev/config/server-options.html#server-proxy
const proxy = {
// proxy the websocket requests, allows tunneling to work with a single port
'^/arrow\\.*': {
target: env.VITE_PROXY_URL,
changeOrigin: true,
ws: true,
},
'^/io\\.deephaven\\..*': {
target: env.VITE_PROXY_URL,
changeOrigin: true,
ws: true,
},
};
if (env.VITE_PROXY_URL) {
// Some paths need to proxy to the engine server
// Vite does not have a "any unknown fallback to proxy" like CRA
// It is possible to add one with a custom middleware though if this list grows
[env.VITE_CORE_API_URL, env.VITE_MODULE_PLUGINS_URL].forEach(p => {
const route = new URL(p, baseURL).pathname;
proxy[route] = {
target: env.VITE_PROXY_URL,
changeOrigin: true,
};
});
// Proxy deep-linking routes to the base itself
// Need to add for each deep-linking route
[env.VITE_ROUTE_NOTEBOOKS].forEach(p => {
const route = new URL(p, baseURL).pathname;
proxy[`^${route}`] = {
target: baseURL.toString(),
rewrite: () => '',
};
});
}
// Proxy to local dev server for js-plugins
if (env.VITE_JS_PLUGINS_DEV_PORT && env.VITE_MODULE_PLUGINS_URL) {
const route = new URL(env.VITE_MODULE_PLUGINS_URL, baseURL).pathname;
proxy[route] = {
target: `http://localhost:${env.VITE_JS_PLUGINS_DEV_PORT}`,
changeOrigin: true,
rewrite: (pathOrig: string) => pathOrig.replace(/^\/js-plugins/, ''),
};
}
return {
// Vite does not read this env variable, it sets it based on the config
// For easy changes using our .env files, read it here and vite will just set it to the existing value
base: env.BASE_URL,
envPrefix: ['VITE_', 'npm_'], // Needed to use $npm_package_version
server: {
port,
open: true,
proxy,
},
preview: {
port,
open: true,
proxy,
},
resolve: {
dedupe: ['react', 'react-redux', 'redux'],
alias:
mode === 'development'
? [
{
find: /^@deephaven\/(.*)\/scss\/(.*)/,
replacement: `${packagesDir}/$1/scss/$2`,
},
{
find: /^@deephaven\/(?!icons|jsapi-types)(.*)/, // Icons package can not import from src
replacement: `${packagesDir}/$1/src`,
},
]
: [],
},
build: {
outDir: path.resolve(__dirname, env.VITE_BUILD_PATH),
emptyOutDir: true,
sourcemap: true,
build: {
target: browserslistToEsbuild(),
},
rollupOptions: {
output: {
manualChunks: id => {
/**
* Without this, our chunk order may cause a circular reference
* by putting the helpers in the vendor or plotly chunk
* This causes failures with loading the compiled version
*
* See https://github.com/rollup/plugins/issues/591
*/
if (id === '\0commonjsHelpers.js') {
return 'helpers';
}
if (id.includes('node_modules')) {
if (id.includes('monaco-editor')) {
return 'monaco';
}
if (id.includes('plotly.js')) {
return 'plotly';
}
if (id.includes('mathjax')) {
return 'mathjax';
}
return 'vendor';
}
},
},
},
},
optimizeDeps: {
exclude: ['@astral-sh/ruff-wasm-web'], // Needed so the wasm file works in dev mode
esbuildOptions: {
// Some packages need this to start properly if they reference global
define: {
global: 'globalThis',
},
},
},
css: {
devSourcemap: true,
},
plugins: [react()],
esbuild: {
/**
* Prevents ESBuild to throw when using a feature not supported by the
* list of supported browsers coming from the `browserslist` file.
*/
supported: {
'top-level-await': true,
},
},
};
});