Open jthrilly opened 1 year ago
Hey, thanks for the thorough detail here and the considerations for fixing this. It seems to me that both of these solutions could be available going forward, where users pre-processing their preload file could use the current export, and users that are authoring a preload file directly could use a snippet.
I'm going to work on adding an example that's non-vite to the examples which should demonstrate the issue, and make the requisite changes.
Thanks again!
Hello, any update on this? :)
Since Electron 20, the
sandbox
option of windowwebPreferences
has been set to true by default. This means that preload scripts can only use a small subset of the Electron and Node APIs. Specifically,require
is polyfilled with a version that will only allow requiring a small subset of node core modules.This causes a problem for this lib, since we need to require
electron-trpc/main
in the preload script, and then callexposeElectronTRPC()
.Disabling the sandbox of course resolves this issue, but at the cost of losing the security benefits of sandboxing.
After a quick think about this I have two potential solutions:
contextBridge
code. This should be a documentation only change, perhaps based on a samplevite-plugin-electron
config.ELECTRON_TRPC_CHANNEL
and make it up to the user to configure an appropriate and consistent channel across main, preload, and renderer. Then supply a copy/paste snippet for the preload file that only uses thecontextBridge
API. It would be helpful to export theRendererGlobalElectronTRPC
type so consumers could at least match up with some sort of package API.There may be other options I haven't considered.
Thanks for creating such a useful lib!