forge: Undocumented breaking change for Vite plugin in v7.3.0

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

7.3.0

Electron version

N/A

Operating system

All

Last known working Electron Forge version

7.2.0

Expected behavior

Upgrading your Vite plugin app from Electron Forge v7.2.0 to v7.3.0 is seamless.

Actual behavior

Changes in the build system during the upgrade to Vite 5 have produced an API-incompatible change, requiring users to upgrade.

Steps to reproduce

N/A

Additional information

To fix this issue, see the @electron-forge/template-vite template config files: https://github.com/electron/forge/tree/main/packages/template/vite/tmpl

About this issue

  • Original URL
  • State: closed
  • Created 3 years ago
  • Reactions: 16
  • Comments: 37 (5 by maintainers)

Commits related to this issue

Most upvoted comments

Personally, the way this “migration” is being handled makes me want to run for the hills. I don’t think it is acceptable that an upgrade from 7.2 to 7.3 breaks my code, without any warning. The reason I am using something like forge in the first place is because I don’t want to look below the abstraction that covers all the messy details.

It’s half a year later now. Meanwhile, electron-forge 7.4.0 was released and this problem still exists. Any update on how to migrate from electron-forge 7.2.0 to electron-forge 7.4.0 (vite+ts)?

Chiming in here… I’ve been using configs based on modified 7.2.0 templates (vite + electron + svelte! 😉 ).

Upgrading to 7.3.0 causes my existing configs to … just not produce any output! (.vite/build/… empty: no main.js found).

I think I might be able to get things to work if I rebuild the changes in my templates on top of the new ones, but that seems involved for a minor release.

Note: I don’t think it’s the vite@5 update that’s causing my issue, as my package.json already specified "vite": "^5.2.6"

@GitMurf That is my understanding 👍 As @VerteDinde mentioned above we’re looking into options here, the issue is the breaking change is bidirectional. i.e. straight up reverting won’t help because then new apps will be broken in the opposite way to old apps. Right now you should pin to a version of forge that works for your app, and we’ll figure out how to make a way forward to higher forge versions.

@caoxiemeihao I am fairly well versed in forge and vite but there is so much conversation here (and in other gh issues) about what needs to be patched where and what forge version and running migration scripts etc etc that I am confused and a bit lost.

Is it possible that you publish a minimal working template repo that has everything we need with forge 7.4 to work with forge-vite-typescript and ESM (type: module)? Basically what the vite typescript template should do (but it sounds like there are reasons why the template can’t work or something?).

I just think we need some “official” docs / direction that will also be maintained / supported by forge long term otherwise it will turn into a maintenance nightmare. Thanks!

I’m a little concerned that this migration guide is implying the de-facto official path uses electron-forge-plugin-vite, which is maintained in a repo under @caoxiemeihao’s personal control.

Ideally any migration utility should be contributed and maintained under electron/forge

Can you provide a migration guide, to fix this issue?

Hey folks, sorry for our delay - we spoke about this in a maintainer meeting today. We’re deciding on a path forward, but wanted to briefly update the issue letting you know that there are currently eyes on this and we should be reporting back with fixes shortly.

Can you provide a migration guide, to fix this issue?

+1 !

@GitMurf Alright, I have a fix, but I hate it.

Note: I stripped out my app “dependencies” to just the externals, then I added afterCopy to my forge.config.ts packagerConfig. I also removed the rebuildConfig: {} because was rebuilding my project node_modules in situ. (not a problem until the next npm install breaks the electron app because it’s depending on the project’s node modules for the native dependency).

Strategy is to strip the copied package.json of devDependencies (and scripts, because we don’t need that developer data in the application anyway) and run npm install and electron/rebuild directly in the buildPath.

Here’s what I ended up with.

    afterCopy: [
      async (buildPath, electronVersion, platform, arch, callback) => {
        try {
          const appPackageJsonPath = join(buildPath, "package.json");

          const {
            scripts,
            devDependencies,
            engines,
            overrides,
            ...packageJson
          } = JSON.parse(readFileSync(appPackageJsonPath, "utf-8"));

          writeFileSync(
            appPackageJsonPath,
            JSON.stringify(packageJson, null, 2),
          );

          const appPackageLockPath = join(buildPath, "package-lock.json");
          cpSync("./package-lock.json", appPackageLockPath);
          execSync("npm install", { cwd: buildPath });
          rmSync(appPackageLockPath);

          await rebuild({
            buildPath,
            electronVersion,
            platform: platform as NodeJS.Platform,
            arch,
            force: true,
          });

          callback(null);
        } catch (error) {
          callback(error);
        }
      },
    ],

At least I’m popping the project’s package-lock.json there for some level of build sanity/consistency. I’m not sure if this is a heavy-weight solution (execSync) or not, (in the context of whatever else is running in forge).

You might consider also adding …

    asar: {
      unpack: "**/{.**,**}/*.node",
    },

… which is actually less code than installing AutoUnpackNatives (where I stole it from). My app seems to work either way (without asar, with asar, with asar+unpack).

In hindsight, it seems this is a general packager/forge issue and not specifically related to the vite-plugin, but in the ecosystem of all these pieces it’s hard to decide exactly which one of these is dropping the ball 🙃.

