-
Notifications
You must be signed in to change notification settings - Fork 1
/
vite.config.ts
74 lines (65 loc) · 1.97 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
import { defineConfig, loadEnv } from 'vite';
import RubyPlugin from 'vite-plugin-ruby';
import vue from '@vitejs/plugin-vue';
import dns from 'dns';
function mutateCookieAttributes (proxy) {
proxy.on('proxyRes', function (proxyRes, req, res) {
if (proxyRes.headers['set-cookie']) {
proxyRes.headers['set-cookie'] = (proxyRes.headers['set-cookie']).map(h => {
return h.replace(/Domain=.*;/, 'Domain=localhost; Secure;')
})
}
})
}
function setHostHeader (proxy) {
const host = new URL(process.env.VITE_PORTAL_API_URL).hostname;
proxy.on('proxyReq', function (proxyRes) {
proxyRes.setHeader('host', host)
})
}
export default ({ command, mode }) => {
process.env = { ...process.env, ...loadEnv(mode, process.cwd()) }
// Defaults locale to en
process.env.VITE_LOCALE = process.env.VITE_LOCALE || 'en'
let portalApiUrl = process.env.VITE_PORTAL_API_URL;
if (!portalApiUrl) {
console.error('\x1b[31m Missing `VITE_PORTAL_API_URL`, update your .env file or set it as an ENV variable. \x1b[0m');
process.exit(1);
}
if (!portalApiUrl.endsWith('/')) {
portalApiUrl += '/'
}
const subdomainR = new RegExp(/http:\/\/(.*)localhost/)
if (subdomainR.test(portalApiUrl)) {
portalApiUrl = 'http://localhost' + portalApiUrl.replace(subdomainR, '')
}
// required to prevent localhost from being rendered as 127.0.0.1
dns.setDefaultResultOrder('verbatim')
return defineConfig({
define: {
'process.env.development': JSON.stringify('development'),
'process.env.production': JSON.stringify('production'),
},
plugins: [
RubyPlugin(),
vue()
],
build: {
rollupOptions: {
external: ['shiki/onig.wasm']
}
},
server: {
proxy: {
'^/api': {
changeOrigin: true,
target: portalApiUrl,
configure: (proxy, options) => {
mutateCookieAttributes(proxy)
setHostHeader(proxy)
}
}
}
}
})
}