-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathvite.config.old.js
More file actions
102 lines (95 loc) · 2.87 KB
/
Copy pathvite.config.old.js
File metadata and controls
102 lines (95 loc) · 2.87 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
95
96
97
98
99
100
101
102
import { defineConfig } from 'vite'
import vue from '@vitejs/plugin-vue'
import mkcert from 'vite-plugin-mkcert'
import fs from 'fs'
import path from 'path'
import { fileURLToPath } from 'url'
import { parseCspOrigins, buildBackgroundStyle, injectTitle, injectApiBase, stripCspMeta } from './src/utils/csp.js'
const __dirname = path.dirname(fileURLToPath(import.meta.url))
const frontendDir = path.resolve(__dirname, 'src/frontend-old')
const devProxyTarget = process.env.VITE_DEV_PROXY_TARGET || 'https://localhost:8787'
const createWorkerProxy = () => ({
target: devProxyTarget,
changeOrigin: true,
secure: false,
ws: true
})
function loadEnvFile() {
const envPath = path.resolve(__dirname, '.env')
const env = {}
if (!fs.existsSync(envPath)) return env
const content = fs.readFileSync(envPath, 'utf8')
for (const line of content.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 apiBaseRaw = env.API_BASE || ''
const cspApiRaw = env.CSP_API || ''
const backgroundImage = env.BACKGROUND_IMAGE || ''
const title = env.TITLE || ''
// API_BASE 与 CSP_API 合并,写入运行时 apiBase meta。
const rawApiDomains = [
...parseCspOrigins(apiBaseRaw),
...parseCspOrigins(cspApiRaw)
]
return {
name: 'env-inject',
transformIndexHtml(html) {
html = stripCspMeta(html)
html = injectTitle(html, title)
html = injectApiBase(html, rawApiDomains)
if (backgroundImage) {
const bgStyle = buildBackgroundStyle(backgroundImage)
html = html.replace('</head>', `${bgStyle}\n</head>`)
}
return html
}
}
}
export default defineConfig({
root: frontendDir,
publicDir: path.resolve(__dirname, 'public'),
plugins: [vue(), mkcert(), envPlugin()],
base: process.env.VITE_BASE || '/',
resolve: {
alias: {
'@': frontendDir
}
},
build: {
outDir: path.resolve(__dirname, 'dist-old'),
assetsDir: 'static',
emptyOutDir: true,
rollupOptions: {
output: {
entryFileNames: 'static/[name]-[hash].js',
chunkFileNames: 'static/[name]-[hash].js',
assetFileNames: 'static/[name]-[hash].[ext]'
}
}
},
server: {
https: true,
port: 5174,
proxy: {
'/api': createWorkerProxy(),
'/admin/api': createWorkerProxy(),
'/update': createWorkerProxy(),
'/updateDatabase': createWorkerProxy(),
'/clearHistory': createWorkerProxy(),
'/__do': createWorkerProxy()
}
}
})