electron-userland / electron-builder

A complete solution to package and build a ready for distribution Electron app with “auto update” support out of the box
https://www.electron.build
MIT License
13.61k stars 1.74k forks source link

Modifying files before package/build using distributed node version #8188

Closed rafaberaldo closed 4 months ago

rafaberaldo commented 5 months ago

Related: https://github.com/electron-userland/electron-builder/issues/5619

I need to modify some files before packing using the distributed node/electron, the problem is that I'm building it on a x64 machine and the distributable is x86 (meaning I can't just use node_modules/electron).

beforePack hook doesn't work because distributable electron hasn't been downloaded/unziped yet, and afterPack already has the asar archive.

I did find a solution using the @electron/asar package:

async function afterPack (context) { const fileName = context.packager.appInfo.productFilename; const binPath = path.join(context.appOutDir, ${fileName}.exe);

// Modifying some files
await compileFile({
  filename: path.join(context.appOutDir, 'resources/app.asar.unpacked/dist/main.js'),
  node: binPath,
});

// Packing to asar
await asar.createPackage(
  path.join(context.appOutDir, 'resources/app.asar.unpacked'),
  path.join(context.appOutDir, 'resources/app.asar'),
);

// Remove leftover files
fs.rmSync(path.join(context.appOutDir, 'resources/app.asar.unpacked'), {
  recursive: true,
  force: true,
});

// Flip fuses
await flipFuses(binPath, {
  version: FuseVersion.V1,
  [FuseV1Options.RunAsNode]: false,
  [FuseV1Options.OnlyLoadAppFromAsar]: true,
});

},



It appears to have worked correctly, and the build size is smaller, but I would like to know if there's a better solution or any downsides to it, does it break the NSIS auto updater?

Thanks!
rafaberaldo commented 5 months ago

After playing a bit with @electron/packager, I could use their afterCopy hook to accomplish that.

Another option I can think of, would be using @electron/get to download the distributed version and modify the files before electron-builder. Doesn't feel right though since electron-builder already does it.

rafaberaldo commented 4 months ago

Closing this as the PR has been merged.

I didn't test but using @electron/asar probably breaks autoupdater from electron-builder.

For those who's gonna look at this in the future, what I'm gonna do is use the afterExtract hook to get the distributed node/electron and modify the files before they get packed.