sindresorhus / electron-better-ipc

Simplified IPC communication for Electron apps
MIT License
715 stars 60 forks source link

Issue when building outside renderer process #18

Closed neist closed 5 years ago

neist commented 5 years ago

Hi

First of all, thank you so much for this very convenient IPC wrapper πŸ™πŸ»

I've just integrated it to my project and everything is working flawlessly... almost!

Unfortunately I'm having an issue when building my renderer project.

This line: https://github.com/sindresorhus/electron-better-ipc/blob/e78c355db4437da8d0cacca0486aa37fceee7205/source/renderer.js#L6

Results in: Object prototype may only be an Object or null: undefined

This happens because const {ipcRenderer} = electron; is resulting in undefined when running outside the renderer process - for example when building / prerendering ☺️

Could be the same issue causing #15

neist commented 5 years ago

I'm using Next.js by the way 😊 Would you be interested in a pull request?

neist commented 5 years ago

A solution could be:

Renderer

Change this: https://github.com/sindresorhus/electron-better-ipc/blob/e78c355db4437da8d0cacca0486aa37fceee7205/source/renderer.js#L6

To this:

const ipc = Object.create(ipcRenderer || {});

Main

Change this: https://github.com/sindresorhus/electron-better-ipc/blob/e78c355db4437da8d0cacca0486aa37fceee7205/source/main.js#L6

To this:

const ipc = Object.create(ipcMain || {});

This allows for pre-rendering / SSR such as Next.js ☺️

bkniffler commented 5 years ago

Experiencing the same, would love to have this fixed! @neist suggestion looks reasonable.