@builder.io/gatsby

中文文档
4.0.0发布于 2024/01/12MIT近 30 天下载 6526

使用 Gatsby 的 Builder.io 插件

中文使用文档
文档目录5 节
  1. 安装
  2. 如何使用
  3. 使用编辑器中的组件
  4. 如何查询
  5. 学多点

@ builder.io/ gatsby

集成插件 构建者.io 允许与 Gatsby 一起拖放页面。 它将构建者.io 计划置于一个 allBuilderModels gatsby GraphQL 查询的字段中,如果模板地图作为选项被传递,此插件将在指定的路径上为每个页面条目动态创建 Gatsby 页面.

example

安装

npm install @builder.io/gatsby

如何使用

开个免费账户 构建者.io 从您的公共 API 密钥中获取 组织页面 和:

// In your gatsby-config.js
const path = require('path');
module.exports = {
  plugins: [
    {
      resolve: '@builder.io/gatsby',
      options: {
        // public API Key
        publicAPIKey: 'MY_PUBLIC_API_KEY',
        // OPTIONAL
        // Set this to `true` to rely on our cached content. Default value is `false`, always fetching the newest content from Builder.
        useCache: false,
        // OPTIONAL
        // mapping model names to template files, the plugin will create a page for each entry of the model at its specified url
        templates: {
          // `page` can be any model of choice, camelCased
          page: path.resolve('templates/my-page.tsx'),
        },
        // OPTIONAL
        mapEntryToContext: async ({ entry, graphql }) => {
          const result = await graphql('....');
          return {
            property: entry.data.property,
            anotherProperty: entry.data.whatever,
            dataFromQuery: result.data
            /* ... */
          };
        },
        // OPTIONAL
        // to resolve a single entry to multiple, for e.g in localization
        resolveDynamicEntries: async (entries) => {
          const entriesToBuild = []
          for entry of entries {
            if (entry.data.myprop.isDynamic){
               entriesToBuild.push(await myEntryResolver(entry))
            }
            else {
               entriesToBuild.push(entry)
            }
          }
          return entriesToBuild;
        },
      },
    },
  ],
};

然后在Builder.io开始建立网页,点击发布,它们将会被录入你的Gatsby网站,在每个新的建筑上!

一个更先进的例子和开始检查 gatsby- starter- 建设器

使用编辑器中的组件

看这个 设计系统实例 使用您的 deisgn 系统+自定义组件的许多示例

👉提示: 要将页面构建限制在您的组件吗 ? 尝试 只有组件模式

注册一个组件

import { Builder } from '@builder.io/react';

class SimpleText extends React.Component {
  render() {
    return <h1>{this.props.text}</h1>;
  }
}

Builder.registerComponent(SimpleText, {
  name: 'Simple Text',
  inputs: [{ name: 'text', type: 'string' }],
});

如何查询

最新完整的例子请检查 Gatsby + 构建器.io 启动器

{
  allBuilderModels {
    myPageModel(options: { cachebust: true }) {
      content
    }
  }
}

学多点

版本与发布记录

@builder.io/gatsby 共发布 65 个版本,首次发布于 2020/02/26;过去 12 个月发布了 0 个版本。

版本发布日期距今
4.0.0latest2024/01/122 年前
3.0.32022/12/143 年前
3.0.22022/09/104 年前
3.0.2-1dev2022/09/084 年前
3.0.2-02022/09/084 年前
3.0.12022/03/014 年前

关于 @builder.io/gatsby 的常见问题

@builder.io/gatsby 是什么?

使用 Gatsby 的 Builder.io 插件

如何安装 @builder.io/gatsby?

在项目目录运行 npm install @builder.io/gatsby,或使用 pnpm add / yarn add / bun add。它还需要宿主项目提供 peer 依赖:gatsby(>= 4)。npm 7 及以上与 pnpm 8 及以上默认会自动安装必需的 peer 依赖,Yarn 需要手动安装。

@builder.io/gatsby 支持 TypeScript 吗?

该包的 package.json 没有声明类型入口。需要类型提示时,可以在 npm 上查找社区维护的 @types/builder.io__gatsby 是否存在。

@builder.io/gatsby 需要什么版本的 Node.js?

package.json 的 engines 字段要求 Node.js >=14.15.0。版本不符时 npm 默认只给出警告,开启 engine-strict 后会拒绝安装。

@builder.io/gatsby 可以商用吗?

@builder.io/gatsby 使用 MIT 许可。宽松许可:允许商业使用、修改和再分发,通常只需在分发时保留原版权与许可声明。以上为概括,不构成法律意见,请以许可原文为准。

@builder.io/gatsby 的下载量有多少?

根据 npm 官方下载统计,@builder.io/gatsby 在 2026/10/09 – 2026/11/07 这 30 天内被下载 6,526 次,日均约 218 次。下载量会把 CI 构建、镜像同步和作为其他包依赖的安装一并计入,适合横向比较热度,不能直接等同于使用人数。

@builder.io/gatsby 还在维护吗?

最新版本 4.0.0 发布于 2024/01/12(2 年前);至今共发布 65 个版本,其中过去 12 个月发布了 0 个;首次发布于 2020/02/26。发布频率只是参考信号:功能稳定的工具可能很少需要更新,建议结合源码仓库的 issue 和提交记录判断。