forked from Kong/konnect-portal
-
Notifications
You must be signed in to change notification settings - Fork 0
/
vite.config.js
135 lines (120 loc) · 3.97 KB
/
vite.config.js
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
/* eslint-disable no-console */
import { defineConfig, loadEnv, createLogger } from 'vite'
import dns from 'dns'
import vue from '@vitejs/plugin-vue'
import svgLoader from 'vite-svg-loader'
import vueJsx from '@vitejs/plugin-vue-jsx'
import { visualizer } from 'rollup-plugin-visualizer'
const path = require('path')
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)
})
}
/**
* Create a custom logger to ignore `vite:css` errors (from postcss) for imported packages
*/
function createCustomLogger () {
const logger = createLogger()
const loggerWarn = logger.warn
// Create array of partial message strings to ignore
const ignoredWarnings = [
'end value has mixed support'
]
logger.warn = (msg, options) => {
// if the msg includes `vite:css` and one of the `ignoredWarnings`, ignore and do not log
if (msg.includes('vite:css') && ignoredWarnings.some((partialMsg) => msg.includes(partialMsg))) return
loggerWarn(msg, options)
}
return logger
}
export default ({ command, mode }) => {
process.env = { ...process.env, ...loadEnv(mode, process.cwd()) }
// Include the rollup-plugin-visualizer if the BUILD_VISUALIZER env var is set to "true"
const buildVisualizerPlugin = process.env.BUILD_VISUALIZER === 'true' && visualizer({
filename: path.resolve(__dirname, 'bundle-analyzer/stats-treemap.html'),
template: 'treemap', // sunburst|treemap|network
sourcemap: true,
gzipSize: true
})
// 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({
logLevel: 'info',
build: {
rollupOptions: {
output: {
manualChunks: {
vue: ['vue', 'vue-router'],
kongponents: ['@kong/kongponents'],
kongAuthelements: ['@kong/kong-auth-elements'],
specRenderer: ['@kong-ui-public/spec-renderer']
}
},
plugins: [
buildVisualizerPlugin
]
}
},
plugins: [
vue(
{
template: {
transformAssetUrls: {
includeAbsolute: false
}
}
}
),
vueJsx(),
svgLoader()
],
resolve: {
alias: {
'@': path.resolve(__dirname, './src')
},
preserveSymlinks: true,
/**
* List of file extensions to try for imports that omit extensions. Note it is NOT recommended to omit extensions for custom import types (e.g. .vue) since it can interfere with IDE and type support.
* TODO: This is a crutch as we need to add `.vue` to all component imports.
* https://vitejs.dev/config/#resolve-extensions
*/
extensions: ['.mjs', '.js', '.ts', '.jsx', '.tsx', '.json', '.vue']
},
server: {
proxy: {
'^/api': {
target: portalApiUrl,
changeOrigin: true,
configure: (proxy, options) => {
mutateCookieAttributes(proxy)
setHostHeader(proxy)
}
}
}
},
customLogger: createCustomLogger()
})
}