-
Notifications
You must be signed in to change notification settings - Fork 64
/
Copy pathvite.config.ts
184 lines (176 loc) · 5.09 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
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
import { defineConfig, loadEnv, PluginOption, ProxyOptions } from "vite";
import react from "@vitejs/plugin-react";
import { visualizer } from "rollup-plugin-visualizer";
import svgr from "vite-plugin-svgr";
import { resolve } from "path";
import fs from "fs";
/**
* Get basename for React router.
*
* @param {[Record<string, string>]} environment
*
* Picks first encountered value:
* 1. $VITE_ROUTER_BASENAME
* 2. $BASE_URL
* 3. undefined (default)
*/
function getRouterBasename(
environment: Record<string, string>
): string | undefined {
return environment.VITE_ROUTER_BASENAME ?? environment.BASE_URL ?? undefined;
}
/**
* Get base url for Klaw API client.
*
* @param {[type]} environment
*
* The $VITE_API_BASE_URL variable allows API to be consumed from another origin.
* Also, this make it easy to override the API base url for unittests.
*/
function getApiBaseUrl(
environment: Record<string, string>
): string | undefined {
return environment.VITE_API_BASE_URL ?? undefined;
}
/**
* Get development server HTTPS config.
*
* @param {[type]} environment
*
* run development server in HTTP mode if is $VITE_SERVER_CERTIFICATE_PATH
* and $VITE_SERVER_CERTIFICATE_KEY_PATH are defined. This is needed when
* using a remote backend that is running under HTTPS.
*/
function getServerHTTPSConfig(
environment: Record<string, string>
): false | { key: Buffer; cert: Buffer } {
if (
environment.VITE_SERVER_CERTIFICATE_PATH &&
environment.VITE_SERVER_CERTIFICATE_KEY_PATH
) {
return {
key: fs.readFileSync(environment.VITE_SERVER_CERTIFICATE_KEY_PATH),
cert: fs.readFileSync(environment.VITE_SERVER_CERTIFICATE_PATH),
};
}
return false;
}
/**
* Get development server Klaw API proxy target.
*
* @param {[type]} environment
*
* Use $VITE_PROXY_TARGET or Klaw API development default (http://localhost:9097)
*/
function getProxyTarget(environment: Record<string, string>): string {
const origin = environment.VITE_PROXY_TARGET ?? "http://localhost:9097";
return `${new URL(origin).origin}`;
}
/**
* Get development server Klaw API proxy target.
*
* @param {[type]} environment
*
* Use $VITE_PROXY_TARGET or Klaw API development default (http://localhost:9097)
*/
function getServerProxyConfig(
environment: Record<string, string>
): Record<string, string | ProxyOptions> | undefined {
const LEGACY_LOGIN_RESOURCES = [
"/login",
"/lib/angular.min.js",
"/lib/angular-route.min.js",
"/assets/css/",
"/assets/js/",
"/assets/plugins/",
"/assets/images/",
];
const target = getProxyTarget(environment);
const secure = false;
return {
"/api": {
target,
rewrite: (path) => path.replace(/^\/api/, ""),
secure,
},
...LEGACY_LOGIN_RESOURCES.reduce(
(acc, current) => ({
...acc,
[current]: {
target,
secure,
},
}),
{}
),
};
}
function getPlugins(environment: Record<string, string>): PluginOption[] {
const plugins: PluginOption[] = [react(), svgr()];
if (environment.BUNDLE_ANALYZE) {
plugins.push(visualizer());
}
return plugins;
}
// https://vitejs.dev/config/
export default defineConfig(({ mode }) => {
const environment = loadEnv(mode, process.cwd(), "");
const usesNodeProxy = mode === "local-api";
console.log("mode", mode);
return {
plugins: getPlugins(environment),
define: {
// Vite does not use process.env (see https://vitejs.dev/guide/env-and-mode.html).
// If a library depends on process.env (like "@aivenio/aquarium").
// ⛔ Note: there are stackoverflow answers / GitHub issues that recommend e.g
// ⛔ 'process.env': process.env or
// ⛔ 'process.env': { ...process.env}
// ⛔️ Don't do that! This can expose unwanted env vars in production builds.
"process.env": {
ROUTER_BASENAME: getRouterBasename(environment),
API_BASE_URL: getApiBaseUrl(environment),
FEATURE_FLAG_ADD_CLUSTER: ["development", "remote-api"]
.includes(mode)
.toString(),
},
},
css: {
modules: {
localsConvention: "camelCaseOnly",
},
},
resolve: {
alias: {
src: resolve(resolve(__dirname), "./src"),
},
},
server: {
port: 5173,
// mode local-api is used in our node proxy,
// - no need to run a second proxy in that case
// - api runs on http:// not https://
https: usesNodeProxy ? null : getServerHTTPSConfig(environment),
proxy: usesNodeProxy ? null : getServerProxyConfig(environment),
},
preview: {
port: 5173,
https: getServerHTTPSConfig(environment),
proxy: getServerProxyConfig(environment),
},
build: {
rollupOptions: {
output: {
manualChunks: (id: string) => {
if (id.includes("node_modules")) {
return "vendor";
}
},
},
},
},
// By setting the base to /coral for nodeProxy mode
// we get the same behaviour as on production mode
// were coral is deployed in directory `/coral`
base: usesNodeProxy ? "/coral/" : "/",
};
});