# 05-preload-and-contextbridge / app 展示「页面能看到什么」:`window.api` 可用,`window.require` 与 `window.ipcRenderer` 不可用; 同时展示隔离世界的配置与 `contextBridge` 的传值边界。 ## 运行 ```bash pnpm --filter @tutorial/05-preload-and-contextbridge-app start ``` ## 验证 ```bash pnpm --filter @tutorial/05-preload-and-contextbridge-app smoke # {"evt":"smoke","ok":true,"hasApi":true,"hasPing":true,"hasRequire":false,"hasIpcRenderer":false,...} ``` ## 结构 - `main/`:窗口、IPC、smoke/shots;固定 `contextIsolation: true`、`sandbox: true` - `preload/`:通过 `contextBridge` 只暴露 `config()` 与 `ping()` - `shared/`:通道与类型 - `src/`:三个页签(暴露面 / 隔离世界 / 传值边界) ## testid | testid | 用途 | | --- | --- | | `app-title` | 页面标题 | | `tab-exposure` / `tab-isolation` / `tab-serialization` | 三个页签 | | `exposure-table` | 页面可见全局对象 | | `info-api` / `info-require` / `info-ipc` | 三个关键探针 | | `isolation-table` | contextIsolation/sandbox/nodeIntegration | | `ping-button` / `ping-result` | 跨桥调用 | ## 已知限制 - 只在 macOS arm64 验证;不同 Electron 版本的 `contextBridge` 细节可能变化。 - 示例只暴露两个方法;真实项目要按第 9 章的契约扩展。