electron-skills

Compare original and translation side by side

🇺🇸

Original

English
🇨🇳

Translation

Chinese

Electron Skills for LlamaFarm Desktop

适用于LlamaFarm Desktop的Electron技能指南

Electron 28 + Electron Vite patterns for the LlamaFarm Desktop application.
为LlamaFarm Desktop应用提供Electron 28 + Electron Vite相关模式。

Overview

概述

This skill extends typescript-skills with Electron-specific patterns for main/renderer process architecture, IPC communication, security, and performance.
本技能在typescript-skills基础上扩展了Electron特有的模式,涵盖主进程/渲染进程架构、IPC通信、安全和性能相关内容。

Tech Stack

技术栈

ComponentTechnologyPurpose
FrameworkElectron 28Desktop application framework
Buildelectron-vite 2Vite-based build for main/preload/renderer
Updateselectron-updaterAuto-update via GitHub releases
Packagingelectron-builderCross-platform packaging (macOS/Win/Linux)
组件技术用途
框架Electron 28桌面应用框架
构建工具electron-vite 2基于Vite的主进程/预加载脚本/渲染进程构建工具
更新工具electron-updater通过GitHub发布版本实现自动更新
打包工具electron-builder跨平台打包(macOS/Win/Linux)

Architecture

架构

electron-app/
  src/
    main/           # Main process (Node.js context)
      index.ts      # App entry, lifecycle, IPC handlers
      backend/      # CLI installer, model downloader
      window-manager.ts
      menu-manager.ts
      logger.ts
    preload/        # Preload scripts (bridge context)
      index.ts      # contextBridge API exposure
    renderer/       # Renderer process (browser context)
      index.html    # Main window
      splash.html   # Splash screen
electron-app/
  src/
    main/           # 主进程(Node.js上下文)
      index.ts      # 应用入口、生命周期、IPC处理器
      backend/      # CLI安装器、模型下载器
      window-manager.ts
      menu-manager.ts
      logger.ts
    preload/        # 预加载脚本(桥接上下文)
      index.ts      # contextBridge API暴露
    renderer/       # 渲染进程(浏览器上下文)
      index.html    # 主窗口
      splash.html   # 启动屏

Core Principles

核心原则

  1. Process isolation - Main, preload, and renderer are separate contexts
  2. Context isolation - Always use
    contextBridge.exposeInMainWorld
  3. No Node in renderer -
    nodeIntegration: false
    always
  4. Type-safe IPC - Define channel types and payload schemas
  5. Secure by default - Minimize exposed APIs in preload
  1. 进程隔离 - 主进程、预加载脚本和渲染进程为独立上下文
  2. 上下文隔离 - 始终使用
    contextBridge.exposeInMainWorld
  3. 渲染进程禁用Node - 始终设置
    nodeIntegration: false
  4. 类型安全的IPC - 定义通道类型和负载模式
  5. 默认安全配置 - 最小化预加载脚本中暴露的API

Related Documents

相关文档

  • electron.md - IPC patterns, main/renderer communication
  • security.md - Context isolation, preload security, CSP
  • performance.md - Window management, memory, startup
  • electron.md - IPC模式、主进程/渲染进程通信
  • security.md - 上下文隔离、预加载脚本安全、CSP
  • performance.md - 窗口管理、内存、启动优化

Shared TypeScript Patterns

共享TypeScript模式

This skill inherits from typescript-skills:
  • patterns.md - Idiomatic TypeScript
  • typing.md - Strict typing, generics
  • security.md - Input validation, XSS prevention
本技能继承自typescript-skills
  • patterns.md - 惯用TypeScript模式
  • typing.md - 严格类型、泛型
  • security.md - 输入验证、XSS防护

Quick Reference

快速参考

IPC Handler Pattern (Main Process)

IPC处理器模式(主进程)

typescript
ipcMain.handle('cli:info', async () => {
  const isInstalled = await this.cliInstaller.isInstalled()
  return {
    isInstalled,
    path: isInstalled ? this.cliInstaller.getCLIPath() : null
  }
})
typescript
ipcMain.handle('cli:info', async () => {
  const isInstalled = await this.cliInstaller.isInstalled()
  return {
    isInstalled,
    path: isInstalled ? this.cliInstaller.getCLIPath() : null
  }
})

Preload Bridge Pattern

预加载桥接模式

typescript
const api = {
  cli: {
    getInfo: () => ipcRenderer.invoke('cli:info')
  },
  platform: process.platform,
  version: process.versions.electron
}

contextBridge.exposeInMainWorld('llamafarm', api)
typescript
const api = {
  cli: {
    getInfo: () => ipcRenderer.invoke('cli:info')
  },
  platform: process.platform,
  version: process.versions.electron
}

contextBridge.exposeInMainWorld('llamafarm', api)

BrowserWindow Configuration

BrowserWindow配置

typescript
new BrowserWindow({
  webPreferences: {
    preload: path.join(__dirname, '../preload/index.js'),
    nodeIntegration: false,
    contextIsolation: true
  }
})
typescript
new BrowserWindow({
  webPreferences: {
    preload: path.join(__dirname, '../preload/index.js'),
    nodeIntegration: false,
    contextIsolation: true
  }
})

Checklist Summary

检查清单摘要

CategoryCriticalHighMediumLow
IPC2321
Security4321
Performance1332
类别关键
IPC2321
安全4321
性能1332