-
Notifications
You must be signed in to change notification settings - Fork 596
/
vite.config.ts
81 lines (71 loc) · 2.12 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
import { defineConfig, loadEnv } from 'vite';
import RubyPlugin from 'vite-plugin-ruby';
import inject from '@rollup/plugin-inject';
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()) }
// Sets VITE_INDEX_API_URL which is templated in index.html
process.env.VITE_INDEX_API_URL = command === 'serve' ? '/' : process.env.VITE_PORTAL_API_URL
// Defaults locale to en
process.env.VITE_LOCALE = process.env.VITE_LOCALE || 'en'
let portalApiUrl = process.env.VITE_PORTAL_API_URL
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'),
},
build: {
commonjsOptions: {
include: [
/@kong\/kongponents/,
/node_modules/
]
},
},
plugins: [
inject({
$: 'jquery',
jQuery: 'jquery',
}),
RubyPlugin(),
vue()
],
server: {
proxy: {
'^/api': {
changeOrigin: true,
target: portalApiUrl,
configure: (proxy, options) => {
mutateCookieAttributes(proxy)
setHostHeader(proxy)
}
}
}
}
})
}