Skip to main content

Apple Silicon 支持

· 7 min read

随着 Apple Silicon 硬件将于今年晚些时候发布,你如何让你的 Electron 应用在新硬件上运行?

¥With Apple Silicon hardware being released later this year, what does the path look like for you to get your Electron app running on the new hardware?


随着 Electron 11.0.0-beta.1 的发布,Electron 团队现在正在发布可在 Apple 计划于今年晚些时候发布的全新 Apple Silicon 硬件上运行的 Electron 版本。你可以获取 npm install electron@beta 的最新测试版,也可以直接从我们的 发布网站 下载。

¥With the release of Electron 11.0.0-beta.1, the Electron team is now shipping builds of Electron that run on the new Apple Silicon hardware that Apple plans on shipping later this year. You can grab the latest beta with npm install electron@beta or download it directly from our releases website.

它是如何工作的?

¥How does it work?

从 Electron 11 开始,我们将为 Intel Mac 和 Apple Silicon Mac 分别发布不同的 Electron 版本。在此更改之前,我们已经发布了两个工件,darwin-x64mas-x64,后者用于 Mac App Store 兼容性。我们现在正在发布另外两个工件 darwin-arm64mas-arm64,它们是上述工件的 Apple Silicon 版本。

¥As of Electron 11, we will be shipping separate versions of Electron for Intel Macs and Apple Silicon Macs. Prior to this change, we were already shipping two artifacts, darwin-x64 and mas-x64, with the latter being for Mac App Store compatibility usage. We are now shipping another two artifacts, darwin-arm64 and mas-arm64, which are the Apple Silicon equivalents of the aforementioned artifacts.

我需要做什么?

¥What do I need to do?

你需要发布两个版本的应用:一个用于 x64(Intel Mac),一个用于 arm64(Apple Silicon)。好消息是,electron-packagerelectron-rebuildelectron-forge 已经支持 arm64 架构。只要你运行的是这些软件包的最新版本,那么在将目标架构更新到 arm64 后,你的应用就应该能够完美运行。

¥You will need to ship two versions of your app: one for x64 (Intel Mac) and one for arm64 (Apple Silicon). The good news is that electron-packager, electron-rebuild and electron-forge already support targeting the arm64 architecture. As long as you're running the latest versions of those packages, your app should work flawlessly once you update the target architecture to arm64.

In the future, we will release a package that allows you to "merge" your arm64 and x64 apps into a single universal binary, but it's worth noting that this binary would be huge and probably isn't ideal for shipping to users.

更新:此软件包现已在 @electron/universal 上线。你可以使用它将两个打包的 x64 和 arm64 应用合并为一个二进制文件。

¥Update: This package is now available at @electron/universal. You can use it to merge two packaged x64 and arm64 apps into a single binary.

潜在问题

¥Potential Issues

原生模块

¥Native Modules

由于你的目标是新的架构,因此需要更新一些依赖,这可能会导致构建问题。以下列出了部分依赖的最低版本,供你参考。

¥As you are targeting a new architecture, you'll need to update several dependencies which may cause build issues. The minimum version of certain dependencies are included below for your reference.

依赖版本要求
Xcode>=12.2.0
node-gyp>=7.1.0
electron-rebuild>=1.12.0
electron-packager>=15.1.0

由于这些依赖版本要求,你可能需要修复/更新某些原生模块。值得注意的是,Xcode 升级将引入新版本的 macOS SDK,这可能会导致原生模块的构建失败。

¥As a result of these dependency version requirements, you may have to fix/update certain native modules. One thing of note is that the Xcode upgrade will introduce a new version of the macOS SDK, which may cause build failures for your native modules.

如何测试?

¥How do I test it?

目前,Apple Silicon 应用仅在 Apple Silicon 硬件上运行,而截至撰写本文时,该硬件尚未上市。如果你拥有 开发者过渡工具包,则可以在其上测试你的应用。否则,你必须等到 Apple Silicon 量产硬件发布后才能测试你的应用是否正常运行。

¥Currently, Apple Silicon applications only run on Apple Silicon hardware, which isn't commercially available at the time of writing this blog post. If you have a Developer Transition Kit, you can test your application on that. Otherwise, you'll have to wait for the release of production Apple Silicon hardware to test if your application works.

Rosetta 2 怎么办?

¥What about Rosetta 2?

Rosetta 2 是 Apple Rosetta 技术的最新版本,它允许你在其新的 arm64 Apple Silicon 硬件上运行 x64 Intel 应用。虽然我们相信 x64 Electron 应用可以在 Rosetta 2 下运行,但仍有一些重要事项需要注意(以及为什么你应该发布原生 arm64 二进制文件的原因)。

¥Rosetta 2 is Apple's latest iteration of their Rosetta technology, which allows you to run x64 Intel applications on their new arm64 Apple Silicon hardware. Although we believe that x64 Electron apps will run under Rosetta 2, there are some important things to note (and reasons why you should ship a native arm64 binary).

  • 你的应用性能将显著下降。Electron / V8 使用 JIT 编译 JavaScript,由于 Rosetta 的工作方式,你将有效地运行两次 JIT(一次在 V8 中,一次在 Rosetta 中)。

    ¥Your app's performance will be significantly degraded. Electron / V8 uses JIT compilation for JavaScript, and due to how Rosetta works, you will effectively be running JIT twice (once in V8 and once in Rosetta).

  • 你失去了 Apple Silicon 新技术带来的好处,例如更大的内存页面大小。

    ¥You lose the benefit of new technology in Apple Silicon, such as the increased memory page size.

  • 我们是否提到过性能会显著下降?

    ¥Did we mention that the performance will be significantly degraded?