-
Notifications
You must be signed in to change notification settings - Fork 29
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
process is not defined
after importing ipcLink in renderer
#68
Comments
Hi, sorry for getting back to you late. Can you clarify for me which versions of If you were running this off the Today I've cut the 0.2.0 release of |
I installed a new version 0.2.0 and I faced an issue with Electron React Boilerplate webpack's build step which is run always after adding a new package:
If I add electron-trpc to webpack's |
I added I'm also able to get a working query using just the example in the project here. I'm adding the diff below, if you want to see what that looks like. If you're still having issues, maybe you can push your project to a repo I can pull down to reproduce? diff --git a/src/main/main.ts b/src/main/main.ts
index 327b81f..d3137c4 100644
--- a/src/main/main.ts
+++ b/src/main/main.ts
@@ -14,6 +14,8 @@ import { autoUpdater } from 'electron-updater';
import log from 'electron-log';
import MenuBuilder from './menu';
import { resolveHtmlPath } from './util';
+import { createIPCHandler } from 'electron-trpc/main';
+import { router } from './router';
class AppUpdater {
constructor() {
@@ -25,12 +27,6 @@ class AppUpdater {
let mainWindow: BrowserWindow | null = null;
-ipcMain.on('ipc-example', async (event, arg) => {
- const msgTemplate = (pingPong: string) => `IPC test: ${pingPong}`;
- console.log(msgTemplate(arg));
- event.reply('ipc-example', msgTemplate('pong'));
-});
-
if (process.env.NODE_ENV === 'production') {
const sourceMapSupport = require('source-map-support');
sourceMapSupport.install();
@@ -128,6 +124,7 @@ app
.whenReady()
.then(() => {
createWindow();
+ createIPCHandler({ router });
app.on('activate', () => {
// On macOS it's common to re-create a window in the app when the
// dock icon is clicked and there are no other windows open.
diff --git a/src/main/preload.ts b/src/main/preload.ts
index d35fdfe..5e4212a 100644
--- a/src/main/preload.ts
+++ b/src/main/preload.ts
@@ -1,23 +1,5 @@
-import { contextBridge, ipcRenderer, IpcRendererEvent } from 'electron';
+import { exposeElectronTRPC } from 'electron-trpc/main';
-export type Channels = 'ipc-example';
-
-contextBridge.exposeInMainWorld('electron', {
- ipcRenderer: {
- sendMessage(channel: Channels, args: unknown[]) {
- ipcRenderer.send(channel, args);
- },
- on(channel: Channels, func: (...args: unknown[]) => void) {
- const subscription = (_event: IpcRendererEvent, ...args: unknown[]) =>
- func(...args);
- ipcRenderer.on(channel, subscription);
-
- return () => {
- ipcRenderer.removeListener(channel, subscription);
- };
- },
- once(channel: Channels, func: (...args: unknown[]) => void) {
- ipcRenderer.once(channel, (_event, ...args) => func(...args));
- },
- },
+process.once('loaded', () => {
+ exposeElectronTRPC();
});
diff --git a/src/renderer/App.tsx b/src/renderer/App.tsx
index eb321c5..a58e09a 100644
--- a/src/renderer/App.tsx
+++ b/src/renderer/App.tsx
@@ -1,14 +1,24 @@
+import { useState } from 'react';
import { MemoryRouter as Router, Routes, Route } from 'react-router-dom';
+import { createTRPCReact } from '@trpc/react-query';
+import { QueryClient, QueryClientProvider } from '@tanstack/react-query';
+import { ipcLink } from 'electron-trpc/renderer';
+import type { AppRouter } from '../main/router';
import icon from '../../assets/icon.svg';
import './App.css';
+const trpcReact = createTRPCReact<AppRouter>();
+
const Hello = () => {
+ const { data } = trpcReact.greeting.useQuery();
+
return (
<div>
<div className="Hello">
<img width="200" alt="icon" src={icon} />
</div>
<h1>electron-react-boilerplate</h1>
+ <h2>From backend: {data?.text}</h2>
<div className="Hello">
<a
href="https://electron-react-boilerplate.js.org/"
@@ -40,11 +50,21 @@ const Hello = () => {
};
export default function App() {
+ const [queryClient] = useState(() => new QueryClient());
+
+ const [trpcClient] = useState(() =>
+ trpcReact.createClient({ links: [ipcLink()] })
+ );
+
return (
- <Router>
- <Routes>
- <Route path="/" element={<Hello />} />
- </Routes>
- </Router>
+ <trpcReact.Provider client={trpcClient} queryClient={queryClient}>
+ <QueryClientProvider client={queryClient}>
+ <Router>
+ <Routes>
+ <Route path="/" element={<Hello />} />
+ </Routes>
+ </Router>
+ </QueryClientProvider>
+ </trpcReact.Provider>
);
}
diff --git a/src/renderer/index.tsx b/src/renderer/index.tsx
index a96350e..b7b48c1 100644
--- a/src/renderer/index.tsx
+++ b/src/renderer/index.tsx
@@ -4,10 +4,3 @@ import App from './App';
const container = document.getElementById('root')!;
const root = createRoot(container);
root.render(<App />);
-
-// calling IPC exposed from preload script
-window.electron.ipcRenderer.once('ipc-example', (arg) => {
- // eslint-disable-next-line no-console
- console.log(arg);
-});
-window.electron.ipcRenderer.sendMessage('ipc-example', ['ping']); |
I also tried it on fresh ERB repo and both errors still occur.
Could you try to run
I hope it will work but IDE shows it can't find type declarations. I'm guessing it happens because of the lack of .d.ts in the package. Correct me if I am wrong. |
I see, so there's two separate issues here. One is in how that project's diff --git a/.erb/configs/webpack.config.renderer.dev.dll.ts b/.erb/configs/webpack.config.renderer.dev.dll.ts
index 614b90f..fd1bdff 100644
--- a/.erb/configs/webpack.config.renderer.dev.dll.ts
+++ b/.erb/configs/webpack.config.renderer.dev.dll.ts
@@ -31,7 +31,9 @@ const configuration: webpack.Configuration = {
module: require('./webpack.config.renderer.dev').default.module,
entry: {
- renderer: Object.keys(dependencies || {}),
+ renderer: Object.keys(dependencies || {}).map((dep) =>
+ dep === 'electron-trpc' ? 'electron-trpc/renderer' : dep
+ ),
},
output: { The other issue is a mistake on my part, the Your setup may need to be tweaked to take advantage of this, however. When using the updated package in my ERB setup I had to update |
Thank you for your help, now it works. Also, I don't have any issues with |
I have a little problem with automatic imports in VSC after changing Do you have any idea how to handle it? I never used Edit: It can also be a little bit annoying for other users of this package to do all of those setups so here are a few ideas:
|
Another solution to stick with "paths": {
"electron-trpc/main": ["../node_modules/electron-trpc/dist/main"],
"electron-trpc/renderer": ["../node_modules/electron-trpc/dist/renderer"],
}
But still, it needs to do some additional setup which feels like a workaround. |
|
I read more about exports and the node16 option. I also thought more about this case and I agree with you. I think we can close this as resolved. Thank you for your time. |
At first, that entire code works if I copy/paste it into my app. When I tried to use the npm package I faced with that issue:
I am using electron-react-boilerplate
I think it is caused because I am using
import { ipcLink } from 'electron-trpc';
. When I am trying to useimport { ipcLink } from 'electron-trpc/renderer';
I am getting:The text was updated successfully, but these errors were encountered: