在电子构建器构建过程中 buildResources 文件夹的目的是什么?

jay*_*rjo 8 electron electron-builder

我正在阅读文档electronelectron-builder文档,但我仍然不太明白该buildResources文件夹的用途是什么?

这是电子制造商的配置文档所说的:

buildResources= build String - 构建资源的路径。

有点不言自明......但是他们如何或何时参与构建过程,尤其是:

...构建资源未打包到应用程序中。如果您需要使用某些文件,例如作为托盘图标,请明确包含所需文件

我们可以简单地将这些图标文件放在任意文件夹中,然后app/手动复制到其中(因为buildResources无论如何我们都需要手动包含)?

Ker*_*ney 9

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)
        }
      })
Run Code Online (Sandbox Code Playgroud)

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))
    }
Run Code Online (Sandbox Code Playgroud)

看起来好像buildResources可以包含专门用于构建过程的资产/脚本。这也解释了为什么buildResources结果app.asar文件中不包含的内容。


小智 5

所以,我要立即说这个选项的文档太糟糕了。

包含的文件buildResources将出现在 asar 文件中,您可以在 Electron 网站上找到有关该文件的文档。

该选项files将包括在 asar 文件中无法访问的图片等文件。

IE

鉴于我的构建文件夹中有一个名为 asset 的文件夹,我想将其包含在我的应用程序中。

"files": [
  "./build/**/*"
],
"directories": {
  "buildResources": "assets"
}
Run Code Online (Sandbox Code Playgroud)

这会将 build 内的所有文件夹放入 asar 文件中,然后您可以通过包含以下内容来解压该文件:

"asarUnpack": "**/assets/*"
Run Code Online (Sandbox Code Playgroud)

这会将文件夹 asset 放入应用程序目录中的 build 文件夹中。

  • 但文档说“buildResources”中的任何内容都不会自动包含到 asar 文件中?顺便说一句,这也是我在这里观察到的。 (2认同)