import type { Artifact, Check, Snapshot } from "../shared/ipc";
function escapeHtml(text: string): string {
return text.replace(/[&<>"]/g, (char) => {
if (char === "&") return "&";
if (char === "<") return "<";
if (char === ">") return ">";
return """;
});
}
function time(at: number): string {
return new Date(at).toLocaleTimeString("zh-CN", { hour12: false });
}
function kb(bytes: number): string {
if (bytes < 1024) return `${bytes} B`;
return `${(bytes / 1024).toFixed(1)} KB`;
}
function displayPath(value: string): string {
return value
.replace(/file:\/\/\/Users\/[^/]+/g, "file://~")
.replace(/\/Users\/[^/]+/g, "~")
.replace(/\/private\/var\/folders\/[^/]+\/[^/]+/g, "/tmp");
}
function shorten(value: string, size = 44): string {
if (value.length <= size) return value;
return `${value.slice(0, size / 2)}…${value.slice(-size / 2)}`;
}
function checkRow(check: Check): string {
return `
${check.pass ? "PASS" : "FAIL"}
${escapeHtml(check.label)}
${escapeHtml(displayPath(check.detail))}
`;
}
function artifactCard(artifact: Artifact): string {
return `
${artifact.target}
${artifact.exists ? "built" : "missing"}
${escapeHtml(artifact.file)}
`;
}
export function mountApp(root: HTMLElement): void {
root.innerHTML = `
正在读取构建产物…
路径与页面探针
file:// 与 __dirname
三份手写配置
Forge 插件零参与
| target | entry | format | outDir |
| main | main/index.ts | cjs | out/main |
| preload | preload/index.ts | cjs | out/preload |
| renderer | index.html | esm | out/renderer |
关键选项
externalelectron + node:* 不打进 bundle
lib.formatsmain/preload 固定 CJS
base: "./"file:// 下资源相对定位
sourcemap三端都产出 .map
dev vs build
同一份 entry
| 项 | dev server | build |
| 来源 | localhost URL | file:// |
| renderer | Vite 按需编译 | 哈希资源 |
| main/preload | 需先 build | 已 build |
| sourcemap | inline/独立 | 独立 .map |
| base | / | ./ |
`;
const score = root.querySelector('[data-testid="score"]');
const banner = root.querySelector('[data-testid="banner"]');
const artifactCount = root.querySelector(
'[data-testid="artifact-count"]',
);
const artifactsEl = root.querySelector(
'[data-testid="artifacts"]',
);
const pathsEl = root.querySelector('[data-testid="paths"]');
const probeEl = root.querySelector('[data-testid="probe"]');
const checkCount = root.querySelector(
'[data-testid="check-count"]',
);
const checksEl = root.querySelector('[data-testid="checks"]');
const eventsEl = root.querySelector('[data-testid="events"]');
let state: Snapshot | null = null;
function renderArtifacts(): void {
if (!state) return;
if (artifactsEl) {
artifactsEl.innerHTML = state.artifacts.map(artifactCard).join("");
}
if (artifactCount) {
const built = state.artifacts.filter((row) => row.exists).length;
artifactCount.textContent = `${built}/${state.artifacts.length} 已生成`;
}
}
function renderPaths(): void {
if (!state) return;
const rows: Array<[string, string]> = [
["__dirname", displayPath(state.mainDir)],
["app.getAppPath()", displayPath(state.appPath)],
["process.resourcesPath", displayPath(state.resourcesPath)],
["页面 URL", displayPath(state.probe.url)],
["dev URL", state.devUrl ? displayPath(state.devUrl) : "(unset)"],
["script src", state.probe.scriptSrcs[0] || "(none)"],
["styleSheets", String(state.probe.styleCount)],
["CSP", state.probe.csp ? "present" : "missing"],
];
if (pathsEl) {
pathsEl.innerHTML = rows
.map(
([label, value]) =>
`${escapeHtml(label)}${escapeHtml(shorten(value, 48))}`,
)
.join("");
}
if (probeEl) {
probeEl.textContent = `require=${state.probe.requireType} · process=${state.probe.processType} · relative=${state.probe.relativeScript}`;
}
}
function renderChecks(): void {
if (!state) return;
if (checksEl) checksEl.innerHTML = state.checks.map(checkRow).join("");
if (checkCount) {
const passed = state.checks.filter((check) => check.pass).length;
checkCount.textContent = `${passed}/${state.checks.length} 通过`;
}
if (score) {
score.textContent = `${state.score} / 100`;
score.className = `pill ${state.score === 100 ? "ok-pill" : "bad-pill"}`;
}
if (banner) {
const built = state.artifacts.filter((row) => row.exists).length;
banner.textContent =
built === state.artifacts.length
? "三端产物齐全:main / preload / renderer 均由手写配置生成"
: `产物不完整:${built}/${state.artifacts.length},先运行 npm run build`;
banner.className = `banner ${built === state.artifacts.length ? "ok" : "warn"}`;
}
}
function renderEvents(): void {
if (!eventsEl || !state) return;
eventsEl.innerHTML =
state.events.length === 0
? `暂无活动`
: state.events
.slice(0, 8)
.map(
(row) => `
${time(row.at)}
${escapeHtml(row.kind)}
${escapeHtml(displayPath(row.detail))}
`,
)
.join("");
}
function render(next: Snapshot): void {
state = next;
renderArtifacts();
renderPaths();
renderChecks();
renderEvents();
}
void window.api.build.snapshot().then(render);
window.api.build.onEvent(() => {
void window.api.build.snapshot().then(render);
});
root
.querySelector('[data-testid="refresh"]')
?.addEventListener("click", async () => {
render(await window.api.build.refresh());
});
window.__prepareShots = async (stage) => {
document.body.dataset.shot = stage;
render(await window.api.build.refresh());
if (stage === "configs") {
document
.querySelector(".configs-card")
?.scrollIntoView({ block: "start" });
} else if (stage === "paths") {
document.querySelector(".paths-card")?.scrollIntoView({ block: "start" });
} else {
document
.querySelector(".artifacts-card")
?.scrollIntoView({ block: "start" });
}
};
window.__demo = async () => {
const next = await window.api.build.snapshot();
state = next;
const find = (target: string) =>
next.artifacts.find((row) => row.target === target);
return {
score: next.score,
checks: next.checks.length,
passed: next.checks.filter((check) => check.pass).length,
mainBytes: find("main")?.bytes ?? 0,
preloadBytes: find("preload")?.bytes ?? 0,
rendererBytes: find("renderer")?.bytes ?? 0,
relativeScript: next.probe.relativeScript,
url: next.probe.url,
requireType: next.probe.requireType,
processType: next.probe.processType,
cjs: next.checks.find((check) => check.id === "cjs")?.pass ?? false,
preloadPath:
next.checks.find((check) => check.id === "preload-path")?.pass ?? false,
files: next.artifacts.filter((row) => row.exists).length,
};
};
}