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

@electron-forge/plugin-vite can no longer build main module as ESM (breaking config change) #3715

Open
3 tasks done
ioV9x opened this issue Sep 23, 2024 · 6 comments · May be fixed by #3721
Open
3 tasks done

@electron-forge/plugin-vite can no longer build main module as ESM (breaking config change) #3715

ioV9x opened this issue Sep 23, 2024 · 6 comments · May be fixed by #3721

Comments

@ioV9x
Copy link

ioV9x commented Sep 23, 2024

Pre-flight checklist

  • I have read the contribution documentation for this project.
  • I agree to follow the code of conduct that this project uses.
  • 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.

@erickzhao
Copy link
Member

Hi @ioV9x! Thanks for the report. From my initial read, I think this is a valid bug and we should take a look.

@Denyol
Copy link

Denyol commented Sep 24, 2024

After many hours, I have managed to get ESM compilation working based on electron-vite-samples found in #3572. Im not entirely sure what did it, however I am using version 7.5.0. https://gist.github.com/Denyol/ff9f1800f1c0d953a2eb69f47c143fd5

@ioV9x
Copy link
Author

ioV9x commented Sep 24, 2024

@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
Copy link

Denyol commented Sep 25, 2024

@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.

@caoxiemeihao
Copy link
Member

caoxiemeihao commented Sep 26, 2024

@ioV9x
Copy link
Author

ioV9x commented Sep 27, 2024

@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

@caoxiemeihao caoxiemeihao linked a pull request Sep 29, 2024 that will close this issue
5 tasks
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging a pull request may close this issue.

4 participants