Modularity


目录

The WordPress block editor is based around the idea that you can combine independent blocks together to write your post or build your page. Blocks can also use and interact with each other. This makes it very modular and flexible.

But the Block Editor does not embrace modularity for its behavior and output only. The Gutenberg repository is also built from the ground up as several reusable and independent modules or packages, that, combined together, lead to the application and interface we all know. These modules are known as WordPress packages and are published and updated regularly on the npm package repository.

These packages are used to power the Block Editor, but they can be used to power any page in the WordPress Admin or outside.

Why?

Using a modular architecture has several benefits for all the actors involved:

Types of packages

Almost everything in the Gutenberg repository is built into a package. We can split these packages into two different types:

Production packages

These are the packages that ship in WordPress itself as JavaScript scripts. These constitute the actual production code that runs on your browsers. As an example, there’s a components package serving as a reusable set of React components used to prototype and build interfaces quickly. There’s also an api-fetch package that can be used to call WordPress Rest APIs.

Third-party developers can use these production packages in two different ways:

npm install @wordpress/components
import { Button } from '@wordpress/components';

function MyApp() {
    return <Button>Nice looking button</Button>;
}
// myplugin.php
// Example of script registration depending on the "components" and "element packages.
wp_register_script( 'myscript', 'pathtomyscript.js', array ('wp-components', "react" ) );
// Using the package in your scripts
const { Button } = wp.components;

function MyApp() {
    return <Button>Nice looking button</Button>;
}

Script dependencies definition can be a tedious task for developers. Mistakes and oversight can happen easily. If you want to learn how you can automate this task. Check the @wordpress/scripts and @wordpress/dependency-extraction-webpack-plugin documentation.

Packages with stylesheets

Some production packages provide stylesheets to function properly.

In the context of existing WordPress pages, if you omit to define the scripts or styles dependencies properly, your plugin might still work properly if these scripts and styles are already loaded there by WordPress or by other plugins, but it’s highly recommended to define all your dependencies exhaustively if you want to avoid potential breakage in future versions.

Packages with data stores

Some WordPress production packages define data stores to handle their state. These stores can also be used by third-party plugins and themes to retrieve data and to manipulate it. The name of these data stores is also normalized following this format core/package-name (E.g. the @wordpress/block-editor package defines and uses the core/block-editor data store).

If you’re using one of these stores to access and manipulate WordPress data in your plugins, don’t forget to add the corresponding WordPress script to your own script dependencies for your plugin to work properly. (For instance, if you’re retrieving data from the core/block-editor store, you should add the wp-block-editor package to your script dependencies like shown above).

Development packages

These are packages used in development mode to help developers with daily tasks to develop, build and ship JavaScript applications, WordPress plugins and themes. They include tools for linting your codebase, building it, testing it…

Editor packages

Post Editor Modules Architecture

What’s the difference between the different editor packages? What’s the purpose of each package?

It’s often surprising to new contributors to discover that the post editor is constructed as a layered abstraction of three separate packages @wordpress/edit-post, @wordpress/editor, and @wordpress/block-editor.

The above Why? section should provide some context for how individual packages aim to satisfy specific requirements. That applies to these packages as well:

Structured this way, these packages can be used in a variety of combinations outside the use-case of the “New Post” screen:

Going further


版本号 #2
由 壳壳虫 创建于 2026-10-31 18:24:08 UTC
由 壳壳虫 更新于 2026-10-31 18:24:10 UTC