-
Notifications
You must be signed in to change notification settings - Fork 11
/
vite.config.shared.ts
214 lines (199 loc) · 6.88 KB
/
vite.config.shared.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
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
/// <reference types="vitest" />
/**
* Shared Vite config settings for all components
*/
import { defineConfig, loadEnv } from 'vite'
import vue from '@vitejs/plugin-vue'
import vueJsx from '@vitejs/plugin-vue-jsx'
import dns from 'dns'
import path, { join } from 'path'
import { visualizer } from 'rollup-plugin-visualizer'
// You can set dns.setDefaultResultOrder('verbatim') to disable the reordering behavior. Vite will then print the address as localhost
// https://vitejs.dev/config/server-options.html#server-host
dns.setDefaultResultOrder('verbatim')
// Include the rollup-plugin-visualizer if the BUILD_VISUALIZER env var is set to "true"
const buildVisualizerPlugin = process.env.BUILD_VISUALIZER
? visualizer({
filename: path.resolve(__dirname, `packages/${process.env.BUILD_VISUALIZER}/bundle-analyzer/stats-treemap.html`),
template: 'treemap', // sunburst|treemap|network
sourcemap: true,
gzipSize: true,
})
: undefined
/**
* Sanitize package/filename to exclude undesired strings
* IMPORANT: If this function is changed, you **must** change the function in `/packages/core/cli/src/core/package.ts` as well.
* @param {string} packageName The string to sanitize
* @returns {string} The sanitized package/filename string
*/
export const sanitizePackageName = (packageName: string): string => {
// Add additional replace rules as needed
// Replace any variation of string 'Analytics' in assets and chunks. These are in order to preserve capitalization.
// (Some adblock filter lists deny requests for files starting with "assets/analytics". See MA-926 for more context.)
const sanitizedName = (packageName || '').replace(/Analytics/g, 'Vitals').replace(/analytics/gi, 'vitals')
return sanitizedName
}
export default defineConfig({
plugins: [
vue(),
vueJsx(),
],
resolve: {
// Use this option to force Vite to always resolve listed dependencies to the same copy (from project root)
dedupe: ['vue', 'vue-router', '@kong/kongponents'],
alias: {
'@entities-shared-sandbox': path.resolve(__dirname, 'packages/entities/entities-shared/sandbox/shared'),
},
},
css: {
preprocessorOptions: {
scss: {
// Inject the @kong/design-tokens SCSS variables to make them available for all components.
additionalData: '@import "@kong/design-tokens/tokens/scss/variables";',
},
},
},
build: {
outDir: './dist',
cssCodeSplit: false,
minify: true,
sourcemap: !!process.env.BUILD_VISUALIZER,
rollupOptions: {
// Make sure to externalize deps that shouldn't be bundled into your library
// If config.build.rollupOptions.external is also set at the package level, the arrays will be merged
external: ['vue', 'vue-router', '@kong/kongponents', '@kong-ui-public/i18n', 'axios'],
output: {
// Provide global variables to use in the UMD build for externalized deps
globals: {
vue: 'Vue',
'vue-router': 'VueRouter',
'@kong-ui-public/i18n': 'kong-ui-public-i18n',
'@kong/kongponents': 'Kongponents',
axios: 'axios',
},
exports: 'named',
},
plugins: [
// visualizer must remain last in the list of plugins
buildVisualizerPlugin,
],
},
},
optimizeDeps: {
include: [
'vue',
'vue-router',
],
},
server: {
fs: {
/**
* Allow serving files from one level up from the package root - IMPORTANT - since this is a monorepo
*/
allow: [join(__dirname, '..')],
},
},
// Change the root when utilizing the sandbox via USE_SANDBOX=true to use the `/sandbox/*` files
// During the build process, the `/sandbox/*` files are not used and we should default to the package root.
root: process.env.USE_SANDBOX ? './sandbox' : process.cwd(),
// Sets the Vite envDir to point to the repository root `.env.*` files.
// Please do NOT add other .env files in child directories.
envDir: '../../../../',
test: {
globals: true,
environment: 'jsdom',
setupFiles: [],
coverage: {
reporter: ['text', 'json', 'html'],
},
// TODO: The `registerNodeLoader` setting is deprecated. Commenting out for now but if tests start failing we need to utilize the new `deps.optimizer.web.include` instead
// deps: {
// registerNodeLoader: true, // Ensure modules are imported properly
// },
include: ['**/src/**/*.spec.ts'],
exclude: [
'**/dist/**',
'**/__template__/**',
'**/node_modules/**',
'packages/core/cli/**',
],
},
})
/**
* Define the server.proxy rules for various shared APIs
* These utilize the `VITE_KONNECT_PAT` Konnect PAT token located in `/.env.development.local`
* @param pathToRoot The path to the repository root, from the package directory, where your .env.development.local file is located. Defaults to `../../../.' which works for most packages.
* @returns Object of API proxies to pass to the vite `config.server.proxy`
*/
export const getApiProxies = (pathToRoot: string = '../../../.') => {
// Import env variables from the root
// Hard-coded to 'development' since we are only using the env variables in the local dev server
const env = loadEnv('development', pathToRoot, '')
const konnectAuthHeader = env.VITE_KONNECT_PAT
? {
authorization: `Bearer ${env.VITE_KONNECT_PAT}`,
}
: undefined
const kongManagerAuthHeader = env.VITE_KONG_MANAGER_TOKEN
? {
'kong-admin-token': env.VITE_KONG_MANAGER_TOKEN,
}
: undefined
return {
/**
* /kong-ui/config JSON
*/
'^/kong-ui/config': {
target: env.VITE_KONNECT_CONFIG,
changeOrigin: true,
},
/**
* KONNECT PROXIES
* TODO: when KHCP-5497 consume these proxies from the helper function?
*/
'^/us/kong-api/konnect-api': {
target: (env.VITE_KONNECT_API ?? '').replace(/\{geo\}/, 'us'),
rewrite: (path: string) => path.replace('/us/kong-api', ''),
changeOrigin: true,
headers: {
...konnectAuthHeader,
},
},
'^/eu/kong-api/konnect-api': {
target: (env.VITE_KONNECT_API ?? '').replace(/\{geo\}/, 'eu'),
rewrite: (path: string) => path.replace('/eu/kong-api', ''),
changeOrigin: true,
headers: {
...konnectAuthHeader,
},
},
// KAuth v1 APIs
'^/kauth': {
target: env.VITE_KONNECT_KAUTH,
changeOrigin: true,
headers: {
...konnectAuthHeader,
},
},
// Global v2 APIs
'^/kong-api/v2': {
target: env.VITE_KONNECT_GLOBAL,
rewrite: (path: string) => path.replace('/kong-api', ''),
changeOrigin: true,
headers: {
...konnectAuthHeader,
},
},
/**
* KONG MANAGER PROXIES
*/
'^/kong-manager': {
target: env.VITE_KONG_MANAGER_API,
rewrite: (path: string) => path.replace('/kong-manager', ''),
changeOrigin: true,
headers: {
...kongManagerAuthHeader,
},
},
}
}