当前位置: 首页 > 文档资料 > Electron 中文文档 >

构建系统概览

优质
小牛编辑
135浏览
2023-12-01

Electron 使用 GN 生成项目,并用 Ninja 完成构建。 项目配置位于 .gn.gni 文件中。

Build System Overview

Electron uses GN for project generation and ninja for building. Project configurations can be found in the .gn and .gni files.

GN 文件

构建 Electron 的主要配置信息位于下列 gn 文件中:

  • BUILD.gn 指明了如何构建 Electron,还包括与 Chromium 链接的默认配置。
  • build/args/{debug,release,all}.gn 包含 Electron 的默认构建参数。

GN Files

The following gn files contain the main rules for building Electron:

  • BUILD.gn defines how Electron itself is built and includes the default configurations for linking with Chromium.
  • build/args/{debug,release,all}.gn contain the default build arguments for building Electron.

分块构建

由于 Chromium 项目及其庞大,最终的链接阶段往往需要数分钟,加大了开发难度。 为此,Chromium 采用了“分块构建”方式,将每个模块作为单独的动态库构建,虽然影响了文件大小和性能,但加快了链接速度,

Electron 也继承了 Chromium 这一构建方式。 在 Debug 模式下构建时,程序将与 Chromium 的动态库链接,以加快链接速度;而 Release 模式下程序则会与静态库链接,以优化程序大小和性能。

Component Build

Since Chromium is quite a large project, the final linking stage can take quite a few minutes, which makes it hard for development. In order to solve this, Chromium introduced the "component build", which builds each component as a separate shared library, making linking very quick but sacrificing file size and performance.

Electron inherits this build option from Chromium. In Debug builds, the binary will be linked to a shared library version of Chromium's components to achieve fast linking time; for Release builds, the binary will be linked to the static library versions, so we can have the best possible binary size and performance.

测试

注意:*此内容已经过时,部分信息对于使用 GN 构建的 Electron 已不再适用。

使用以下方式测试你的修改符合项目编码风格:

$ npm run lint

测试功能使用:

$ npm test

每当您更改Electron源代码时,都需要在测试之前重新运行构建:

$ npm run build && npm test

你可以通过分离特定的测试来让测试套件运行速度更快或阻止你目前正在使用摩卡的 独占性测试功能. 追加 .only 到任何 describeit 函数调用:

describe.only('some feature', function () {
  // ... only tests in this block will be run
})

或者,您可以使用 mocha 的 grep 选项来只运行与给定正则表达式模式匹配的测试

$ npm test -- --grep child_process

包含本地模块(例如runas)的测试无法在调试版本中执行 (查看 #2558 获取详情), 但它们将与发行版本一起使用。

要使用发行构建来运行测试:

$ npm test -- -R

Tests

NB this section is out of date and contains information that is no longer relevant to the GN-built electron.

Test your changes conform to the project coding style using:

$ npm run lint

Test functionality using:

$ npm test

Whenever you make changes to Electron source code, you'll need to re-run the build before the tests:

$ npm run build && npm test

You can make the test suite run faster by isolating the specific test or block you're currently working on using Mocha's exclusive tests feature. Append .only to any describe or it function call:

describe.only('some feature', function () {
  // ... only tests in this block will be run
})

Alternatively, you can use mocha's grep option to only run tests matching the given regular expression pattern:

$ npm test -- --grep child_process

Tests that include native modules (e.g. runas) can't be executed with the debug build (see #2558 for details), but they will work with the release build.

To run the tests with the release build use:

$ npm test -- -R