Skip to content

Commit ddc11cb

Browse files
committed
Use pnpm workspaces, add working example. (#29)
* Use pnpm workspaces, add working example. * Add example build to CI workflow. * Add changeset for fix.
1 parent c30f44a commit ddc11cb

30 files changed

+751
-198
lines changed

.changeset/sharp-crews-melt.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
'electron-trpc': patch
3+
---
4+
5+
Fix transformer path.

.github/workflows/main.yml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ jobs:
1919
cache: 'pnpm'
2020
- run: pnpm install --frozen-lockfile
2121
- run: pnpm build
22+
- run: pnpm --filter=examples/basic build
2223
- run: pnpm test:coverage
2324
- uses: codecov/codecov-action@v3
2425

.gitignore

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,7 @@
11
node_modules
2-
/dist
3-
/coverage
2+
examples/*/dist
3+
examples/*/coverage
4+
examples/*/*/dist
5+
examples/*/*/coverage
6+
packages/*/dist
7+
packages/*/coverage

examples/basic/main/api.ts

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
import z from 'zod';
2+
import { initTRPC } from '@trpc/server';
3+
4+
const t = initTRPC.create({ isServer: true });
5+
6+
export const router = t.router({
7+
greeting: t.procedure.input(z.object({ name: z.string() })).query((req) => {
8+
const { input } = req;
9+
10+
return {
11+
text: `Hello ${input.name}` as const,
12+
};
13+
}),
14+
});
15+
16+
export type AppRouter = typeof router;

examples/basic/main/main.ts

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
import path from 'path';
2+
import { app, ipcMain, BrowserWindow } from 'electron';
3+
import { createIPCHandler } from 'electron-trpc';
4+
import { router } from './api';
5+
6+
app.on('ready', () => {
7+
createIPCHandler({ ipcMain, router: router as any });
8+
9+
const win = new BrowserWindow({
10+
webPreferences: {
11+
preload: path.resolve(__dirname, 'preload.js'),
12+
},
13+
});
14+
15+
win.loadFile(path.resolve(__dirname, '../../renderer/dist/index.html'));
16+
17+
win.show();
18+
});

examples/basic/main/preload.ts

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
import { contextBridge, ipcRenderer } from 'electron';
2+
import { exposeElectronTRPC } from 'electron-trpc';
3+
4+
process.once('loaded', async () => {
5+
exposeElectronTRPC({ contextBridge, ipcRenderer });
6+
});

examples/basic/main/tsconfig.json

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
{
2+
"compilerOptions": {
3+
"alwaysStrict": true,
4+
"esModuleInterop": true,
5+
"forceConsistentCasingInFileNames": true,
6+
"isolatedModules": true,
7+
"lib": ["dom", "es2017"],
8+
"module": "commonjs",
9+
"moduleResolution": "node",
10+
"noFallthroughCasesInSwitch": true,
11+
"noUnusedLocals": true,
12+
"noUnusedParameters": true,
13+
"outDir": "dist",
14+
"resolveJsonModule": true,
15+
"strict": true,
16+
"target": "esnext"
17+
},
18+
"include": ["./*.ts"],
19+
"exclude": ["node_modules"]
20+
}

examples/basic/package.json

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
{
2+
"name": "examples/basic",
3+
"description": "Electron support for tRPC",
4+
"version": "0.0.0",
5+
"private": true,
6+
"main": "main/dist/main.js",
7+
"license": "MIT",
8+
"scripts": {
9+
"start": "electron .",
10+
"build": "tsc -p main && tsc -p renderer && vite build renderer "
11+
},
12+
"dependencies": {
13+
"@tanstack/react-query": "^4.8.0",
14+
"@trpc/client": "10.0.0-proxy-beta.25",
15+
"@trpc/react": "10.0.0-proxy-beta.11",
16+
"@trpc/react-query": "10.0.0-proxy-beta.25",
17+
"@trpc/server": "10.0.0-proxy-beta.25",
18+
"electron": "^19.0.9",
19+
"electron-trpc": "0.2.0-next.2",
20+
"react": "^18.2.0",
21+
"react-dom": "^18.2.0",
22+
"zod": "^3.19.1"
23+
},
24+
"devDependencies": {
25+
"@types/node": "^17.0.10",
26+
"@types/react": "^18.0.21",
27+
"@types/react-dom": "^18.0.6",
28+
"@vitejs/plugin-react": "^2.1.0",
29+
"vite": "^3.0.3"
30+
}
31+
}

examples/basic/renderer/index.html

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
<!DOCTYPE html>
2+
<html>
3+
<head>
4+
<meta charset="UTF-8" />
5+
<!-- https://developer.mozilla.org/en-US/docs/Web/HTTP/CSP -->
6+
<meta http-equiv="Content-Security-Policy" content="default-src 'self'; script-src 'self'" />
7+
<meta http-equiv="X-Content-Security-Policy" content="default-src 'self'; script-src 'self'" />
8+
<title>Hello from Electron renderer!</title>
9+
</head>
10+
<body>
11+
<div id="react-root"></div>
12+
</body>
13+
<script type="module" src="./index"></script>
14+
</html>

examples/basic/renderer/index.tsx

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
import React, { useState } from 'react';
2+
import ReactDom from 'react-dom';
3+
import { ipcLink } from 'electron-trpc';
4+
import { createTRPCReact } from '@trpc/react-query';
5+
import { QueryClient, QueryClientProvider } from '@tanstack/react-query';
6+
import type { AppRouter } from '../main/api';
7+
8+
const trpcReact = createTRPCReact<AppRouter>();
9+
10+
function App() {
11+
const [queryClient] = useState(() => new QueryClient());
12+
const [trpcClient] = useState(() =>
13+
trpcReact.createClient({
14+
links: [ipcLink()],
15+
})
16+
);
17+
18+
return (
19+
<trpcReact.Provider client={trpcClient} queryClient={queryClient}>
20+
<QueryClientProvider client={queryClient}>
21+
<HelloElectron />
22+
</QueryClientProvider>
23+
</trpcReact.Provider>
24+
);
25+
}
26+
27+
function HelloElectron() {
28+
const { data } = trpcReact.greeting.useQuery({ name: 'Electron' });
29+
30+
if (!data) {
31+
return null;
32+
}
33+
34+
return <div>{data.text}</div>;
35+
}
36+
37+
ReactDom.render(<App />, document.getElementById('react-root'));

0 commit comments

Comments
 (0)