Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Closed
rafaberaldo opened this issue Apr 26, 2024 · 2 comments
Closed

Comments

@rafaberaldo
Copy link
Contributor

rafaberaldo commented Apr 26, 2024

  • 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: #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
Copy link
Contributor Author

rafaberaldo commented Apr 30, 2024

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
Copy link
Contributor Author

rafaberaldo commented May 8, 2024

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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant