Vite ignoring resolve.mainFields in configuration
Problem
Using the default config, vite doesn't seem to respect the resolve.mainField.
We are using ws in our electron setup, and when using vite-plugin-electron vite kept importing the browser module from ws, corrupting the build.
Seeing the correct approach in default config, ignoring the browser as a mainField. Unfortunately vite kept using the browser field.
Turns out resolve.mainField is not respected when targeting browser (which is vite default).
Solution
Enforce main entry build to be ssr: true.
I'm ready to create PR if issue is accepted.
Hey! 👋
However, you can override the default configuration by configuring electron({}) in vite.cinfig.ts.
Definitely, and how we have done it, so change not required to get it working.
But debugging of multiple hours to figure out why the (already existing) clause of handling browser field didn't take effect, could have been avoided, and should for new-comers IMO 💡
It would have saved me a lot of frustrations at least 🙏🏻
Are you talking about import the ws module in the Renderer process? If the Main process import the ws module, it will not look for the browser module.
The default config only acting on Main process and Preload scripts.