跨平台打包
掌握使用 `electron-builder` 进行稳健跨平台分发的高级打包选项与配置
跨平台打包 是 CoddyKit 上的免费 Electron Desktop App Development 课时。 这是第 1 节课,共 4 节。 你可以在下方免费阅读本课时的完整内容 — 然后在浏览器中使用内置代码编辑器和全天候 AI 导师进行实践。 这是 Electron Desktop App Development 学习路径的一部分,你的进度在网页和 CoddyKit 应用中同步。 Electron Desktop App Development 课程共包含 4 节课。
本课时的部分内容尚未翻译,以英文显示。
What is Cross-Platform Packaging?
When you build an Electron application, you're creating a desktop app using web technologies. To distribute this app to users, you need to package it into an executable or installer specific to their operating system.
Cross-platform packaging means creating these distribution files for Windows, macOS, and Linux from a single codebase. This ensures your app can reach a wide audience regardless of their chosen OS.
Introducing electron-builder
electron-builder is the go-to solution for packaging and distributing Electron applications. It's a powerful and flexible tool that automates much of the complex process.
- Generates installers: Creates `.exe`, `.dmg`, `.deb`, `.AppImage`, and more.
- Code signing: Handles signing for security and trust.
- Auto-updates: Integrates with auto-update mechanisms.
- Flexible configuration: Highly customizable through your
package.jsonfile or external configuration.
Basic electron-builder Setup
To get started, you'll install electron-builder as a development dependency and add a "build" script to your package.json. This script typically points to the electron-builder command.
Here's a basic setup:
{
"name": "my-electron-app",
"version": "1.0.0",
"main": "main.js",
"scripts": {
"start": "electron .",
"build": "electron-builder"
},
"devDependencies": {
"electron": "^28.0.0",
"electron-builder": "^24.0.0"
},
"build": {
"appId": "com.yourapp.id"
}
}Understanding "appId"
The appId (Application ID) is a crucial identifier for your application. It's a unique string, often in reverse domain name format (e.g., com.yourcompany.yourappname).
- Uniqueness: Helps operating systems identify your app.
- Updates: Essential for distinguishing your app during updates.
- Data storage: Used by OS for application-specific data.
Always use a unique appId for your application.
Windows-Specific Configurations
For Windows, electron-builder can generate different installer types. The most common are NSIS (Nullsoft Scriptable Install System) and Squirrel.Windows.
- NSIS: Creates traditional `.exe` installers, offering more customization.
- Squirrel.Windows: Provides easy auto-updates and a simpler installation experience.
You can define Windows-specific settings under the "win" key in your "build" configuration.
Windows Config Example
Here's how you might configure `electron-builder` for Windows, specifying targets and an icon:
{
"name": "my-electron-app",
"build": {
"appId": "com.yourapp.id",
"win": {
"target": [
"nsis",
"squirrel"
],
"icon": "build/icon.ico"
}
}
}macOS-Specific Configurations
On macOS, the primary distribution formats are DMG (Disk Image) and PKG (Installer Package). `electron-builder` handles the specifics for these.
- DMG: The most common way to distribute macOS apps, allowing drag-and-drop installation.
- PKG: A traditional installer package, suitable for more complex installations or enterprise distribution.
Crucial macOS settings include category and bundleId (which should match your appId).
macOS Config Example
This snippet shows macOS-specific settings, including targets, bundle ID, and application category:
{
"name": "my-electron-app",
"build": {
"appId": "com.yourapp.id",
"mac": {
"target": [
"dmg",
"pkg"
],
"category": "public.app-category.developer-tools",
"bundleId": "com.yourapp.id",
"icon": "build/icon.icns"
}
}
}Linux-Specific Configurations
Linux has a variety of package formats. electron-builder supports popular ones like AppImage, deb, and rpm.
- AppImage: A universal format that runs on most Linux distributions without installation.
- deb: For Debian-based systems (Ubuntu, Debian).
- rpm: For Red Hat-based systems (Fedora, CentOS).
Similar to macOS, you'll often define a category for your application.
Linux Config Example
Here's an example of configuring for Linux, targeting different formats and setting an icon:
{
"name": "my-electron-app",
"build": {
"appId": "com.yourapp.id",
"linux": {
"target": [
"AppImage",
"deb",
"rpm"
],
"category": "Development",
"icon": "build/icon.png"
}
}
}Check Your Packaging Knowledge
Match the `electron-builder` target configuration key with its primary operating system.
Recap & Next Steps
Great job! You've learned the fundamentals of cross-platform packaging with electron-builder.
- We explored how
electron-buildersimplifies creating installers for Windows, macOS, and Linux. - You saw how to configure platform-specific settings directly in your
package.json. - Understanding
appIdand various installer targets (NSIS, DMG, AppImage) is key to successful distribution.
Next, you'll learn about code signing and notarization, essential steps for ensuring your application is trusted by users and operating systems.
常见问题解答
「跨平台打包」课时是免费的吗?
是的 — 「跨平台打包」的完整文本可在网页上免费阅读。要进行交互式练习(内置代码编辑器和全天候 AI 导师)并解锁 Electron Desktop App Development 课程的其余内容,请升级到 CoddyKit PRO。 Electron Desktop App Development 课程共包含 4 节课。
「跨平台打包」这节课中我会学到什么?
掌握使用 `electron-builder` 进行稳健跨平台分发的高级打包选项与配置 你通过在浏览器中直接运行的动手代码来练习 Electron Desktop App Development,全天候 AI 导师会在你学习这节课的过程中回答你的问题。
学习 Electron Desktop App Development 需要有经验吗?
无需任何先前经验。CoddyKit 上的 Electron Desktop App Development 课程适合初学者到高级学习者,你可以从这里开始或从头开始,按照自己的节奏学习。 这是第 1 节课,共 4 节。
「跨平台打包」课时需要多长时间?
大多数 CoddyKit 课程大约需要 5–10 分钟。每节课都很精短且互动,所以你能稳步进步,并在网页和应用中从离开的地方继续。
我能在这节 Electron Desktop App Development 课中编写并运行代码吗?
能。每节 Electron Desktop App Development 课都包含内置代码编辑器,你可以在浏览器中直接编写并运行真实代码,并获得即时 AI 反馈 — 无需本地设置。