GV

GeezerVison

The World Wide Web, restored to 800 pixels of glory.

Home|About|Gaming cabinet|Archives|How it works|Starter sites

On this site

  • Sign in
  • ActionsCI/CD and automation
  • CodespacesCloud development environments
  • All Features
  • Documentation
  • Training
  • Certifications
  • Build with Copilot
  • Customize Copilot
  • Integrations
  • Trending repos
  • GitHub Sponsors
  • Developer articles
  • Enterprises

This view

  • Original page
  • New address

GeezerVison is showing github.com — GitHub - cawa-93/dts-for-context-bridge: Generate `.d.ts` file for Electron `contextBridge.exposeInMainWorld` calls

This repository was archived by the owner on Feb 24, 2022. It is now read-only.
BranchesTags
Open more actions menu

Latest commit

 

History

61 Commits
61 Commits

Folders and files

NameName
Last commit message
Last commit date
.github/workflows
.github/workflows
 
 
bin
bin
 
 
examples
examples
 
 
scripts
scripts
 
 
src
src
 
 
tests
tests
 
 
.editorconfig
.editorconfig
 
 
.gitignore
.gitignore
 
 
LICENSE
LICENSE
 
 
README.md
README.md
 
 
global.d.ts
global.d.ts
 
 
package-lock.json
package-lock.json
 
 
package.json
package.json
 
 

Repository files navigation

dts-cb


Archived!

As a result of Russia's military invasion of Ukraine, I have neither the time nor the strength to continue working. 😥

I will restore this template when the war in Ukraine is over.🙏


This utility is designed to analyze context bridges in Electron application and create .d.ts files.

Problem

In one part of your application, you describe some API and expose it into the global context of the renderer.

contextBridge.exposeInMainWorld(
  'electron',
  {
    doThing: () => ipcRenderer.send('do-a-thing')
  }
)

However, for the TypeScript in the renderer knows nothing about the new global api.

// Property 'electron' does not exist on type 'Window & typeof globalThis'.
window.electron.doThing()

Therefore, you must somehow register these apps.

Decision

This utility scans the source code and finds all exposeInMainWorld calls. Then it generates a .d.ts file with an interface that can later be connected in the renderer.

contextBridge.exposeInMainWorld(
  'electron',
  {
    doThing: () => ipcRenderer.send('do-a-thing')
  }
)
// generated.d.ts
interface Window {
    readonly electron: {doThing: () => void}
}

See more examples.

Installation

# global
npm i -g dts-for-context-bridge

# for project 
npm i dts-for-context-bridge

Usage

# You can use glob pattern as input
dts-cb --input="preload/src/**/*.ts" --output="preload/exposed.d.ts"

# You can use tsconfig.json as input
dts-cb --input="preload/tsconfig.json" --output="preload/exposed.d.ts"

See all options by

dts-cb --help

Programmatic usage

import {generate} from 'dts-for-context-bridge'

generate({
    input: 'preload/src/**/*.ts', // Or path to tsconfig.json
    output: 'preload/exposed.d.ts',
})

Is it stable?

The utility relies on AST analysis and currently does not cover all cases. Feel free to create PR.

About

Generate `.d.ts` file for Electron `contextBridge.exposeInMainWorld` calls

npmjs.com/package/dts-for-context-bridge

Topics

electrontypescript

Resources

Readme
MIT license
Activity

Stars

9 stars

Watchers

1 watching

Forks

2 forks
Report repository

Packages

Used by

Contributors

Languages

You are visitor 01402792 — GeezerVison does not store the pages you look up.
Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required.