Hooks挂钩

Specify custom build logic with asynchronous callback functions使用异步回调函数指定自定义生成逻辑

In Electron Forge, hooks are asynchronous callback functions that allow you to insert your own logic at different points in the development or build process.在Electron Forge中,钩子是异步回调函数,允许您在开发或构建过程的不同点插入自己的逻辑。

Each hook function comes with the Forge configuration object as a first parameter.每个钩子函数都带有Forge配置对象作为第一个参数。

To read more about the different stages in Forge's build process, please refer to the Build Lifecycle documentation.要了解更多关于Forge构建过程中不同阶段的信息,请参阅构建生命周期文档。

Simple hooks简单挂钩

In Electron Forge, most hooks are simple hooks, which perform side effects during the build lifecycle without directly affecting subsequent steps in the build.在Electron Forge中,大多数钩子都是简单的钩子,它们在构建生命周期中执行副作用,而不会直接影响构建中的后续步骤。

generateAssets

generateAssets() is invoked before Forge's start or package commands.在Forge的startpackage命令之前调用。

You can use this hook to generate any static files or resources your app requires on runtime but aren't in the source code.您可以使用此挂钩生成应用程序在运行时所需但不在源代码中的任何静态文件或资源。

For instance, you could use this hook to generate a license file containing the license of all your dependencies.例如,您可以使用这个钩子生成一个包含所有依赖项的许可证的许可证文件。

postStart

postStart() called after Forge's start command launches the app in dev mode.在Forge的start命令以dev模式启动应用程序后调用postStart()

You can use this hook to attach listeners to the spawned child process.您可以使用这个钩子将侦听器附加到派生的子进程。

module.exports = {
  hooks: {
    postStart: async (forgeConfig, appProcess) => {
      console.log(`Spawned child pid: ${appProcess.pid}`);
    }
  }
};

prePackage

prePackage() is called before Forge runs Electron Packager in the package step .在Forge在package步骤中运行Electron Packager之前调用prePackage()

packageAfterCopy

packageAfterCopy() is called inside the afterCopy hook of Electron Packager.packageAfterCopy()在Electron Packager的afterCopy钩子内部调用。

During Forge's package step, Electron Packager copies your app's build directory to a temporary folder.在Forge的package步骤中,Electron Packager会将应用程序的构建目录复制到一个临时文件夹中。

The afterCopy hook runs after this copy step.afterCopy钩子在该复制步骤之后运行。

packageAfterPrune

packageAfterPrune() is called inside the afterPrune hook of Electron Packager.packageAfterPrune()在Electron Packager的afterPrune钩子内部调用。

During Forge's package step, Electron Packager prunes non-production node_modules dependencies from the temporary folder your app is copied to. This step minimizes the size of your app's production bundle.在Forge的package步骤中,Electron Packager从应用程序复制到的临时文件夹中删除非生产node_modules依赖项。这一步骤最大限度地减少了应用程序生产捆绑包的大小。

The afterPrune hook runs after this prune step.afterPrune钩子在这个修剪步骤之后运行。

packageAfterPrune() will have no effect if your packagerOptions.prune option is set to false.如果您的packagerOptions.prune选项设置为false,那么packageAfterPrune()将无效。

packageAfterExtract

packageAfterExtract() is called inside the afterExtract hook of Electron Packager.packageAfterExtract()在Electron Packager的afterExtract钩子内部调用。

During Forge's package step, Electron Packager extracts your Electron binary into a temporary folder.在Forge的package步骤中,Electron Packager将您的Electron二进制文件提取到一个临时文件夹中。

The afterExtract hook runs after this extract step.afterExtract钩子在该提取步骤之后运行。

postPackage

postPackage() is called after Forge's package step has successfully completed.postPackage()是在Forge的package步骤成功完成后调用的。

For example:例如:

module.exports = {
  hooks: {
    postPackage: async (forgeConfig, options) => {
      console.info('Packages built at:', options.outputPaths);
    }
  }
};

preMake

preMake() is called before the make step runs.make步骤运行之前调用preMake()

Mutating hooks突变钩子

In Electron Forge, mutating hooks are a special kind of hook that return the same type of value as their second parameter.在Electron Forge中,变异钩子是一种特殊的钩子,它返回与第二个参数相同类型的值。

The returned value will replace the original parameter's value for subsequent steps in the Forge lifecycle.返回的值将替换Forge生命周期中后续步骤的原始参数值。

postMake

postMake()is called after Forge's make step has successfully completed.postMake()是在Forge的make步骤成功完成后调用的。

It is passed an array of MakeResult objects that are output from the make step. 它被传递一个MakeResult对象数组,这些对象是从make步骤输出的。If you wish to mutate the array of Make results, you can return a new array of MakeResult objects that Electron Forge can use for future steps.如果你想改变Make结果的数组,你可以返回一个新的MakeResult对象数组,Electron Forge可以在未来的步骤中使用。

readPackageJson

readPackageJson() is called every time Forge attempts to read your package.json file.每当Forge尝试读取您的package.json文件时,就会调用readPackageJson()

The full package.json object is passed in as a parameter. If you want to modify that object in any way, you must do so and return the new value for Forge to use.完整的package.json对象作为参数传入。如果您想以任何方式修改该对象,您必须这样做,并返回新值供Forge使用。

This is useful to set things like the package.json version field at runtime.这对于在运行时设置package.jsonversion字段等内容非常有用。

module.exports = {
  hooks: {
    readPackageJson: async (forgeConfig, packageJson) => {
      packageJson.version = '4.0.0';
      return packageJson;
    }
  }
};

Note: this hook will not change the name or version used by Electron Packager to customize your app metadata, as that is read prior to this hook being called (during Electron Packager's afterCopy hooks).此挂钩不会更改Electron Packager用于自定义应用程序元数据的名称或版本,因为在调用此挂钩之前(在Electron Package的afterCopy挂钩期间)会读取该名称或版本。