Good luck!

cc @erickzhao @caoxiemeihao @MarshallOfSound

I hate to post in an old gh issue, but the relevant conversation and starting point for a lot of the issues is described nicely in this discussion above. And I apologize for the @ pinging but feel it is necessary to understand where things sit in the new year as I am unclear and have tried to look through as many gh issues, PRs and docs as I can and have not been able to find an answer.

Last year (during the time of the discussions above) after months of effort I got something working with v7.4 (had to do some git patches… see here: https://github.com/electron/forge/pull/3336#issuecomment-2076349319). And honestly after getting it working I did not want to touch anything again so I stopped following along. But we recently needed to update to Electron v34 and with that needed to update forge. So I updated to v7.6 and now am back to having my issues with native modules again. This time it is the fact that zero node_modules are being bundled with the asar even though I mark them as external in vite.main.config as the forge docs currently state (see screenshot below).

Image

So my question is, what is the expectation for forge v7.6 and the vite plugin? Are external modules supposed to be packaged? Or do we have to handle this completely ourselves via a forge hook or something?

If there is a thread/discussion/post somewhere that already addresses this please point me there (and I apologize for not finding it) but I have spent several hours searching through old and new gh issues and PRs, the docs etc. and cannot figure out what the clear expectation is on how the vite plugin should be functioning for things like external deps (native node modules)?

Thanks!

Personally, the way this “migration” is being handled makes me want to run for the hills. I don’t think it is acceptable that an upgrade from 7.2 to 7.3 breaks my code, without any warning. The reason I am using something like forge in the first place is because I don’t want to look below the abstraction that covers all the messy details.

You only see that there is an increase in template code for v7.3.0, and you have overlooked what the approach of v7.3.0 brings and what incompatible aspects of Forge itself have been avoided.

  1. The release speed of Vite is very fast, and Forge will not release a new version immediately due to a version update of Vite.
  2. The modern development experience of hot-restart and hot-reload is very popular in electron-vite and tauri, but implementing this feature in Forge without authorization can damage Forge’s own design. It’s simply impossible to advance.
  3. It is not possible to directly use {"type": "module"} in the package. json of @electron-forge/plugin-vite, which is fatal for frameworks like svelte, as Forge itself is built in the format of CommonsJS(tsconfig. json) and cannot risk switching to ESNext entirely due to the vite-template.
  4. Vite itself was not designed for Electron and does not even support Node.js construction. Forge exposes the configuration, which is beneficial for users to update the Vite version in a timely manner without waiting for Forge’s version update, and it can also more conveniently fix some unknown BUG.

BTW, if you feel that there is too much @electron-forge/template-vite code, it is recommended that you use electron-vite(even without a separate config file, really simplify) or other frameworks.

@GitMurf Okay, I did the thing.

forge-7.3.0-rewrite-esm-support:./vite.base.config.ts should be a shim that helps people migrate.

Copy-paste that file into the project, and then try taking whatever your existing configs are and wrap them with the appropriate extendMainConfig(), extendPreloadConfig() or extendRendererConfig() wrappers. (these accept all 4 possible forms of configs, objects, functions, promises, and async functions, and will mix in what the vite.base.config and templates currently have).

the only non-trivial (non extendMainConfig({}) and extendPreloadConfig({})) in this example is

export default extendRendererConfig({
  plugins: [svelte({})],
});

because svelte!

I’ve only tested this with package.json type=module because svelte requires it. If anyone can do the legwork to validate this solution is stable in a commonjs/other-framework package, that would be great.

Thanks for the nudge, I feel like I can finally update my actual project now!

@Ben-Avrahami @boldt this may be easier to apply for you.

EDIT: oof, it appears that I didn’t test preload. It seems to be imported by require in electron and therefore needs to be a .cjs file when package.json type=module.

Migrate to 7.3.0+

Updated on 2024-04-25 (electron-forge-plugin-vite@0.6.1)


vite.main.config.ts

  import { defineConfig } from 'vite';
+ import { forgeViteConfig } from 'electron-forge-plugin-vite/migration';

  // https://vitejs.dev/config
- export default defineConfig({
+ export default defineConfig(forgeViteConfig.main({
    resolve: {
      // Some libs that can run in both Web and Node.js, such as `axios`, we need to tell Vite to build them in Node.js.
      browserField: false,
      conditions: ['node'],
      mainFields: ['module', 'jsnext:main', 'jsnext'],
    },
- });
+ }));

vite.renderer.config.ts

  import { defineConfig } from 'vite';
+ import { forgeViteConfig } from 'electron-forge-plugin-vite/migration';

  // https://vitejs.dev/config
- export default defineConfig({});
+ export default defineConfig(forgeViteConfig.renderer({}));

vite.preload.config.ts

  import { defineConfig } from 'vite';
+ import { forgeViteConfig } from 'electron-forge-plugin-vite/migration';

  // https://vitejs.dev/config
- export default defineConfig({});
+ export default defineConfig(forgeViteConfig.preload({}));

Do you mind pointing to the change that fixed the issue? There seems to be a lot going on there. Here’s my files: https://github.com/will-stone/browserosaurus