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
Arguments:参数:config:
ResolvedForgeConfig
-Forge configuration objectForge配置对象platform: string
-Operating system platform操作系统平台arch: string
-CPU architectureCPU架构
Returns:返回:Promise<void>
generateAssets()
is invoked before Forge's 在Forge的start
or package
commands.start
或
命令之前调用。package
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
Arguments:参数:config:
ResolvedForgeConfig
-Forge configuration objectForge配置对象appProcess:
ChildProcess
-Node.js child process instanceNode.js子进程实例
Returns:返回:Promise<void>
在Forge的postStart()
called after Forge's start
command launches the app in dev mode.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
Arguments:参数:config:
ResolvedForgeConfig
-Forge configuration objectForge配置对象platform: string
-Operating system platform操作系统平台arch: string
-CPU architectureCPU架构
Returns:返回:Promise<void>
在Forge在prePackage()
is called before Forge runs Electron Packager in the package
step .package
步骤中运行Electron Packager之前调用prePackage()
。
packageAfterCopy
Arguments:参数:config:
ResolvedForgeConfig
-Forge configuration objectForge配置对象buildPath: string
-the app's temporary folder path应用程序的临时文件夹路径electronVersion: string
-the app's Electron version应用程序的Electron版本platform: string
-Operating system platform操作系统平台arch: string
-CPU architectureCPU架构
Returns:返回:Promise<void>
packageAfterCopy()
is called inside the afterCopy
hook of Electron Packager.packageAfterCopy()
在Electron Packager的afterCopy
钩子内部调用。
During Forge's 在Forge的package
step, Electron Packager copies your app's build directory to a temporary folder.package
步骤中,Electron Packager会将应用程序的构建目录复制到一个临时文件夹中。
The afterCopy
hook runs after this copy step.afterCopy
钩子在该复制步骤之后运行。
packageAfterPrune
Arguments:参数:config:
ResolvedForgeConfig
-Forge configuration objectForge配置对象buildPath: string
-the app's temporary folder path应用程序的临时文件夹路径electronVersion: string
-the app's Electron version应用程序的Electron版本platform: string
-Operating system platform操作系统平台arch: string
-CPU architectureCPU架构
Returns:返回:Promise<void>
packageAfterPrune()
is called inside the afterPrune
hook of Electron Packager.packageAfterPrune()
在Electron Packager的afterPrune
钩子内部调用。
During Forge's 在Forge的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.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
Arguments:参数:config:
ResolvedForgeConfig
-Forge configuration objectForge配置对象buildPath: string
-the Electron binary's temporary folder pathElectron二进制文件的临时文件夹路径electronVersion: string
-the app's Electron version应用程序的Electron版本platform: string
-Operating system platform操作系统平台arch: string
-CPU architectureCPU架构
Returns:返回:Promise<void>
packageAfterExtract()
is called inside the afterExtract
hook of Electron Packager.packageAfterExtract()
在Electron Packager的afterExtract
钩子内部调用。
During Forge's 在Forge的package
step, Electron Packager extracts your Electron binary into a temporary folder.package
步骤中,Electron Packager将您的Electron二进制文件提取到一个临时文件夹中。
The afterExtract
hook runs after this extract step.afterExtract
钩子在该提取步骤之后运行。
postPackage
Arguments:参数:config:
ResolvedForgeConfig
-Forge configuration objectForge配置对象packageResult: Object
platform: string
-Operating system platform操作系统平台arch: string
-CPU architectureCPU架构outputPaths: string[]
-filesystem paths for package output包输出的文件系统路径
Returns:返回:Promise<void>
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
Arguments:参数:config:
ResolvedForgeConfig
-Forge configuration objectForge配置对象
Returns:返回:Promise<void>
在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
Arguments:参数:config:
ResolvedForgeConfig
-Forge configuration objectForge配置对象makeResults:
MakeResult
[]
Returns:返回:Promise<
MakeResult
[] | void>
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 如果你想改变Make结果的数组,你可以返回一个新的MakeResult
objects that Electron Forge can use for future steps.MakeResult
对象数组,Electron Forge可以在未来的步骤中使用。
readPackageJson
Arguments:参数:config:
ResolvedForgeConfig
-Forge configuration objectForge配置对象packageJson: Record<string, unknown>
-Full package.json object完整的package.json对象
Returns:返回:Promise<Record<string, unknown> | void>
每当Forge尝试读取您的readPackageJson()
is called every time Forge attempts to read your package.json
file.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 这对于在运行时设置package.jsonversion
field at runtime.version
字段等内容非常有用。
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 此挂钩不会更改Electron Packager用于自定义应用程序元数据的名称或版本,因为在调用此挂钩之前(在Electron Package的afterCopy
hooks).afterCopy
挂钩期间)会读取该名称或版本。