@faustwp/blocks

中文文档
7.1.1发布于 2026/11/04MITTypeScriptESM + CJS零依赖构建来源已验证近 30 天下载 4730

Faust Blocks

原始使用文档
文档目录3 节
  1. Quick Start
  2. A Simple Block Example
  3. Further Learning

@faustwp/blocks

@faustwp/blocks provides conventional connector components for rendering WordPress blocks.

Getting Started with Gutenberg Blocks Provider and Viewer

Quick Start

Make sure you have completed the initial setup for Faust at Getting Started.

Install the blocks package:

npm i @faustwp/blocks

Install the peer dependencies:

npm i @wordpress/style-engine

Open _app.js and import the WordPressBlocksProvider:

import { WordPressBlocksProvider } from '@faustwp/blocks';

<FaustProvider pageProps={pageProps}>
  <WordPressBlocksProvider
    config={{
      blocks,
    }}>
    <Component {...pageProps} key={router.asPath} />
  </WordPressBlocksProvider>
</FaustProvider>

Then, inside your templates you need to pass on the editorBlocks data in your WordPressBlocksViewer.

The helper function flatListToHierarchical is imported from @faustwp/core:

import { flatListToHierarchical } from '@faustwp/core';
import { WordPressBlocksViewer } from '@faustwp/blocks';
import blocks from '../wp-blocks';

const { editorBlocks } = props.data.post;
const blocks = flatListToHierarchical(editorBlocks, {childrenKey: 'innerBlocks'});

return <WordPressBlocksViewer blocks={blocks}/>

By default the API brings all the nodes back in one array instead of a bunch of separate nodes with their own arrays. Therefore we use the flatListToHierarchical to convert the list back to the hierarchical tree type.

Example editorBlocks GraphQL query fragment:

${components.CoreParagraph.fragments.entry}
editorBlocks {
  __typename
  name
  renderedHtml
  id: clientId
  parentClientId
  ...${components.CoreParagraph.fragments.key}
}

A Simple Block Example

This is a simple block called CoreParagraph. The block is a p tag that sets its content to attributes.content which is passed in from the props.

CoreParagraph.fragments does a WPGraphQL query for the content and cssClassName and sets it as the fragment CoreParagraphFragment.

import { gql } from '@apollo/client';
import React from 'react';

export default function CoreParagraph(props) {
  const attributes = props.attributes;
  return (
    <p
      className={attributes?.cssClassName}
      dangerouslySetInnerHTML={{ __html: attributes.content }}></p>
  );
}

CoreParagraph.fragments = {
  entry: gql`
    fragment CoreParagraphFragment on CoreParagraph {
      attributes {
        content
        cssClassName
      }
    }
  `,
  key: `CoreParagraphFragment`,
};

Use a default barrel export of the CoreParagraph Block in index.js:

import CoreParagraph from './CoreParagraph';
export default {
  'CoreParagraph': CoreParagraph,
};

By doing so the framework will match the name of the export CoreParagraph with the __typename or name fields in the query response. If it finds a match it will resolve the Component associated with that name.

Further Learning

More details on the WordPressBlocksProvider.

More details on the WordPressBlocksViewer.

Continue learning about the project structure, how to change styles, layout, etc. by referencing the Example Project Walkthrough Structure.

Please see https://faustjs.org/docs/gutenberg/getting-started for the Getting Started Guide for Gutenberg Blocks.

版本与发布记录

@faustwp/blocks 共发布 105 个版本,首次发布于 2023/01/20;过去 12 个月发布了 12 个版本。最新版本附带 npm provenance 构建来源证明,可追溯到公开的 CI 构建记录。

版本发布日期距今
7.1.1latest2026/11/0410 天前
7.1.02026/10/2619 天前
7.0.02026/10/2322 天前
6.1.142026/10/2024 天前
6.1.132026/10/1925 天前
6.1.122026/10/091 个月前

其他发布通道:canary → 0.0.0-20251119203751。

关于 @faustwp/blocks 的常见问题

@faustwp/blocks 是什么?

Faust Blocks

如何安装 @faustwp/blocks?

在项目目录运行 npm install @faustwp/blocks,或使用 pnpm add / yarn add / bun add。它还需要宿主项目提供 peer 依赖:next(^16.4.2)、react(>=17.0.2)、react-dom(>=17.0.2)、@faustwp/core(>=1.1.0)、@apollo/client(^3.14.1 || ^4.0.0)、@wordpress/style-engine(>=1.18.0)。npm 7 及以上与 pnpm 8 及以上默认会自动安装必需的 peer 依赖,Yarn 需要手动安装。

@faustwp/blocks 支持 TypeScript 吗?

支持。包内自带 TypeScript 类型声明(package.json 声明了 types 入口),安装后即可获得类型提示,无需再安装 @types 包。

@faustwp/blocks 支持 ESM 还是 CommonJS?

同时提供 ES Module 与 CommonJS 入口,import 和 require() 都可以使用,打包工具会自动选择合适的版本。

@faustwp/blocks 需要什么版本的 Node.js?

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

@faustwp/blocks 可以商用吗?

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

@faustwp/blocks 的下载量有多少?

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

@faustwp/blocks 还在维护吗?

最新版本 7.1.1 发布于 2026/11/04(10 天前);至今共发布 105 个版本,其中过去 12 个月发布了 12 个;首次发布于 2023/01/20。发布频率只是参考信号:功能稳定的工具可能很少需要更新,建议结合源码仓库的 issue 和提交记录判断。