示例工程 02-first-app

02-first-app/app/.tsc/src/renderer.js

javascript · 4.4 KB

import "./styles.css";
const state = {
    tab: "structure",
    info: null,
};
const tabs = [
    { id: "structure", label: "目录解剖" },
    { id: "modes", label: "开发 vs 生产" },
    { id: "scripts", label: "构建脚本" },
];
function escapeHtml(value) {
    return value
        .replaceAll("&", "&")
        .replaceAll("<", "&lt;")
        .replaceAll(">", "&gt;")
        .replaceAll('"', "&quot;");
}
function row(label, value, testid) {
    return `<tr><td>${escapeHtml(label)}</td><td><code${testid ? ` data-testid="${testid}"` : ""}>${escapeHtml(value)}</code></td></tr>`;
}
function structure(info) {
    const files = [
        ["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) {
    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) {
    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() {
    const root = document.querySelector("#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("[data-tab]")) {
        button.addEventListener("click", () => {
            const tab = button.dataset.tab;
            if (tab === "structure" || tab === "modes" || tab === "scripts") {
                state.tab = tab;
                render();
            }
        });
    }
}
async function boot() {
    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();
//# sourceMappingURL=renderer.js.map