创建新的 Electron 浏览器模块

    这不是创建Electron Browser API的全面指南,而是记录一些更晦涩的步骤的大纲。

    Electron使用 作为元构建系统为其编译器 Ninja 生成文件。 这意味着,为了让Electron编译您的代码,我们必须将您的API的代码和头文件名添加到 。

    您需要按字母顺序将 API 文件名追加到相应的文件中,如下所示:

    filenames.gni

    请注意,Windows、 macOS 和 Linux 数组添加是可选的,只有当您的 API 有具体的平台实现时才应被添加。

    类型定义由 Electron 使用 和 @electron/typescript-definitions生成。 这个步骤对于确保Electron的 API 文档的一致性是必要的。 这意味着,要使 API 类型定义显示在 electron.d.ts 文件中,我们必须创建一个 .md 文件。 示例可以在中找到。

    Electron使用 构建其模块。

    下面是您可能需要添加的代码的基本示例,以便将 object_template_builderwrappable 合并到 API 中。 如需进一步参考,您可以在此处找到更多实现。

    api_name.h 文件中:

    api_name.h

    1. #ifndef ELECTRON_SHELL_BROWSER_API_ELECTRON_API_{API_NAME}_H_
    2. #define ELECTRON_SHELL_BROWSER_API_ELECTRON_API_{API_NAME}_H_
    3. #include "gin/wrappable.h"
    4. namespace electron {
    5. namespace api {
    6. class ApiName : public gin::Wrappable<ApiName> {
    7. public:
    8. static gin::Handle<ApiName> Create(v8::Isolate* isolate);
    9. // gin::Wrappable
    10. static gin::WrapperInfo kWrapperInfo;
    11. v8::Isolate* isolate) override;
    12. const char* GetTypeName() override;
    13. } // namespace api
    14. } // namespace electron

    api_name.cc 文件中:

    api_name.cc

    In the typings/internal-ambient.d.ts file, we need to append a new property onto the Process interface like so:

    typings/internal-ambient.d.ts

    1. interface Process {
    2. _linkedBinding(name: 'electron_browser_{api_name}', Electron.ApiName);
    3. }

    api_name.cc

    In your file, add your node binding name to Electron’s built-in modules.

    shell/common/node_bindings.cc

    1. V(electron_browser_{api_name})

    我们需要在以下路径中创建一个新的 TypeScript 文件:

    此文件的内容示例可在这里找到。

    向 TypeScript 公开你的模块

    lib/browser/api/module-list.ts