feat(env-config): 运行时配置改造-index.html内联引导脚本同步XHR拉/{ctx}/api/env-config?target=admin写__APP_CONFIG__,host.config三级合并(运行时JSON>VITE_*(仅dev/mock)>同域推导),删.env.base/.env.prod产物零环境值,vitest 56/56+build产物零烧入
build / build (push) Successful in 2m21s

This commit is contained in:
chenxing
2026-09-08 17:18:35 +08:00
parent d8ac455428
commit 198175975b
6 changed files with 72 additions and 26 deletions
+40 -10
View File
@@ -1,24 +1,54 @@
import { BaseConf } from "@/types/Basics";
/**
* 通过 Vite 环境变量管理配置(Notice front 同模式)
* 环境变量定义在项目根目录的 .env.dev / .env.base / .env.prod 中
* serviceHost = 源 + 后端上下文路径(如 http://base.yunhaixingtong.com/cms),
* dev 模式留空走 vite 同源代理(规避 CAS 过滤器不回 CORS 头)
* 配置优先级(高 → 低):
* ① window.__APP_CONFIG__:线上运行时配置,index.html 引导脚本在应用模块加载前同步拉取
* /<contextPath>/api/env-config?target=<部署目录名>(默认 admin,projman 配置管理维护)写入
* ② import.meta.env.VITE_*:仅本地开发/演示(vite dev --mode dev 读 .env.dev、--mode mock 读 .env.mock);
* build 产物不读任何 .env.*(.env.base/.env.prod 已删),VITE_* 恒为 undefined,本层自动跳过,
* 产物不含任何环境值
* ③ 同域推导兜底:env-config 未配置/拉取失败时框架至少能渲染并调通同源 API;
* ssoHost 为空 → 登录跳转不可用——与后端未就绪状态一致,可接受。
*/
const env = import.meta.env;
// ③ 同域推导兜底:context 段取当前路径首段(/{contextPath}/admin/ → contextPath,即后端上下文),
// 与服务端 context-path 对齐(本地 /cms、base/prod /yschcms);本地 dev/mock 在根路径运行(无段),
// serviceHost 留空走 vite 同源代理(规避 CAS 过滤器不回 CORS 头)
const seg = window.location.pathname.split("/").filter(Boolean);
const origin = window.location.origin;
let conf: BaseConf = {
baseHost: env.VITE_BASE_HOST,
ssoHost: env.VITE_SSO_HOST,
serviceHost: env.VITE_IS_MOCK === "true" ? "" : env.VITE_SERVICE_HOST,
isMock: env.VITE_IS_MOCK === "true",
baseHost: origin,
ssoHost: "",
serviceHost: seg.length >= 2 ? `${origin}/${seg[0]}` : "",
isMock: false,
isDebug: env.DEV,
};
// 逐键覆盖:只覆盖来源里实际出现且非空的键,未出现的键沿用底层值
const applyConf = (target: any, source: any) => {
if (!source || typeof source !== "object") return;
Object.keys(source).forEach(key => {
if (source[key] !== undefined && source[key] !== null && source[key] !== "") {
(target as any)[key] = source[key];
}
});
};
// ② 本地开发/演示(build 产物中恒 undefined → 自动跳过)
applyConf(conf, {
baseHost: env.VITE_BASE_HOST,
ssoHost: env.VITE_SSO_HOST,
serviceHost: env.VITE_SERVICE_HOST,
isMock: env.VITE_IS_MOCK === "true",
});
// ① 线上运行时配置(最高优先,覆盖前面所有)
applyConf(conf, (window as any).__APP_CONFIG__);
// HTTPS 自动升级:当前页面是 https 时,将所有 http:// 配置项升级为 https://
var ishttps = "https:" == document.location.protocol ? true : false;
if (ishttps) {
if ("https:" === document.location.protocol) {
Object.keys(conf).forEach(element => {
const val = (conf as any)[element];
if (typeof val === "string" && val.indexOf("http://") > -1) {
+4
View File
@@ -1,5 +1,9 @@
/// <reference types="vite/client" />
// import.meta.env 类型声明:VITE_* 仅供本地开发/演示(.env.dev、.env.mock,vite dev 对应 mode)读取;
// build 不读任何 .env.*(.env.base/.env.prod 已删),VITE_* 恒为 undefined,不会进构建产物。
// 线上环境差异由 env-config 运行时配置承担(index.html 引导脚本 → window.__APP_CONFIG__,见
// src/assets/config/host.config.ts)。
interface ImportMetaEnv {
readonly VITE_BASE_HOST: string;
readonly VITE_SSO_HOST: string;