Skip to content

Commit

Permalink
feat: initial code (#4)
Browse files Browse the repository at this point in the history
* 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
mato533 authored Oct 17, 2024
1 parent affac79 commit a8b4b78
Show file tree
Hide file tree
Showing 15 changed files with 949 additions and 203 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/test.yml
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
name: Run Test
run-name: 'TEST@${{github.ref_name}}: ${{github.event.head_commit.message}}'
run-name: 'TEST@${{github.ref_name}}'
on:
workflow_call:

Expand Down
67 changes: 67 additions & 0 deletions README.md
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
}
}
```
8 changes: 4 additions & 4 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand All @@ -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",
Expand Down
Loading

0 comments on commit a8b4b78

Please sign in to comment.