Skip to content
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

rollup uses require() with multiple inputs #58

Closed
Kilian opened this issue Aug 12, 2022 · 4 comments
Closed

rollup uses require() with multiple inputs #58

Kilian opened this issue Aug 12, 2022 · 4 comments

Comments

@Kilian
Copy link

Kilian commented Aug 12, 2022

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?).

@caoxiemeihao
Copy link
Member

caoxiemeihao commented Aug 13, 2022

May be neet remove the following options.

renderer: {},

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.

@Kilian
Copy link
Author

Kilian commented Aug 15, 2022

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.

@caoxiemeihao
Copy link
Member

caoxiemeihao commented Aug 15, 2022

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

@caoxiemeihao
Copy link
Member

Now electron-vite-react@2.1.0 is based on the vite-electron-plugin, This is broke!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants