-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathvite.config.js
More file actions
94 lines (86 loc) · 2.68 KB
/
Copy pathvite.config.js
File metadata and controls
94 lines (86 loc) · 2.68 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
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
import { fileURLToPath, URL } from 'node:url'
import fs from 'node:fs'
import path from 'node:path'
import vue from '@vitejs/plugin-vue'
import { defineConfig } from 'vite'
import { buildBackgroundStyle, injectApiBase, injectTitle, parseCspOrigins, stripCspMeta } from './src/utils/csp.js'
const devProxyTarget = process.env.VITE_DEV_PROXY_TARGET || 'https://localhost:8787'
const rootDir = fileURLToPath(new URL('.', import.meta.url))
const frontendDir = fileURLToPath(new URL('./src/frontend', import.meta.url))
const createWorkerProxy = () => ({
target: devProxyTarget,
changeOrigin: true,
secure: false,
ws: true
})
function loadEnvFile() {
const envPath = path.resolve(rootDir, '.env')
const env = {}
if (!fs.existsSync(envPath)) return env
for (const line of fs.readFileSync(envPath, 'utf8').split('\n')) {
const trimmed = line.trim()
if (!trimmed || trimmed.startsWith('#')) continue
const eqIndex = trimmed.indexOf('=')
if (eqIndex === -1) continue
const key = trimmed.slice(0, eqIndex).trim()
let value = trimmed.slice(eqIndex + 1).trim()
if ((value.startsWith('"') && value.endsWith('"')) || (value.startsWith("'") && value.endsWith("'"))) {
value = value.slice(1, -1)
}
env[key] = value
}
return env
}
function envPlugin() {
const env = loadEnvFile()
const apiDomains = [
...parseCspOrigins(env.API_BASE || ''),
...parseCspOrigins(env.CSP_API || ''),
]
return {
name: 'env-inject',
transformIndexHtml(html) {
let transformed = stripCspMeta(html)
transformed = injectTitle(transformed, env.TITLE || '')
transformed = injectApiBase(transformed, apiDomains)
if (env.BACKGROUND_IMAGE) {
transformed = transformed.replace('</head>', `${buildBackgroundStyle(env.BACKGROUND_IMAGE)}\n</head>`)
}
return transformed
},
}
}
export default defineConfig({
root: frontendDir,
publicDir: fileURLToPath(new URL('./public', import.meta.url)),
plugins: [vue(), envPlugin()],
base: process.env.VITE_BASE || '/',
resolve: {
alias: {
'@': frontendDir
}
},
build: {
outDir: fileURLToPath(new URL('./dist', import.meta.url)),
assetsDir: 'static',
emptyOutDir: true,
rollupOptions: {
output: {
entryFileNames: 'static/[name]-[hash].js',
chunkFileNames: 'static/[name]-[hash].js',
assetFileNames: 'static/[name]-[hash].[ext]'
}
}
},
server: {
port: 5173,
proxy: {
'/api': createWorkerProxy(),
'/admin/api': createWorkerProxy(),
'/update': createWorkerProxy(),
'/updateDatabase': createWorkerProxy(),
'/clearHistory': createWorkerProxy(),
'/__do': createWorkerProxy()
}
}
})