34 lines
1.0 KiB
TypeScript
34 lines
1.0 KiB
TypeScript
import react from '@vitejs/plugin-react'
|
|
import { defineConfig, loadEnv, type ProxyOptions } from 'vite'
|
|
|
|
// https://vite.dev/config/
|
|
export default defineConfig(({ mode }) => {
|
|
const env = loadEnv(mode, process.cwd(), '')
|
|
const proxy: Record<string, ProxyOptions> = {}
|
|
|
|
if (env.CALDAV_URL) {
|
|
const target = new URL(env.CALDAV_URL)
|
|
const basePath = target.pathname.replace(/\/$/, '')
|
|
|
|
// Credentials never reach the client bundle: the dev server injects
|
|
// Basic auth on the proxied request, browser code only ever calls /caldav/.
|
|
const headers: Record<string, string> = {}
|
|
if (env.CALDAV_USERNAME && env.CALDAV_PASSWORD) {
|
|
const token = Buffer.from(`${env.CALDAV_USERNAME}:${env.CALDAV_PASSWORD}`).toString('base64')
|
|
headers.Authorization = `Basic ${token}`
|
|
}
|
|
|
|
proxy['/caldav'] = {
|
|
target: target.origin,
|
|
changeOrigin: true,
|
|
rewrite: (path) => path.replace(/^\/caldav/, basePath),
|
|
headers,
|
|
}
|
|
}
|
|
|
|
return {
|
|
plugins: [react()],
|
|
server: { proxy },
|
|
}
|
|
})
|