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

Allow multiple preload configs or renderer configs #154

Open
4 tasks done
andersk opened this issue May 4, 2023 · 12 comments
Open
4 tasks done

Allow multiple preload configs or renderer configs #154

andersk opened this issue May 4, 2023 · 12 comments
Labels
enhancement New feature or request

Comments

@andersk
Copy link

andersk commented May 4, 2023

Clear and concise description of the problem

I’m working on an app that uses multiple preload scripts (one for the main window and one for embedded webviews). This works with vite-plugin-electron. I’m interested in trying electron-vite for better performance, but it seems electron-vite only supports one preload configuration.

Suggested solution

Allow preload and renderer to be arrays of Vite configs.

Alternative

No response

Additional context

No response

Validations

@andersk andersk added the enhancement New feature or request label May 4, 2023
@alex8088
Copy link
Owner

alex8088 commented May 5, 2023

@andersk
Copy link
Author

andersk commented May 5, 2023

Ah thanks, sorry for the noise!

@andersk andersk closed this as completed May 5, 2023
@andersk
Copy link
Author

andersk commented Jan 19, 2024

I’m getting around to trying this; unfortunately, providing multiple inputs to a single rollupOptions doesn’t actually work here. When multiple preloads have shared dependencies, Rollup puts those in shared chunks to be loaded via require(), which fails when Electron sandboxing is enabled (as required for security).

Since Rollup fundamentally lacks support for disabling chunks and duplicating shared dependencies into every output, we need a way for electron-vite to create multiple independent Rollup configs for preloads.

@andersk andersk reopened this Jan 19, 2024
@alex8088
Copy link
Owner

@andersk
check out: https://electron-vite.org/guide/dev#limitations-of-sandboxing
Because the require function is a polyfill with limited functionality, you will not be able to use CommonJS modules to separate your preload script into multiple files, unless sandbox: false is specified.

When different preloads import some shared methods or modules and the output format is CommonJS, chunks will definitely be generated. UMD format can be output to a single file but not apply to preload scripts. It is best to refactor the shared modules and import them separately.

@andersk
Copy link
Author

andersk commented Jan 23, 2024

Like I said, I understand that the Electron sandbox doesn’t support chunks, and that a single Rollup configuration with multiple inputs doesn’t support avoiding chunks. That is why my request is for electron-vite to use multiple Rollup configurations, each with a single input. That way there is nothing to share and extra chunks will not be generated. (Just like the way it already avoids generating chunks for code shared between the main and renderer processes, for example.)

sandbox: false is not a viable option due to its reduced security.

@alex8088
Copy link
Owner

@andersk
I understand your idea now, maybe letting preload support a set of configurations just like rollup can avoid this problem. But it is restricted by Vite. But I think it is still worth trying.

@alex8088
Copy link
Owner

Is there any better suggestion?

@andersk
Copy link
Author

andersk commented Jan 24, 2024

It would be better if Electron, Rollup, and Vite had better support for this, but they don’t, so the only option I see is for electron-vite to allow the creation of multiple Vite configurations.

My specific proposal is to allow the user to provide preload as either a single config (like today) or as an array of configs, which would then be processed separately from each other just like main, preload, and renderer are processed separately today.

// electron.vite.config.js
export default {
  
  preload: [
    {
      // first preload config
    },
    {
      // second preload config
    },
    {
      // third preload config
    },
  ],}

@Mesimpler
Copy link

i think is unnecessary. you can define a adapter function to solve this.
config.js is jsut a js object, you can split chunk and import, just ensure final result is legal.

@andersk
Copy link
Author

andersk commented Feb 1, 2024

@Mesimpler Your comment doesn’t make sense. The discussion is about chunking in the output of electron-vite, not chunking the config file itself.

@LukeHagar
Copy link
Contributor

Being able to provide multiple main configs would also solve the issue that led to me creating this PR #412

@alex8088
Copy link
Owner

Plan to support this feature

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

No branches or pull requests

4 participants