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

源码目录结构

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

Electron 的源代码主要依据 Chromium 的拆分约定被拆成了许多部分。

为了更好地理解源代码,您可能需要了解一下 Chromium 的多进程架构。

Source Code Directory Structure

The source code of Electron is separated into a few parts, mostly following Chromium on the separation conventions.

You may need to become familiar with Chromium's multi-process architecture to understand the source code better.

源代码的目录结构

Electron
├── atom/ - C++ 源代码.
|   ├── app/ - 系统入口代码.
|   ├── browser/ - 包含了主窗口、UI 和所有主进程相关的东西.
|   |   |          它会告诉渲染进程如何管理页面.
|   |   ├── ui/ - 不同平台上 UI 部分的实现.
|   |   |   ├── cocoa/ - Cocoa 部分的源代码.
|   |   |   ├── win/ - Windows GUI 部分的源代码.
|   |   |   └── x/ - X11 部分的源代码.
|   |   ├── api/ - 主进程 API 的实现.
|   |   ├── net/ - 网络相关的代码.
|   |   ├── mac/ - 与 Mac 有关的 Objective-C 代码.
|   |   └── resources/ - 图标,平台相关的文件等.
|   ├── renderer/ - 运行在渲染进程中的代码.
|   |   └── api/ - 渲染进程 API 的实现.
|   └── common/ - 同时被主进程和渲染进程用到的代码,
|       |         包括了一些用来将 node 的消息循环整合到 Chromium 的
|       |         消息循环中时用到的工具函数和代码.
|       └── api/ - 同时被主进程和渲染进程使用到的 API 的实现,
|                  并且是 Electron 内置模块的基础.
├── chromium_src/ - 从 Chromium 项目中拷贝来的代码. 参见下文。
├── default_app/ - 在没有提供应用程序的情况下
|                  启动 Electron 的默认页面.
├── docs/ - 文档.
├── lib/ - JavaScript 源代码.
|   ├── browser/ - Javascript 主进程初始化代码.
|   |   └── api/ - Javascript API 实现.
|   ├── common/ - 主进程和渲染器进程使用的 JavaScript.
|   |   └── api/ - Javascript API 实现.
|   └── renderer/ - Javascript 渲染器进程初始化代码.
|       └── api/ - Javascript API 实现.
├── native_mate/ - 复制了 Chromium 的 gin 库, 使得更加便捷
|                  地在 C++ 类型和 JavaScript 类型之间转换.
├── spec/ - 自动化测试.
└── BUILD.gn - Electron 的构建规则.

Structure of Source Code

Electron
├── atom/ - C++ source code.
|   ├── app/ - System entry code.
|   ├── browser/ - The frontend including the main window, UI, and all of the
|   |   |          main process things. This talks to the renderer to manage web
|   |   |          pages.
|   |   ├── ui/ - Implementation of UI stuff for different platforms.
|   |   |   ├── cocoa/ - Cocoa specific source code.
|   |   |   ├── win/ - Windows GUI specific source code.
|   |   |   └── x/ - X11 specific source code.
|   |   ├── api/ - The implementation of the main process APIs.
|   |   ├── net/ - Network related code.
|   |   ├── mac/ - Mac specific Objective-C source code.
|   |   └── resources/ - Icons, platform-dependent files, etc.
|   ├── renderer/ - Code that runs in renderer process.
|   |   └── api/ - The implementation of renderer process APIs.
|   └── common/ - Code that used by both the main and renderer processes,
|       |         including some utility functions and code to integrate node's
|       |         message loop into Chromium's message loop.
|       └── api/ - The implementation of common APIs, and foundations of
|                  Electron's built-in modules.
├── chromium_src/ - Source code copied from Chromium. See below.
├── default_app/ - The default page to show when Electron is started without
|                  providing an app.
├── docs/ - Documentations.
├── lib/ - JavaScript source code.
|   ├── browser/ - Javascript main process initialization code.
|   |   └── api/ - Javascript API implementation.
|   ├── common/ - JavaScript used by both the main and renderer processes
|   |   └── api/ - Javascript API implementation.
|   └── renderer/ - Javascript renderer process initialization code.
|       └── api/ - Javascript API implementation.
├── native_mate/ - A fork of Chromium's gin library that makes it easier to marshal
|                  types between C++ and JavaScript.
├── spec/ - Automatic tests.
└── BUILD.gn - Building rules of Electron.

