electron/forge

Forge make combined with vite is creating an incomplete asar

steveoh opened this issue · 8 comments

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

Electron version

33.0.1

Operating system

windows 11

Last known working Electron Forge version

7.4.0

Expected behavior

npm run make creates an asar file that includes the node_modules and the app can run on windows successfully.

Actual behavior

node_modules aren't present and install fails with ERR_MODULE_NOT_FOUND

Steps to reproduce

  1. npm run make
  2. go to the out folder and find open the nupkg/resources/app.asar file
  3. run npx @electron/asar extract app.asar app
  4. notice there is no node_modules

Additional information

This is only happening on windows as I believe that is the only place that asar is used.
We are using vite instead of webpack.

Image

The main.js file from within the asar in the error message imports the following

import "node:path";
import "node:url";
import "electron";
import "electron-window-state";
import "update-electron-app";
import "./main-DWMHPuxT.js";
import "electron-squirrel-startup";
import "fs";
import "path";
import "url";
import "stream";
import "zlib";

Where electron-window-state and update-electron-app and I assume electron-squirrel-startup are not available to be imported.

I'm not sure about 7.5.0 but in earlier versions it was needed to move electron-window-state and any other dependency that isn't getting bundled correctly to devDependencies in package.json, This is a known mismatch between vite and webpack in forge.

This works as expected downgrading to 7.4.0. I'll give this a try but no bundles are getting added to the app.asar file at 7.5.0.

Well now I'm getting a require is not defined error after making the project. :/

@steveoh I use electron-log and it works fine after downgrading.

I've had this happen to ubuntu as well, but found a way to get the trick:
When you run "npm run make" for the first time, open the @electron-forge/plugin-vite configuration, get the .vite/ folder, and delete the out/ folder; Then turn off the @electron-forge/plugin-vite config and run "npm run make" again to get a complete installer

Ran into a similar issue with other packages. Found a work-around and modified it to work for me. Seems @electron/packager related.

// forge.config.js

const { spawn } = require('node:child_process');

module.exports = {
	hooks: {
		packageAfterPrune: async (config, build_path) => {
			const vite_config = await import('./vite.preload.config.mjs');
			const external = vite_config?.default?.build?.rollupOptions?.external || [];
			const commands = [
				'install',
				'--no-package-lock',
				'--no-save',
				...external,
			];

			return new Promise((resolve, reject) => {
				npm = spawn('npm', commands, {
					cwd: build_path,
					stdio: 'inherit',
					shell: true,
				});

				npm.on('close', (code) => {
					if (0 === code) {
						resolve();
						return;
					}

					reject(`Process exited with code: ${code}`);
				});

				npm.on('error', reject);
			});
		},
	},
	// ...
};