Packaging Your Application打包应用程序
This is part 5 of the Electron tutorial.这是Electron教程的第5部分。
Learning goals学习目标
In this part of the tutorial, we'll be going over the basics of packaging and distributing your app with Electron Forge.在本教程的这一部分,我们将介绍使用Electron Forge打包和分发应用程序的基础知识。
Using Electron Forge使用Electron锻造
Electron does not have any tooling for packaging and distribution bundled into its core modules. Electron没有任何打包和分发工具捆绑到其核心模块中。Once you have a working Electron app in dev mode, you need to use additional tooling to create a packaged app you can distribute to your users (also known as a distributable). 一旦您在开发模式下有了一个工作的Electron应用程序,您需要使用额外的工具来创建一个打包的应用程序,可以分发给您的用户(也称为可分发应用程序)。Distributables can be either installers (e.g. MSI on Windows) or portable executable files (e.g. 可分发文件可以是安装程序(如Windows上的MSI)或可移植可执行文件(如macOS上的.app
on macOS)..app
)。
Electron Forge is an all-in-one tool that handles the packaging and distribution of Electron apps. Electron Forge是一个处理Electron应用程序打包和分发的一体化工具。Under the hood, it combines a lot of existing Electron tools (e.g. electron-packager, @electron/osx-sign, electron-winstaller, etc.) into a single interface so you do not have to worry about wiring them all together.在引擎盖下,它将许多现有的Electron工具(如electron-packager、@electron/osx-sign、electron-winstaller等)组合到一个界面中,因此您不必担心将它们连接在一起。
Importing your project into Forge将项目导入Forge
You can install Electron Forge's CLI in your project's 您可以在项目的devDependencies
and import your existing project with a handy conversion script.devDependencies
中安装Electron Forge的CLI,并使用方便的转换脚本导入现有项目。
- npm
- Yarn
npm install --save-dev @electron-forge/cli
npx electron-forge import
yarn add --dev @electron-forge/cli
npx electron-forge import
Once the conversion script is done, Forge should have added a few scripts to your 转换脚本完成后,Forge应该已经在package.json
file.package.json
文件中添加了一些脚本。
//...
"scripts": {
"start": "electron-forge start",
"package": "electron-forge package",
"make": "electron-forge make"
},
//...
For more information on 有关make
and other Forge APIs, check out the Electron Forge CLI documentation.make
和其他Forge API的更多信息,请参阅Electron Forge CLI文档。
You should also notice that your package.json now has a few more packages installed under your 您还应该注意到,您的devDependencies
, and contains an added config.forge
field with an array of makers configured. package.json
现在在devDependencies
下安装了更多的软件包,并且包含了一个添加的config.forge
字段,其中配置了一组制造商。Makers are Forge plugins that create distributables from your source code. 是从源代码创建可分发的Forge插件。You should see multiple makers in the pre-populated configuration, one for each target platform.您应该看到预填充配置中的多个制造商,每个目标平台一个制造商。
Creating a distributable创建可分发
To create a distributable, use your project's new 要创建可分发的,请使用项目的新make
script, which runs the electron-forge make
command.make
脚本,该脚本运行electron-forge make
命令。
- npm
- Yarn
npm run make
yarn run make
This 这个make
command contains two steps:make
命令包含两个步骤:
It will first run它将首先在后台运行electron-forge package
under the hood, which bundles your app code together with the Electron binary.electron-forge package
,该包将应用程序代码与Electron二进制文件捆绑在一起。The packaged code is generated into a folder.打包的代码生成到一个文件夹中。It will then use this packaged app folder to create a separate distributable for each configured maker.然后,它将使用这个打包的应用程序文件夹为每个配置的制造商创建一个单独的可分发应用程序。
After the script runs, you should see an 脚本运行后,您应该看到一个out
folder containing both the distributable and a folder containing the packaged application code.out
文件夹,其中包含可分发文件和包含打包应用程序代码的文件夹。
out/
├── out/make/zip/darwin/x64/my-electron-app-darwin-x64-1.0.0.zip
├── ...
└── out/my-electron-app-darwin-x64/my-electron-app.app/Contents/MacOS/my-electron-app
The distributable in the out/make
folder should be ready to launch! out/make
文件夹中的可分发文件应准备好启动!You have now created your first bundled Electron application.现在,您已经创建了第一个捆绑的Electron应用程序。
If you want to manually package your code, or if you're just interested understanding the mechanics behind packaging an Electron app, check out the full Application Packaging documentation.如果您想手动打包代码,或者您只是想了解打包Electron应用程序背后的机制,请查看完整的应用程序打包文档。
Important: signing your code重要:给代码加签名
In order to distribute desktop applications to end users, we highly recommended for you to code sign your Electron app. 为了将桌面应用程序分发给最终用户,我们强烈建议您对Electron应用程序进行代码签名。Code signing is an important part of shipping desktop applications, and is mandatory for the auto-update step in the final part of the tutorial.代码签名是交付桌面应用程序的一个重要部分,在本教程最后部分的自动更新步骤中是必需的。
Code signing is a security technology that you use to certify that a desktop app was created by a known source. 代码签名是一种安全技术,用于证明桌面应用程序是由已知源创建的。Windows and macOS have their own OS-specific code signing systems that will make it difficult for users to download or launch unsigned applications.Windows和macOS有自己的特定于操作系统的代码签名系统,这将使用户难以下载或启动未签名的应用程序。
If you already have code signing certificates for Windows and macOS, you can set your credentials in your Forge configuration. 如果您已经拥有Windows和macOS的代码签名证书,则可以在Forge配置中设置凭据。Otherwise, please refer to the full Code Signing documentation to learn how to purchase a certificate and for more information on the desktop app code signing process.否则,请参阅完整的代码签名文档,了解如何购买证书,以及有关桌面应用程序代码签名过程的更多信息。
On macOS, code signing is done at the app packaging level. 在macOS上,代码签名是在应用程序打包级别完成的。On Windows, distributable installers are signed instead.在Windows上,可分发的安装程序被签名。
- macOS
- Windows
{
//...
"config": {
"forge": {
//...
"packagerConfig": {
"osxSign": {
"identity": "Developer ID Application: Felix Rieseberg (LT94ZKYDCJ)",
"hardened-runtime": true,
"entitlements": "entitlements.plist",
"entitlements-inherit": "entitlements.plist",
"signature-flags": "library"
},
"osxNotarize": {
"appleId": "felix@felix.fun",
"appleIdPassword": "this-is-a-secret"
}
}
//...
}
}
//...
}
{
//...
"config": {
"forge": {
//...
"makers": [
{
"name": "@electron-forge/maker-squirrel",
"config": {
"certificateFile": "./cert.pfx",
"certificatePassword": "this-is-a-secret"
}
}
]
//...
}
}
//...
}
Summary小结
Electron applications need to be packaged to be distributed to users. Electron应用程序需要打包才能分发给用户。In this tutorial, you imported your app into Electron Forge and configured it to package your app and generate installers.在本教程中,您将应用程序导入Electron Forge,并将其配置为打包应用程序并生成安装程序。
In order for your application to be trusted by the user's system, you need to digitally certify that the distributable is authentic and untampered by code signing it. 为了使您的应用程序受到用户系统的信任,您需要数字地证明可分发的是真实的,并且不受代码签名的限制。Your app can be signed through Forge once you configure it to use your code signing certificate information.一旦您将应用程序配置为使用代码签名证书信息,就可以通过Forge对其进行签名。