Skip to content

Undocumented breaking change for Vite plugin in v7.3.0 #3506

Closed

Description

@erickzhao

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

Activity

  1. pinned this issue on Feb 23, 2024
  2. phlegmaticprogrammer commented on Mar 3, 2024

    @phlegmaticprogrammer
  3. OmegaRogue commented on Mar 3, 2024

    @OmegaRogue
  4. will-stone commented on Mar 20, 2024

    @will-stone

    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

  5. sparecycles commented on Mar 25, 2024

    @sparecycles

    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"

  6. Ben-Avrahami commented on Mar 28, 2024

    @Ben-Avrahami
  7. sparecycles commented on Mar 28, 2024

    @sparecycles

    The only insight I didn't mention is that the main PR description ( #3468 ) admits

    ... this PR change is very significant. Moving most of the logic form [sic] plugin/vite to template/vite ...

    So, I think the expectation here is we recreate our build configs on the new base templates?

  8. boldt commented on Apr 16, 2024

    @boldt

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

  9. Ben-Avrahami commented on Apr 17, 2024

    @Ben-Avrahami

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

    +1 !

  10. added a commit that references this issue on Apr 17, 2024
  11. caoxiemeihao commented on Apr 20, 2024

    @caoxiemeihao
    Member

    Migrate to 7.3.0+

    Updated on 2024-04-25 ([email protected])


    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({}));
  12. 23 remaining items

  13. added a commit that references this issue on May 3, 2024
  14. linked a pull request that will close this issuefeat(plugin-vite): better logic #3583on Jul 1, 2024
  15. boldt commented on Aug 21, 2024

    @boldt

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

  16. unpinned this issue on Sep 20, 2024
  17. adidahiya commented on Oct 7, 2024

    @adidahiya

    Thanks for fixing this in #3583! I can finally upgrade electron-forge in my project 🙏🏽

  18. GitMurf commented on Jan 17, 2025

    @GitMurf

    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: #3336 (comment)). 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!

  19. sparecycles commented on Jan 18, 2025

    @sparecycles

    @GitMurf Okay, you sent me down the rabbit hole. The best I've found is that some of the forge VitePlugin methods are no longer actually run and copying dependencies was "removed" according to this comment.

    #3632 (comment)

    This turns out to be the cause of an instability issue I was having trouble chasing down, so I'm equally invested.

  20. GitMurf commented on Jan 18, 2025

    @GitMurf

    The best I've found is that some of the forge VitePlugin methods are no longer actually run and copying dependencies was "removed"

    Thank you for confirming I am not crazy :) hopefully the team will be able to point us to a workaround and we can work towards a long term solution.

  21. sparecycles commented on Jan 18, 2025

    @sparecycles

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

Type

No type

Projects

No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions