@electron-forge/plugin-vite can no longer build main module as ESM (breaking config change)
Pre-flight checklist
- [X] I have read the contribution documentation for this project.
- [X] I agree to follow the code of conduct that this project uses.
- [X] I have searched the issue tracker for a bug that matches the one I want to file, without success.
Electron Forge version
0.7.5
Electron version
32.1.1
Operating system
Windows 11
Last known working Electron Forge version
0.7.4
Expected behavior
I started with a template from 0.7.4 and I configured vite to output ESM like so (~~I don't remember whether it was the default back then or not~~, it wasn't):
const config: UserConfig = {
build: {
lib:
forgeConfigSelf?.entry != null
? {
// eslint-disable-next-line @typescript-eslint/no-unsafe-assignment
entry: forgeConfigSelf.entry,
fileName: () => "[name].mjs",
formats: ["es"],
}
: false,
rollupOptions: {
external,
},
},
[…]
I expect ESM only output and a working build.
Actual behavior
vite outputs both ESM and CJS to the same file whereas they sometimes contain artifacts of each other due to differing file lengths.
The build console output of electron-forge start points in this direction, too:
✓ 1324 modules transformed.
rendering chunks (1)...
computing gzip size (0)...
computing gzip size (1)...
.vite/build/main.mjs 2,771.45 kB │ gzip: 697.16 kB │ map: 5,514.09 kB
rendering chunks (2)...
computing gzip size (2)...
.vite/build/main.mjs 2,771.42 kB │ gzip: 697.13 kB │ map: 5,513.99 kB
However, final proof can be obtained by
- fileName: () => "[name].mjs",
+ fileName: (format) => `[name].${format}.js`,
and indeed I my build directory contains:
-a--- 2920546 main.cjs.js
-a--- 5529438 main.cjs.js.map
-a--- 2920578 main.es.js
-a--- 5529531 main.es.js.map
Steps to reproduce
See expected behaviour.
Additional information
I use a few vite ESM-only APIs (like import.meta.glob()) and therefore cannot switch to CJS.
Users upgrading from any past version of Forge to 7.5.0 should now not experience breaking config changes.
I noted that you dropped compatibility guarantees, but I thought that this would qualify as a breaking config change. I highly suspect that other vite array-options can't be overwritten as well.
Hi @ioV9x! Thanks for the report. From my initial read, I think this is a valid bug and we should take a look.
After many hours, I have managed to get ESM compilation working based on electron-vite-samples found in https://github.com/electron/forge/pull/3572. Im not entirely sure what did it, however I am using version 7.5.0. https://gist.github.com/Denyol/ff9f1800f1c0d953a2eb69f47c143fd5
@Denyol please double check your logs. In my case the ESM and CJS builds race and I suspect that you just were lucky that the ESM build won the race.
@Denyol please double check your logs. In my case the ESM and CJS builds race and I suspect that you just were lucky that the ESM build won the race.
That is strange, I was experiencing some builds failing due to compiling as CJS, trying again and it works seemingly randomly. Please point me in the direction of where I could find build logs.
We did not make any changes to the build format.
7.4.0
https://github.com/electron/forge/blob/v7.4.0/packages/template/vite/tmpl/vite.main.config.mjs#L17-L21
7.5.0
https://github.com/electron/forge/blob/v7.5.0/packages/plugin/vite/src/config/vite.main.config.ts#L10-L14
@caoxiemeihao as I stated in the opening post: I started with a template from 0.7.4 and I reconfigured vite to output ESM instead of CJS. The issue is not that a default or the template has changed, but that I can no longer override the default.
Btw. GitHub shows a code preview for referenced code if (and only if) you reference it via the commit hash (this is what the copy perma-link context menu action does), i.e. you would need to change the link like so:
-https://github.com/electron/forge/blob/v7.4.0/packages/template/vite/tmpl/vite.main.config.mjs#L17-L21
+https://github.com/electron/forge/blob/d3d47b2bc53be13609b5ff4d858275ce74bff819/packages/template/vite/tmpl/vite.main.config.mjs#L17-L21
https://github.com/electron/forge/blob/7574b364901ec429220dbe7076184585a99529a5/packages/plugin/vite/src/Config.ts#L3-L17
@caoxiemeihao Whether to make the entry optional, all the configuration of the vite build part is placed in vite.config.ts
Any updates?