Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What is the purpose of buildResources folder in electron-builder building process?

I'm reading through electron and electron-builder docs, but I still do not quite understand what is the purpose of the buildResources folder?

Here's what a configuration doc for electron-builder says:

buildResources = build String - The path to build resources.

Kind of self-explanatory... But how or when they are involved in the build process, especially having that:

...build resources is not packed into the app. If you need to use some files, e.g. as tray icon, please include required files explicitly

Can we simply put those icon files in an arbitrary folder and then copy over into the app/ manually (since we need to include buildResources manually anyway)?

like image 317
jayarjo Avatar asked Mar 04 '19 08:03

jayarjo


People also ask

What is appId Electron?

In simple terms, appId is a name that the client's computer is using to identify. This is particularly useful and you must've noticed it when you are trying to launch an app thar's already open, your OS would open the minimised version, rather than opening a new instance of the same app.

What does Electron builder do?

electron-builder is a CLI tool that helps us create multi-platform distributions for Electron applications. It supports macOS, Windows, and Linux targets out of the box. It supports multiple file formats for each of these platforms such as dmg , pkg , mas and, mas-dev for macOS.

What is build Resources?

This is an extension you can use to copy all your resource files into your bin/classes directory (you can modify these path). This is useful if you want to use npm as your build tool, e.g. to build your Java applications.


2 Answers

TL;DR:

As far as I can tell from a quick glance at the source code, the buildResources folder is used to hold additional scripts, plugins, etc. that can be used by the package building software. Electron-builder doesn't generate the packages itself, it uses tools like NSIS.

Explanation:

I've had the same question and unfortunately find an answer for this isn't very straight-forward. The docs entry is pretty useless. I found out that someone asked about it in the GitHub issues but never got an answer.

I decided to dig in the code a bit myself to find out what it does. In NsisTargets.ts, you can see that the buildResources folder can contain custom includes and plugins for NSIS.

// NsisTargets.ts
taskManager.add(async () => {
      const userPluginDir = path.join(packager.info.buildResourcesDir, pluginArch)
      const stat = await statOrNull(userPluginDir)
      if (stat != null && stat.isDirectory()) {
        scriptGenerator.addPluginDir(pluginArch, userPluginDir)
      }
    })

// [...]

taskManager.add(async () => {
        const customInclude = await packager.getResource(this.options.include, "installer.nsh")
        if (customInclude != null) {
          scriptGenerator.addIncludeDir(packager.info.buildResourcesDir)
          scriptGenerator.include(customInclude)
        }
      })

and in pkg.ts it's used to load additional scripts to the pkg builder:

// pkg.ts
if (options.scripts != null) {
      args.push("--scripts", path.resolve(this.packager.info.buildResourcesDir, options.scripts))
    }

It appears as though buildResources can contain assets/scripts specifically used for the build process. That also explains why the contents of buildResources aren't included in the resulting app.asar file.

like image 93
Kerim Güney Avatar answered Jan 01 '23 21:01

Kerim Güney


So, I'm going to say straight away that the documentation for this option is just awful.

Files included in buildResources will appear in the asar file which you can find documentation about on electron's website.

The option files will include files such as pictures which are not accessible in the asar file.

I.E.

given I have a folder called assets in my build folder I want to include with my app.

"files": [
  "./build/**/*"
],
"directories": {
  "buildResources": "assets"
}

This will put all folders inside build into the asar file, which you can then unpack by including,

"asarUnpack": "**/assets/*"

This will put the folder assets into the build folder in the app directory.

like image 34
Daniel Hix Avatar answered Jan 01 '23 22:01

Daniel Hix