/chromium_src

/chromium_src 中的文件更多地是 Chromium 的片段而不是内容层面的部分。 例如要实现 Papper API, 我们需要一些类似官方 Chrome 一样的联接操作。 我们可能已经构建了相应的源文件作为 libcc 的一部分,但是多数时候我们不需要所有的特性 (一些用于专用和分析的东西), 所以我们采用其部分代码。 这些可能很容易在 libcc 中已经存在补丁,但在编写目标 libcc 时维护的补丁非常小,而 chromium_src 的变化往往很大。 另外请注意,这些补丁不像我们维护的其他 libcc 补丁,绝对不能推到上游。

/chromium_src

The files in /chromium_src tend to be pieces of Chromium that aren't part of the content layer. For example to implement Pepper API, we need some wiring similar to what official Chrome does. We could have built the relevant sources as a part of libcc but most often we don't require all the features (some tend to be proprietary, analytics stuff) so we took parts of the code. These could have easily been patches in libcc, but at the time when these were written the goal of libcc was to maintain very minimal patches and chromium_src changes tend to be big ones. Also, note that these patches can never be upstreamed unlike other libcc patches we maintain now.

其它目录的结构

  • script - 用于诸如构建、打包、测试等开发用途的脚本等.
  • tools - 在 gyp 文件中用到的工具脚本,但与 script 目录不同,该目录中的脚本不应该被用户直接调用.
  • vendor - 第三方依赖项的源代码,为了防止人们将它与 Chromium 源码中的同名目录相混淆,在这里我们不使用 third_party 作为目录名.
  • node_modules - 在构建中用到的第三方 node 模块.
  • out - ninja 的临时输出目录.
  • dist - 由脚本 script/create-dist.py 创建的临时发布目录.
  • external_binaries - 下载了不支持用 gn 构建的第三方框架的二进制文件.

Structure of Other Directories

  • script - Scripts used for development purpose like building, packaging, testing, etc.
  • tools - Helper scripts used by GN files, unlike script, scripts put here should never be invoked by users directly.
  • vendor - Source code of third party dependencies, we didn't use third_party as name because it would confuse it with the same directory in Chromium's source code tree.
  • node_modules - Third party node modules used for building.
  • out - Temporary output directory of ninja.
  • dist - Temporary directory created by script/create-dist.py script when creating a distribution.
  • external_binaries - Downloaded binaries of third-party frameworks which do not support building with gn.

让 Git 子模块保持最新

Electron信息库有一些被提供的依赖, 在 /vendor 目录中可以找到. 运行 git status 时,偶尔会看到这样的消息:

$ git status
    modified:   vendor/depot_tools (new commits)
    modified:   vendor/boto (new commits)

要更新这些被提供的依赖关系,运行以下命令:

git submodule update --init --recursive

如果您发现自己经常运行此命令, 你可以在 ~/.gitconfig 文件中创建一个别名:

[alias]
    su = submodule update --init --recursive

Keeping Git Submodules Up to Date

The Electron repository has a few vendored dependencies, found in the /vendor directory. Occasionally you might see a message like this when running git status:

$ git status
	modified:   vendor/depot_tools (new commits)
	modified:   vendor/boto (new commits)

To update these vendored dependencies, run the following command:

git submodule update --init --recursive

If you find yourself running this command often, you can create an alias for it in your ~/.gitconfig file:

[alias]
	su = submodule update --init --recursive