-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathvite.config.ts
More file actions
61 lines (57 loc) · 1.93 KB
/
Copy pathvite.config.ts
File metadata and controls
61 lines (57 loc) · 1.93 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
import { defineConfig, loadEnv } from 'vite'
import react from '@vitejs/plugin-react'
import path from 'path'
export default defineConfig(({ command, mode }) => {
const env = loadEnv(mode, process.cwd(), '')
const base = normalizeBasePath(env.VITE_BASE_PATH ?? '/')
const devApiProxyTarget = command === 'serve'
? normalizeOptionalHttpOrigin(env.SLASHMON_DEV_API_PROXY_TARGET)
: undefined
return {
plugins: [react()],
resolve: {
alias: {
'@': path.resolve(__dirname, './src'),
'@shared': path.resolve(__dirname, './shared'),
},
},
base,
server: {
port: 5173,
proxy: devApiProxyTarget ? {
'/api': {
target: devApiProxyTarget,
changeOrigin: true,
configure(proxy) {
// The browser talks same-origin to Vite. Do not forward localhost's
// Origin to a backend deliberately pinned to the production PWA.
proxy.on('proxyReq', (proxyRequest) => {
proxyRequest.removeHeader('origin')
})
},
},
} : undefined,
},
}
})
function normalizeBasePath(value: string): string {
const normalized = `/${value.trim().replace(/^\/+|\/+$/g, '')}/`.replace('//', '/')
if (!/^\/(?:[A-Za-z0-9._~-]+\/)*$/.test(normalized)) {
throw new Error('VITE_BASE_PATH must be an absolute URL path such as / or /slashmon/')
}
return normalized
}
function normalizeOptionalHttpOrigin(value: string | undefined): string | undefined {
const raw = value?.trim()
if (!raw) {
return undefined
}
const url = new URL(raw)
if (!['http:', 'https:'].includes(url.protocol) || url.username || url.password) {
throw new Error('SLASHMON_DEV_API_PROXY_TARGET must be a credential-free HTTP(S) origin')
}
if (url.pathname !== '/' || url.search || url.hash) {
throw new Error('SLASHMON_DEV_API_PROXY_TARGET must be an origin without a path, query, or fragment')
}
return url.origin
}