electron / forge

:electron: A complete tool for building and publishing Electron applications
https://electronforge.io
MIT License
6.42k stars 507 forks source link

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

Open ioV9x opened 3 days ago

ioV9x commented 3 days ago

Pre-flight checklist

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):

  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 commented 3 days ago

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

Denyol commented 2 days ago

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

ioV9x commented 2 days ago

@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 commented 2 days ago

@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 commented 22 hours ago

We did not make any changes to the build format.

7.4.0

image

https://github.com/electron/forge/blob/v7.4.0/packages/template/vite/tmpl/vite.main.config.mjs#L17-L21

7.5.0

image

https://github.com/electron/forge/blob/v7.5.0/packages/plugin/vite/src/config/vite.main.config.ts#L10-L14