Build Lifecycle构建生命周期
How Forge takes your app code from development to distribution.Forge如何将您的应用程序代码从开发到分发。
Once your app is ready to be released, Electron Forge can handle the rest to make sure it gets into your users' hands. 一旦你的应用程序准备好发布,Electron Forge就可以处理剩下的部分,以确保它进入你的用户手中。The complete build flow for Electron Forge can be broken down into three smaller steps:Electron Forge的完整构建流程可以分解为三个较小的步骤:

Each one of these steps is a separate command exposed through Forge's 这些步骤中的每一个都是一个单独的命令,通过Forge的electron-forge
command line interface, and is usually mapped to a script in your package.json file.electron-forge
命令行接口公开,并且通常映射到您的package.json文件中的脚本。
Cascading build steps级联构建步骤
Running each of these tasks will also run the previous ones in the sequence (i.e. running the 运行这些任务中的每一个也将运行序列中的前一个任务(即,运行electron-forge publish
script will first run package
and make
as prerequisite steps).electron-forge publish
脚本将首先运行package
和make
作为先决条件步骤)。
Step 1: Package第1步:打包
In the Package step, Forge uses Electron Packager to package your app. This means creating an executable bundle for a target operating system (e.g. 在打包步骤中,Forge使用Electron Packager来打包您的应用程序。这意味着为目标操作系统创建一个可执行捆绑包(例如macOS上的.app
on macOS or .exe
on Windows)..app
或Windows上的.exe
)。
This step also performs a few supporting tasks:此步骤还执行一些支持任务:
Handles code signing and notarization on macOS.在macOS上处理代码签名和公证。Rebuilds native node add-ons for your app's Electron version.为应用程序的Electron版本重新构建本机节点加载项。Handles Custom App Icons on Windows and macOS.处理Windows和macOS上的自定义应用程序图标。
By default, running the Package step will only create a packaged application for your machine's platform and architecture.默认情况下,运行Package步骤只会为机器的平台和体系结构创建一个打包的应用程序。
On bundling app code关于捆绑应用程序代码
Note that Forge does not perform any bundling of your app code for production in the Package step without additional configuration.请注意,在没有额外配置的情况下,Forge不会在Package步骤中为生产执行任何应用程序代码绑定。
If you need to perform any custom JavaScript build tasks (e.g. module bundling with Parcel or webpack) for either renderer or main process code, see the Using lifecycle hooks section below.如果您需要为渲染器或主流程代码执行任何自定义JavaScript构建任务(例如,与Parcel或webpack绑定模块),请参阅下面的使用生命周期挂钩部分。
After the Package step, your packaged application will be available in the 完成打包步骤后,打包后的应用程序将在/out/
directory./out/
目录中可用。
Step 2: Make第2步:制作
Forge's Make step takes the bundled executable output from the previous Package step and creates "distributables" from it. Forge的Make步骤从上一个Package步骤获得捆绑的可执行文件输出,并从中创建“可分发文件”。Distributables refer to any output format that you want to distribute to users, whether it be an OS-specific installer (e.g. 可分发文件是指您想要分发给用户的任何输出格式,无论是特定于操作系统的安装程序(例如.dmg
or .msi
) or a simple compressed archive (e.g. .zip
) of the bundle..dmg
或.msi
)还是捆绑包的简单压缩存档(例如.zip
)。
You can choose which distributables you want to build by adding Makers to your Forge config.您可以通过在Forge配置中添加Makers来选择要构建的可分发文件。
By default, running the Make step will only run Makers targeting your machine's platform and architecture.默认情况下,运行Make步骤将只运行针对机器平台和体系结构的Maker。
After the Make step, distributable archives or installers are generated for your packaged app in the 在Make步骤之后,将在项目的/out/make/
folder of your project./out/make/
文件夹中为打包的应用程序生成可分发的存档或安装程序。
Step 3: Publish第3步:发布
Forge's Publish step takes the distributable build artifacts from the Make step and uploads for distribution to your app's end users (e.g. to GitHub Releases or AWS S3 static storage). Forge的发布步骤从Make步骤中获取可分发的构建工件,并上传到应用程序的最终用户(例如GitHub Release或AWS S3静态存储)。Publishing is an optional step in the Electron Forge pipeline, since the artifacts from the Make step are already in their final format.发布是Electron Forge管道中的一个可选步骤,因为Make步骤中的工件已经是最终格式。
You can choose which platforms you want to target by adding Publishers to your Forge config.您可以通过在Forge配置中添加Publishers来选择要针对的平台。
After the Publish step, your app distributables will be available to download by users.在“发布”步骤之后,用户将可以下载您的应用程序可分发文件。
Using lifecycle hooks使用生命周期挂钩
Your Electron application might have custom build needs that aren't handled with the most basic Forge pipeline described above. 您的Electron应用程序可能有自定义构建需求,而这些需求无法通过上述最基本的Forge管道来处理。To solve this issue, Electron Forge exposes callback hooks at various points in the build process.为了解决这个问题,Electron Forge在构建过程中的各个点公开了回调挂钩。
These hooks can be used to implement custom logic that your application needs. 这些挂钩可用于实现应用程序所需的自定义逻辑。For instance, you can perform actions between the Package and Make steps with the 例如,您可以使用premake
hook.premake
挂钩在Package和Make步骤之间执行操作。
For a full list of Forge hooks and usage examples, see the Hooks documentation.有关Forge钩子和使用示例的完整列表,请参阅挂钩文档。
If you want to share a specific sequence of build hook logic, you can modularize your hook code into a plugin instead. 如果您想共享构建钩子逻辑的特定序列,您可以将钩子代码模块化为插件。This is how Forge's Webpack Plugin works, for instance. 例如,Forge的Webpack插件就是这样工作的。For more details on authoring custom plugins, see the Writing Plugins guide.有关编写自定义插件的更多详细信息,请参阅编写插件指南。
Cross-platform build systems跨平台构建系统
By default, Electron Forge will only build your app for the operating system it's running on. 默认情况下,Electron Forge只会为其运行的操作系统构建应用程序。Targeting a different operating system (e.g. building a Windows app from macOS) has many caveats.针对不同的操作系统(例如,从macOS构建Windows应用程序)有很多注意事项。
If you don't have access to Windows, macOS, and Linux machines, we highly recommend creating a build pipeline on a Continuous Integration platform that supports all these platforms (e.g. CircleCI or GitHub Actions). 如果您无法访问Windows、macOS和Linux机器,我们强烈建议您在支持所有这些平台的持续集成平台上创建构建管道(例如CircleCI或GitHub Actions)。For an example of CI builds in action, see Electron Fiddle's GitHub Actions pipeline.有关CI构建的示例,请参阅Electron Fiddle的GitHub Actions管道。