rollup uses require() with multiple inputs
When building an app with multiple inputs:
build: {
rollupOptions: {
input: {
index: path.resolve(__dirname, 'index.html'),
panel: path.resolve(__dirname, 'panel.html')
}
}
},
rollup automatically creates a shared app.<hash>.js file with all the common modules and requires it from both entry files.
However with Electron properly configured, require is not available in the renderer, so the app doesn't work. What is the correct way to make this work, either by inlining the common modules in both (larger file size) or selectively allowing require (I'd rather not?).
May be neet remove the following options.
https://github.com/electron-vite/electron-vite-react/blob/63cef8829995d1309acbc4a799240cfca177f9e4/vite.config.ts#L48
This options will cause Rollup to build code in cjs format. 👉 Config presets (Opinionated)
After removing this option, Rollup will use the esm format to build the code by default, which can avoid the require in the bundle code.
Unfortunately, some of Electrons scripts use path and those (specifically, node_modules/electron/index.js) get included into the app.<hash>.js bundle, causing an error because path is not available with node integration turned off.
Edit: I am using nativeImage, which according to the Electron docs can be used in the renderer process (so even with nodeIntegration turned off, as I currently do with webpack) but with Vite, that fails with the "path" error:
Uncaught Error: Module "path" has been externalized for browser compatibility. Cannot access "path.join" in client code.
at Object.get (browser-external:path:9:13)
at node_modules/electron/index.js (index.js:4:23)
at __require (chunk-CYSXIT7F.js?v=b28474e4:8:50)
at dep:electron:1:16
Edit2: Seems I can work around this by making nativeImage available in the preload script and using it from there.
Bottom line: It would be nice if multiple entry points also worked with the electron-vite-renderer module.
In fact, if nodeIntegration is not enabled, even the require function cannot be used, although we can use the following to avoid Vite Pre-Bundling node_modules/electron/index.js, but in the where the require function is not available Down. We still can't use any exported members of electron.
// e.g. renderer.js
const { nativeTheme } = require('electron')
// Uncaught ReferenceError: require is not defined
Now [email protected] is based on the vite-electron-plugin, This is broke!