How antelm-dev/electron-ipc-module works
Purpose and package surfaces
electron-ipc-module is an ESM TypeScript library for modular, type-safe Electron main/renderer IPC. It combines main-process channel registration and lifecycle management with static generation of a restricted, typed preload bridge. Public runtime exports originate at src/index.ts; command-line use enters through src/cli.ts; Rollup/Vite integration enters through src/rollup-plugin.ts.
Main-process IPC module runtime
defineIpcModule and channel helpers model prefixed handler/listener registrations over Electron ipcMain. They support request/response (handle, handleOnce), fire-and-forget (listen, listenOnce), typed emitted events, authorization, validation, cleanup, and registration rollback.
• src/runtime/ipc-module.ts
• src/shared/types/runtime.ts
• src/shared/types/bridge.ts
The key boundary is Electron structured-clone serialization: channel argument and handler-return types are constrained before code reaches the generated renderer API. Runtime authorization and validation remain necessary because renderer types are not trusted at runtime.
Module lifecycle and collision control
The IPC container owns multiple named module registrations and serializes all lifecycle mutations through a FIFO queue. It loads/replaces modules, offers transactional loadAll, unloads/disposes registrations, tracks committed channel names, and prevents physical-channel collisions across modules. Observer failures are isolated from lifecycle operations.
• src/runtime/ipc-container.ts
• src/runtime/ipc-module.ts
Control flow: application main code builds modules with injected services, then calls container load/loadAll; the container invokes module registration against Electron IPC and retains cleanup callbacks/channels only after successful commitment.
Static bridge analysis
The analyzer uses the TypeScript compiler at runtime to discover and inspect *.ipc.ts files. It extracts module names, channel definitions, argument/return types, and declared emitted events; it reports unsupported or ambiguous source shapes and scopes type diagnostics to sources reachable from IPC modules.
• src/bridge/ipc-bridge-analyzer.ts
• src/shared/ts-utils.ts
• src/shared/types/bridge.ts
This is a build-time boundary: exactly declared module APIs become renderer-accessible; arbitrary ipcRenderer access is not exposed.
Bridge generation and preload boundary
The generator converts analyzed IPC metadata into TypeScript defining a nested bridge: invoke wrappers for handlers, send wrappers for listeners, and subscription/unsubscription wrappers for events. Optional exposure emits contextBridge.exposeInMainWorld and a matching Window declaration.
• src/bridge/ipc-bridge-generator.ts
• src/bridge/ipc-bridge.ts
• example/generated/ipc-bridge.ts
Generated code is intended for Electron preload, then must be bundled as a self-contained CommonJS artifact when sandboxed. Electron is the material external runtime; expected security settings are context isolation enabled and Node integration disabled.
Build and CLI integration
The Rollup plugin invokes generation during builds and watches IPC sources; its compatible plugin shape also supports Vite. The CLI provides standalone generate, stale-output check, and watch workflows, reusing the same analysis/generation pipeline.
• src/rollup-plugin.ts
• src/cli.ts
• src/bridge/ipc-bridge-generator.ts
Runnable integration example
The example demonstrates the full application flow: main defines/loads IPC modules, generated bridge is imported by preload and exposed to the renderer, and renderer calls only that bridge.
• example/main.ts
• example/ipc/greeting.ipc.ts
• example/preload.ts
• example/rollup.config.js
Main entry points: ipc-module.ts IPC module · ipc-container.ts IPC container · ipc-bridge-analyzer.ts IPC bridge analyzer · ipc-bridge.ts Bridge orchestration
Components 18
Main-process runtime
- Public runtime API · Library runtime entry point.
src/index.ts - IPC module · Defines prefixed handlers, listeners, events, authorization, and validation.
src/runtime/ipc-module.ts - IPC container · Serializes module lifecycle changes and commits collision-free registrations.
src/runtime/ipc-container.ts - Runtime IPC types · Constrains channel payloads and returns to structured-clone-safe values.
src/shared/types/runtime.ts - Electron ipcMain
Bridge toolchain
- Bridge type contracts
src/shared/types/bridge.ts - CLI · Provides generate, stale-check, and watch workflows.
src/cli.ts - Rollup/Vite plugin · Generates and watches bridge output during builds.
src/rollup-plugin.ts - IPC bridge analyzer · Discovers IPC modules and extracts their declared API metadata.
src/bridge/ipc-bridge-analyzer.ts - TypeScript utilities
src/shared/ts-utils.ts - Bridge generator · Emits invoke, send, and event subscription bridge wrappers.
src/bridge/ipc-bridge-generator.ts - Bridge orchestration
src/bridge/ipc-bridge.ts
Electron example
- Greeting IPC declaration · Statically declared renderer-accessible API.
example/ipc/greeting.ipc.ts - Example main process · Builds modules with services and loads them into the container.
example/main.ts - Generated preload bridge · Restricted nested renderer API, optionally exposed through contextBridge.
example/generated/ipc-bridge.ts - Example preload · Imports the generated bridge before renderer execution.
example/preload.ts - Renderer application · Uses only the exposed typed bridge.
- Example Rollup build · Bundles preload as a self-contained CommonJS artifact.
example/rollup.config.js
Connections 21
- Public runtime API → IPC module: exports
- Public runtime API → IPC container: exports
- IPC module → Runtime IPC types: uses typed contracts
- IPC module → Bridge type contracts: aligns declared API
- IPC container → IPC module: loads and unloads
- IPC container → Electron ipcMain: registers committed channels
- Example main process → IPC module: defines
- Example main process → IPC container: load / loadAll
- CLI → Bridge orchestration: runs
- Rollup/Vite plugin → Bridge orchestration: runs during build
- Bridge orchestration → IPC bridge analyzer: analyzes IPC sources
- Bridge orchestration → Bridge generator: generates bridge
- IPC bridge analyzer → TypeScript utilities: uses compiler helpers
- IPC bridge analyzer → Bridge type contracts: produces metadata
- Greeting IPC declaration → IPC bridge analyzer: analyzed
- Bridge generator → Generated preload bridge: emits
- Example Rollup build → Rollup/Vite plugin: configures
- Example Rollup build → Example preload: bundles
- Example preload → Generated preload bridge: imports
- Generated preload bridge → Renderer application: exposes restricted API
- Renderer application → Electron ipcMain: invokes declared channels