示例工程 02-first-app
02-first-app/app/src/renderer.ts
import "./styles.css";
import type { ProjectInfo } from "../shared/ipc";
type TabId = "structure" | "modes" | "scripts";
declare global {
interface Window {
api: { info(): Promise<ProjectInfo> };
__setTab?: (tab: TabId) => void;
__snapshot?: () => unknown;
}
}
const state: { tab: TabId; info: ProjectInfo | null } = {
tab: "structure",
info: null,
};
const tabs: Array<{ id: TabId; label: string }> = [
{ id: "structure", label: "目录解剖" },
{ id: "modes", label: "开发 vs 生产" },
{ id: "scripts", label: "构建脚本" },
];
function escapeHtml(value: string): string {
return value
.replaceAll("&", "&")
.replaceAll("<", "<")
.replaceAll(">", ">")
.replaceAll('"', """);
}
function row(label: string, value: string, testid?: string): string {
return `<tr><td>${escapeHtml(label)}</td><td><code${testid ? ` data-testid="${testid}"` : ""}>${escapeHtml(value)}</code></td></tr>`;
}
function structure(info: ProjectInfo): string {
const files: Array<[string, string]> = [
["forge.config.ts", "Forge 8 + Vite 插件"],
["vite.main.config.mts", "主进程构建"],
["vite.preload.config.mts", "preload 构建(preload.cjs)"],
["vite.renderer.config.mts", "渲染进程构建"],
["main/index.ts", "主进程入口"],
["preload/index.ts", "桥"],
["shared/ipc.ts", "通道与类型"],
["src/renderer.ts", "页面脚本"],
];
return `<div class="section">
<h2>应用信息</h2>
<table class="table" data-testid="structure-table">
<tbody>
${row("名称", info.appName)}
${row("版本", info.appVersion)}
${row("appPath", info.appPath, "info-app-path")}
${row("resourcesPath", info.resourcesPath, "info-resources")}
</tbody>
</table>
</div>
<div class="section">
<h2>关键文件</h2>
<div class="layers">
${files.map(([file, role]) => `<div class="layer"><strong>${escapeHtml(file)}</strong><span>${escapeHtml(role)}</span></div>`).join("")}
</div>
</div>`;
}
function modes(info: ProjectInfo): string {
return `<div class="section">
<h2>两种运行方式</h2>
<table class="table" data-testid="mode-table">
<thead><tr><th>项</th><th>当前值</th></tr></thead>
<tbody>
${row("isPackaged", String(info.isPackaged))}
${row("有 dev server URL", String(info.hasDevServerUrl))}
${row("页面来源", info.hasDevServerUrl ? "http://localhost(Vite)" : "本地文件(.vite/renderer)")}
</tbody>
</table>
<p class="hint">开发时 HMR 来自 Vite;打包后没有 dev server,路径与 CSP 都要按本地文件处理。</p>
</div>`;
}
function scripts(info: ProjectInfo): string {
const entries = Object.entries(info.scripts);
return `<div class="section">
<h2>package.json scripts</h2>
<table class="table" data-testid="scripts-table">
<thead><tr><th>脚本</th><th>命令</th></tr></thead>
<tbody>
${entries.map(([name, command]) => `<tr><td><code>${escapeHtml(name)}</code></td><td><code>${escapeHtml(command)}</code></td></tr>`).join("")}
</tbody>
</table>
<p class="hint">start 走开发服务器;package 生成可运行目录;make 生成安装包(本章只用到 package)。</p>
</div>`;
}
function render(): void {
const root = document.querySelector<HTMLDivElement>("#app");
if (!root) return;
const info = state.info;
const body = !info
? `<p class="hint">正在读取应用信息…</p>`
: state.tab === "structure"
? structure(info)
: state.tab === "modes"
? modes(info)
: scripts(info);
root.innerHTML = `
<header class="header">
<div>
<h1 data-testid="app-title">第一个应用</h1>
<p class="subtitle">Forge + Vite + TypeScript:三端目录、开发/生产、构建脚本</p>
</div>
<nav class="tabs">
${tabs
.map(
(tab) =>
`<button class="tab${state.tab === tab.id ? " active" : ""}" data-tab="${tab.id}" data-testid="tab-${tab.id}">${tab.label}</button>`,
)
.join("")}
</nav>
</header>
<main class="content">${body}</main>`;
for (const button of root.querySelectorAll<HTMLButtonElement>("[data-tab]")) {
button.addEventListener("click", () => {
const tab = button.dataset.tab;
if (tab === "structure" || tab === "modes" || tab === "scripts") {
state.tab = tab;
render();
}
});
}
}
async function boot(): Promise<void> {
state.info = await window.api.info();
render();
window.__setTab = (tab) => {
state.tab = tab;
render();
};
window.__snapshot = () => ({
tab: state.tab,
isPackaged: state.info?.isPackaged,
scripts: Object.keys(state.info?.scripts ?? {}).length,
});
}
void boot();