fix(debug): inject `import.env`

Signed-off-by: Innei <tukon479@gmail.com>
This commit is contained in:
Innei 2024-12-03 14:59:25 +08:00
parent 5fec56e9b0
commit 810516ae7d
No known key found for this signature in database
GPG Key ID: 0F62D33977F021F7
2 changed files with 10 additions and 4 deletions

View File

@ -3,7 +3,7 @@
<head>
<title>Debug Proxy</title>
<script>
<script type="module">
globalThis["__DEBUG_PROXY__"] = true
const searchParams = new URLSearchParams(window.location.search)

View File

@ -1,4 +1,4 @@
import { copyFileSync, existsSync } from "node:fs"
import { existsSync, readFileSync, writeFileSync } from "node:fs"
import { resolve } from "node:path"
import { fileURLToPath } from "node:url"
@ -288,7 +288,7 @@ function checkBrowserSupport() {
}
}
const htmlPlugin: (env: any) => PluginOption = () => {
const htmlPlugin: (env: any) => PluginOption = (env) => {
let config: ResolvedConfig
return {
name: "html-transform",
@ -302,7 +302,13 @@ const htmlPlugin: (env: any) => PluginOption = () => {
const debugProxyHtml = resolve(root, "debug_proxy.html")
if (existsSync(debugProxyHtml)) {
copyFileSync(debugProxyHtml, resolve(dist, "__debug_proxy.html"))
const content = readFileSync(debugProxyHtml, "utf-8")
writeFileSync(
resolve(dist, "__debug_proxy.html"),
content.replace("import.meta.env.VITE_API_URL", `"${env.VITE_API_URL}"`),
)
}
},
transformIndexHtml(html) {