electron-builder icon indicating copy to clipboard operation
electron-builder copied to clipboard

Modifying files before package/build using distributed node version

Open rafaberaldo opened this issue 2 months ago • 1 comments

  • Electron-Builder Version: 24.13.3
  • Node Version: 20.11.1
  • Electron Version: 29.3.0
  • Electron Type (current, beta, nightly): stable/current
  • Target: win32

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:

  • set asar: true and asarUnpack: '**/*' this is for electron-builder don't throw error saying the main.js doesn't exist.
  • then the afterPack hook:
import asar from '@electron/asar';
import { FuseV1Options, FuseVersion, flipFuses } from '@electron/fuses';
import fs from 'node:fs';
import path from 'node:path';

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 avatar Apr 26 '24 16:04 rafaberaldo

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 avatar Apr 30 '24 15:04 rafaberaldo

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.

rafaberaldo avatar May 08 '24 12:05 rafaberaldo