-
Notifications
You must be signed in to change notification settings - Fork 109
Expand file tree
/
Copy pathvite.config.ts
More file actions
229 lines (212 loc) · 6.99 KB
/
Copy pathvite.config.ts
File metadata and controls
229 lines (212 loc) · 6.99 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
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
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
import tailwindcss from '@tailwindcss/vite'
import react from '@vitejs/plugin-react'
import fs from 'node:fs/promises'
import path from 'node:path'
import { type ServerOptions, type UserConfig, defineConfig } from 'vite'
const BACKEND_JOINMARKET_CLIENTSERVER_NATIVE = 'joinmarket-clientserver'
const BACKEND_JOINMARKET_NG_NATIVE = 'joinmarket-ng'
const BACKEND_JAM_STANDALONE = 'jam-standalone'
type SupportedBackend =
typeof BACKEND_JOINMARKET_CLIENTSERVER_NATIVE | typeof BACKEND_JOINMARKET_NG_NATIVE | typeof BACKEND_JAM_STANDALONE
const SUPPORTED_BACKENDS: SupportedBackend[] = [
BACKEND_JOINMARKET_CLIENTSERVER_NATIVE,
BACKEND_JOINMARKET_NG_NATIVE,
BACKEND_JAM_STANDALONE,
]
function isSupportedBackend(val: unknown): val is SupportedBackend {
return SUPPORTED_BACKENDS.includes(val as SupportedBackend) === true
}
type BackendConfigEnvironmentVariables = {
JMWALLETD_API_PORT: number
JMWALLETD_WEBSOCKET_PORT: number
JMOBWATCH_PORT: number
JAM_API_PORT?: number // (optioal) Jam specific API endpoint (only present in jam-standalone)
}
const {
//PUBLIC_URL = '', // TODO: support serving from non-root?
JAM_BACKEND = BACKEND_JOINMARKET_CLIENTSERVER_NATIVE,
JAM_API_PORT = undefined,
} = process.env
const BACKEND_ENV_JOINMARKET_CLIENTSERVER_DEFAULT: BackendConfigEnvironmentVariables = {
JMWALLETD_API_PORT: 28183,
JMWALLETD_WEBSOCKET_PORT: 28283,
JMOBWATCH_PORT: 62601,
}
const BACKEND_ENV_JOINMARKET_NG_DEFAULT: BackendConfigEnvironmentVariables = {
JMWALLETD_API_PORT: 28183,
JMWALLETD_WEBSOCKET_PORT: 28183,
JMOBWATCH_PORT: 8000,
}
const createJamStandloneConfigEnvironmentVariables = (jamApiPort: number): BackendConfigEnvironmentVariables => ({
JMWALLETD_API_PORT: jamApiPort,
JMWALLETD_WEBSOCKET_PORT: jamApiPort,
JMOBWATCH_PORT: jamApiPort,
JAM_API_PORT: jamApiPort,
})
// https://vite.dev/config/
export default defineConfig((config): UserConfig => {
if (!isSupportedBackend(JAM_BACKEND)) {
throw new Error(`Unsupported backend: Use one of [${SUPPORTED_BACKENDS.join(', ')}]`)
}
const server = createServer(JAM_BACKEND)
const buildOrPreview = config.command === 'build' || config.isPreview === true
return {
plugins: [
react(),
tailwindcss({ optimize: buildOrPreview }),
{
name: 'delete-service-worker',
async writeBundle() {
await fs.rm('dist/mockServiceWorker.js', { force: true })
},
},
],
resolve: {
alias: {
'@': path.resolve(__dirname, './src'),
},
},
server: {
...server,
open: true,
},
}
})
const createServer = (backend: SupportedBackend): ServerOptions => {
switch (backend) {
case BACKEND_JOINMARKET_CLIENTSERVER_NATIVE: {
return createServerConfigJoinmarketClientServerNative({
...BACKEND_ENV_JOINMARKET_CLIENTSERVER_DEFAULT,
...process.env,
})
}
case BACKEND_JOINMARKET_NG_NATIVE: {
return createServerConfigJoinmarketNgNative({
...BACKEND_ENV_JOINMARKET_NG_DEFAULT,
...process.env,
})
}
case BACKEND_JAM_STANDALONE: {
if (JAM_API_PORT === undefined) {
throw new Error('Unsupported port: Please specify a valid JAM_API_PORT')
}
return createServerConfigJamStandalone({
...createJamStandloneConfigEnvironmentVariables(Number(JAM_API_PORT)),
...process.env,
})
}
// No default
}
throw new Error(`Unsupported backend: Use one of [${SUPPORTED_BACKENDS.join(', ')}]`)
}
/**
* Server config for "joinmarket-clientserver".
* The "native" installation *does not run* a webserver!
* Requests must be adapted:
* - proxy API requests to correct target service
* - rewrite paths to match target service paths
* - translate header "x-jm-authorization" to "Authorization"
*/
const createServerConfigJoinmarketClientServerNative = (config: BackendConfigEnvironmentVariables): ServerOptions => {
return {
proxy: {
'/api': {
target: `https://127.0.0.1:${config.JMWALLETD_API_PORT}`,
changeOrigin: true,
secure: false,
configure: (proxy, _options) => {
proxy.on('proxyReq', (proxyRequest, request, _response) => {
if (request.headers['x-jm-authorization']) {
proxyRequest.setHeader('Authorization', request.headers['x-jm-authorization'])
}
})
},
},
'/obwatch': {
target: `http://127.0.0.1:${config.JMOBWATCH_PORT}`,
changeOrigin: true,
secure: false,
rewrite: (p) => p.replace(/^\/obwatch/, ''),
},
'/jmws': {
target: `https://127.0.0.1:${config.JMWALLETD_WEBSOCKET_PORT}`,
changeOrigin: true,
secure: false,
ws: true,
rewrite: (p) => p.replace(/^\/jmws/, ''),
},
},
}
}
/**
* Server config for "joinmarket-ng".
* Similar to `createServerConfigJoinmarketClientServerNative`, but own function to keep concerns seperated:
* Changes are expected as joinmarket-ng keeps maturing.
*/
const createServerConfigJoinmarketNgNative = (config: BackendConfigEnvironmentVariables): ServerOptions => {
return {
proxy: {
'/api': {
target: `https://127.0.0.1:${config.JMWALLETD_API_PORT}`,
changeOrigin: true,
secure: false,
configure: (proxy, _options) => {
proxy.on('proxyReq', (proxyRequest, request, _response) => {
if (request.headers['x-jm-authorization']) {
proxyRequest.setHeader('Authorization', request.headers['x-jm-authorization'])
}
})
},
},
'/obwatch': {
target: `http://127.0.0.1:${config.JMOBWATCH_PORT}`,
changeOrigin: true,
secure: false,
rewrite: (p) => p.replace(/^\/obwatch/, ''),
},
'/jmws': {
target: `https://127.0.0.1:${config.JMWALLETD_WEBSOCKET_PORT}`,
changeOrigin: true,
secure: false,
ws: true,
rewrite: (p) => p.replace(/^\/jmws/, '/api/v1/ws'),
},
},
}
}
/**
* Server config for "jam-standalone" (Jam Docker image).
* `standalone` backend has a webserver ("Jam API") running and provides a normalized API for the underlying backend.
* Requests must be adapted:
* - proxy all API requests to "Jam API"
*/
const createServerConfigJamStandalone = (config: BackendConfigEnvironmentVariables): ServerOptions => {
if (config.JAM_API_PORT === undefined) {
throw new Error('Unsupported port: Please specify a valid JAM_API_PORT')
}
return {
proxy: {
'/api': {
target: `http://127.0.0.1:${config.JMWALLETD_API_PORT}`,
changeOrigin: true,
secure: false,
},
'/obwatch': {
target: `http://127.0.0.1:${config.JMOBWATCH_PORT}`,
changeOrigin: true,
secure: false,
},
'/jmws': {
target: `http://127.0.0.1:${config.JMWALLETD_WEBSOCKET_PORT}`,
changeOrigin: true,
secure: false,
ws: true,
},
'/jam': {
target: `http://127.0.0.1:${config.JAM_API_PORT}`,
changeOrigin: true,
secure: false,
},
},
}
}