From 8fb45bd42ae5dde5681c4a2a04e844822243d4cd Mon Sep 17 00:00:00 2001 From: zhangshun <453905631@qq.com> Date: Tue, 2 Jun 2026 11:46:55 +0800 Subject: [PATCH] =?UTF-8?q?=E7=94=9F=E4=BA=A7=E6=9E=84=E5=BB=BA=E8=B7=B3?= =?UTF-8?q?=E8=BF=87=E6=9C=AC=E5=9C=B0=E5=BC=80=E5=8F=91=E8=AF=81=E4=B9=A6?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- web/vite.config.ts | 55 +++++++++++++++++++++++++--------------------- 1 file changed, 30 insertions(+), 25 deletions(-) diff --git a/web/vite.config.ts b/web/vite.config.ts index 21cce82..40ccd60 100644 --- a/web/vite.config.ts +++ b/web/vite.config.ts @@ -5,30 +5,35 @@ import { fileURLToPath } from "node:url"; const __dirname = path.dirname(fileURLToPath(import.meta.url)); const rootDir = path.resolve(__dirname, "../.."); -const httpsConfig = { - key: fs.readFileSync(path.join(rootDir, "certs/localhost+2-key.pem")), - cert: fs.readFileSync(path.join(rootDir, "certs/localhost+2.pem")) -}; -export default defineConfig({ - server: { - port: 5174, - strictPort: true, - https: httpsConfig, - headers: { - // 关键:添加 COEP 和 COOP - "Cross-Origin-Embedder-Policy": "credentialless", // 或 "require-corp" - "Cross-Origin-Opener-Policy": "same-origin", - "Cross-Origin-Resource-Policy": "cross-origin", // 改为 cross-origin - // 开发环境允许跨域 - "Access-Control-Allow-Origin": "https://localhost:3000", // 父页面的地址 - "Access-Control-Allow-Methods": "GET, POST, PUT, DELETE, OPTIONS", - "Access-Control-Allow-Headers": "Content-Type", - "Access-Control-Allow-Credentials": "true" - }, - proxy: { - "/api": "http://127.0.0.1:3001", - "/storage": "http://127.0.0.1:3001" +function getDevHttpsConfig() { + return { + key: fs.readFileSync(path.join(rootDir, "certs/localhost+2-key.pem")), + cert: fs.readFileSync(path.join(rootDir, "certs/localhost+2.pem")) + }; +} + +export default defineConfig(({ command }) => ({ + server: command === "serve" + ? { + port: 5174, + strictPort: true, + https: getDevHttpsConfig(), + headers: { + // 关键:添加 COEP 和 COOP + "Cross-Origin-Embedder-Policy": "credentialless", // 或 "require-corp" + "Cross-Origin-Opener-Policy": "same-origin", + "Cross-Origin-Resource-Policy": "cross-origin", // 改为 cross-origin + // 开发环境允许跨域 + "Access-Control-Allow-Origin": "https://localhost:3000", // 父页面的地址 + "Access-Control-Allow-Methods": "GET, POST, PUT, DELETE, OPTIONS", + "Access-Control-Allow-Headers": "Content-Type", + "Access-Control-Allow-Credentials": "true" + }, + proxy: { + "/api": "http://127.0.0.1:3001", + "/storage": "http://127.0.0.1:3001" + } } - } -}); + : undefined +}));