-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* feat: add initial source code * docs: initial update for readme * test: add configrations for test and source code * chore: add generation logic for the dts file * chore: remove deep-equal * docs: add jsdocs to index.d.ts
- Loading branch information
Showing
15 changed files
with
949 additions
and
203 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,2 +1,69 @@ | ||
# electron-context-bridge | ||
|
||
Generate api on the bridge across isolated contexts of the electron. | ||
|
||
# Use | ||
|
||
## install | ||
|
||
``` | ||
npm install --save-dev electron-context-bridge | ||
``` | ||
|
||
# Implimentation | ||
|
||
1. create api on main script | ||
|
||
```ex. main/api.ts | ||
const api = { | ||
hello: (to: string)=>console.log(`hellow ${to}!`), | ||
calc: { | ||
add: (a:number, b:number) => a + b, | ||
minus: (a:number, b:number) => a - b, | ||
} | ||
} as const | ||
|
||
export type IpcBridgeApi = IpcBridgeApiTypeGenerator<typeof api> | ||
|
||
``` | ||
|
||
1. add handler at main.ts | ||
|
||
``` | ||
registerIpcHandler(api) | ||
``` | ||
|
||
1. add invoker at preload.ts | ||
|
||
``` | ||
const api = await getApiInvoker() | ||
if (process.contextIsolated) { | ||
try { | ||
contextBridge.exposeInMainWorld('electron', electronAPI) | ||
contextBridge.exposeInMainWorld('api', api) | ||
} catch (error) { | ||
console.error(error) | ||
} | ||
} else { | ||
window.electron = electronAPI | ||
window.api = api | ||
} | ||
``` | ||
|
||
1. add type decolation | ||
|
||
``` | ||
import type { IpcBridgeApi } from '@main/api' | ||
import type { ElectronAPI } from '@electron-toolkit/preload' | ||
declare global { | ||
interface Window { | ||
electron: ElectronAPI | ||
api: IpcBridgeApi | ||
} | ||
} | ||
``` |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -39,6 +39,8 @@ | |
"scripts": { | ||
"format": "prettier --write .", | ||
"lint": "eslint . --fix", | ||
"test": "vitest --run", | ||
"test:watch": "vitest", | ||
"coverage": "vitest run --coverage", | ||
"typecheck": "tsc --noEmit -p tsconfig.json --composite false", | ||
"build": "tsc --noEmit && rollup --config rollup.config.ts --configPlugin typescript" | ||
|
@@ -48,27 +50,25 @@ | |
"license": "MIT", | ||
"packageManager": "[email protected]", | ||
"peerDependencies": { | ||
"electron": "^32.0.0" | ||
"electron": "^32.0.0 || ^33.0.0" | ||
}, | ||
"devDependencies": { | ||
"@eslint/js": "^9.12.0", | ||
"@rollup/plugin-json": "^6.1.0", | ||
"@rollup/plugin-typescript": "^12.1.0", | ||
"@types/deep-equal": "^1.0.4", | ||
"@types/eslint-config-prettier": "^6.11.3", | ||
"@types/eslint__js": "^8.42.3", | ||
"@types/node": "^22.7.5", | ||
"@vitest/coverage-v8": "^2.1.3", | ||
"deep-equal": "^2.2.3", | ||
"electron": "^32.2.0", | ||
"eslint": "^9.12.0", | ||
"eslint-config-prettier": "^9.1.0", | ||
"eslint-plugin-import": "^2.31.0", | ||
"eslint-plugin-unicorn": "^56.0.0", | ||
"prettier": "^3.3.3", | ||
"rollup": "^4.24.0", | ||
"rollup-plugin-copy": "^3.5.0", | ||
"rollup-plugin-delete": "^2.1.0", | ||
"rollup-plugin-dts": "^6.1.1", | ||
"rollup-plugin-node-externals": "^7.1.3", | ||
"tslib": "^2.7.0", | ||
"typescript": "^5.6.3", | ||
|
Oops, something went wrong.