Files
NoticeFront/plugins/vite-plugin-theme/lib/index.cjs
T
chenxing 21a0bd36b6
build / build (push) Successful in 2m34s
fix(ci): vendor @yunhai/vite-plugin-theme 进仓,修 CI npm ci 解析不到 file:../../YschTheme
- CI 只 clone NoticeFront,file:../../YschTheme/packages/vite-plugin 在容器里不存在 → npm ci 秒失败(run#13)。
- 改为仓内 plugins/vite-plugin-theme(拷构建产物,dist→lib 规避 .gitignore 的 dist 规则;剥离 devDeps 防嵌套安装)。
- package.json 消费改 file:./plugins/vite-plugin-theme,重生成 lock(已清 extraneous 孤儿)。
- 本地 npm run build 通过。待 Nexus npm 仓就绪后再迁回版本号消费(方案 B)。
2026-09-24 12:22:09 +08:00

167 lines
6.9 KiB
JavaScript

'use strict';
"use strict";
var __defProp = Object.defineProperty;
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
var __getOwnPropNames = Object.getOwnPropertyNames;
var __hasOwnProp = Object.prototype.hasOwnProperty;
var __export = (target, all) => {
for (var name in all)
__defProp(target, name, { get: all[name], enumerable: true });
};
var __copyProps = (to, from, except, desc) => {
if (from && typeof from === "object" || typeof from === "function") {
for (let key of __getOwnPropNames(from))
if (!__hasOwnProp.call(to, key) && key !== except)
__defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
}
return to;
};
var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
// src/index.ts
var src_exports = {};
__export(src_exports, {
DEFAULT_PROXY_TARGET: () => DEFAULT_PROXY_TARGET,
FALLBACK_STYLE: () => FALLBACK_STYLE,
MANIFEST: () => MANIFEST,
RESOLVED_VIRTUAL_ID: () => RESOLVED_VIRTUAL_ID,
VIRTUAL_ID: () => VIRTUAL_ID,
default: () => src_default,
initBootstrap: () => initBootstrap,
isValidStaticBase: () => isValidStaticBase,
loaderSource: () => loaderSource,
yhTheme: () => yhTheme
});
module.exports = __toCommonJS(src_exports);
var DEFAULT_PROXY_TARGET = "http://base.yunhaixingtong.com";
var VIRTUAL_ID = "virtual:yh-theme-loader";
var RESOLVED_VIRTUAL_ID = `\0${VIRTUAL_ID}`;
var MANIFEST = "manifest.json";
var FALLBACK_STYLE = `html:not(.yh-theme-ready) body { opacity: 0; }
html.yh-theme-ready body { opacity: 1; transition: opacity .15s; }`;
function isValidStaticBase(v) {
return v === "" || /^\//.test(v) || /^https?:\/\//.test(v);
}
function loaderSource(staticBase) {
return `// BASE\uFF1A\u6839\u76F8\u5BF9\uFF08\u4E09\u7AEF\u540C\u6E90\uFF09\u3002\u7981\u6B62\u7528 BASE_URL \u4E4B\u7C7B\u7684\u6784\u5EFA\u671F\u76F8\u5BF9\u57FA\u51C6\u2014\u2014
// Notice \u7684 vite base \u662F './'\uFF0C\u4F1A\u89E3\u6790\u6210 /notice/front/static/... \u5BFC\u81F4 404
const BASE = ${JSON.stringify(staticBase)} || (window.__APP_CONFIG__ && window.__APP_CONFIG__.staticHost != null ? window.__APP_CONFIG__.staticHost : '')
const markReady = () => document.documentElement.classList.add('yh-theme-ready')
const markFailed = (f) => {
console.warn(\`[yh-theme] \${f} \u52A0\u8F7D\u5931\u8D25\uFF0C\u964D\u7EA7\u4E3A\u7EC4\u4EF6\u5E93\u9ED8\u8BA4\u89C2\u611F\`)
document.documentElement.classList.add('yh-theme-failed', 'yh-theme-ready')
}
// \u4F18\u5148\u590D\u7528 init \u811A\u672C\u5728 <head> \u91CC\u5DF2\u542F\u52A8\u7684 manifest \u9884\u53D6\uFF08\u542B\u5E26\u7248\u672C preload\uFF09
;(window.__YH_THEME_MANIFEST__ ?? fetch(\`\${BASE}/static/${MANIFEST}\`).then(r => r.json()))
.then((m) => {
let loaded = 0
m.files.forEach((f) => {
const l = document.createElement('link')
l.rel = 'stylesheet'
l.href = \`\${BASE}/static/\${f}?v=\${m.version}\` // \u4E0E init preload \u7684 URL \u4E00\u5B57\u4E0D\u5DEE
l.onload = () => { if (++loaded === m.files.length) markReady() }
l.onerror = () => markFailed(f)
document.head.appendChild(l)
})
setTimeout(markReady, 500) // \u515C\u5E95\uFF1A500ms \u672A load \u5B8C\u4E5F\u653E\u884C
})
.catch(() => markFailed('manifest'))
`;
}
function initBootstrap(staticBase) {
const url = `${staticBase.replace(/\/+$/, "")}/static/js/yh-theme-init.js`;
const setBase = staticBase ? `window.__YH_THEME_STATIC_BASE__=${JSON.stringify(staticBase)};` : "";
return `(function(){${setBase}document.write('<script src="${url}"><\\/script>');})();`;
}
function yhTheme(options = {}) {
const target = options.target ?? "pc";
const staticBase = options.staticBase ?? "";
const proxyTarget = options.proxyTarget ?? DEFAULT_PROXY_TARGET;
if (target !== "pc") {
throw new Error(`[yh-theme] target='${target}' \u7B2C\u4E00\u6CE2\u672A\u5B9E\u73B0\uFF08app \u6CE2\u6B21\u518D\u8865 manifest.app.json \u4E0E app \u4EA7\u7269\uFF09`);
}
if (!isValidStaticBase(staticBase)) {
throw new Error(
`[yh-theme] staticBase \u53EA\u63A5\u53D7 '' / '/xxx' \u6839\u76F8\u5BF9\u524D\u7F00 / 'https?://\u2026'\uFF0C\u6536\u5230 '${staticBase}'\u3002\u4E0D\u8981\u628A vite base \u4F20\u8FDB\u6765\uFF08Notice \u7684 base \u662F './'\uFF0C\u4F1A\u89E3\u6790\u6210 /notice/front/static/... 404\uFF09`
);
}
const entryRe = options.entry ?? /(^|\/)src\/main\.(ts|js)$/;
const proxyFromEnv = () => {
var _a;
const env = (_a = globalThis.process) == null ? void 0 : _a.env;
const v = env == null ? void 0 : env.YH_THEME_PROXY_TARGET;
return v && v.trim() ? v.trim() : void 0;
};
return {
name: "@yunhai/vite-plugin-theme",
/**
* proxy 在 config 钩子里合并:Vite 的 proxy 中间件在 configureServer 之前就已建立,
* 到 configureServer 里再改 server.config.server.proxy 不会生效(B4 的语义在这里落地)。
*/
config(userConfig) {
var _a;
const url = proxyFromEnv() ?? proxyTarget;
const existing = ((_a = userConfig.server) == null ? void 0 : _a.proxy) ?? {};
if (existing["/static"]) return {};
const patch = {
proxy: {
...existing,
"/static": { target: url, changeOrigin: true }
}
};
if (!proxyFromEnv() && url !== DEFAULT_PROXY_TARGET) {
patch.proxy["/static"] = {
target: url,
changeOrigin: true,
headers: { Host: DEFAULT_PROXY_TARGET.replace(/^https?:\/\//, "") }
};
}
return { server: patch };
},
resolveId(id) {
return id === VIRTUAL_ID ? RESOLVED_VIRTUAL_ID : null;
},
load(id) {
return id === RESOLVED_VIRTUAL_ID ? loaderSource(staticBase) : null;
},
transform(code, id) {
const cleanId = id.split("?")[0].replace(/\\/g, "/");
if (!entryRe.test(cleanId)) return null;
if (code.includes(VIRTUAL_ID)) return null;
return { code: `${code}
import "${VIRTUAL_ID}";
`, map: null };
},
transformIndexHtml: {
order: "post",
handler() {
return [
{ tag: "style", children: FALLBACK_STYLE, injectTo: "head" },
{ tag: "script", children: initBootstrap(staticBase), injectTo: "head" }
];
}
},
configureServer(server) {
var _a;
const proxy = ((_a = server.config.server) == null ? void 0 : _a.proxy) ?? {};
const target0 = proxy["/static"];
server.config.logger.info(
`[yh-theme] dev /static \u2192 ${(target0 == null ? void 0 : target0.target) ?? "(\u672A\u6302\uFF1A\u5E94\u7528\u4FA7\u5DF2\u81EA\u5E26\u540C\u540D proxy\uFF1F)"}${proxyFromEnv() ? `\uFF08YH_THEME_PROXY_TARGET=${proxyFromEnv()}\uFF09` : ""}`
);
}
};
}
var src_default = yhTheme;
// Annotate the CommonJS export names for ESM import in node:
0 && (module.exports = {
DEFAULT_PROXY_TARGET,
FALLBACK_STYLE,
MANIFEST,
RESOLVED_VIRTUAL_ID,
VIRTUAL_ID,
initBootstrap,
isValidStaticBase,
loaderSource,
yhTheme
});