块编辑器开发文档

区块开发环境

💡 云策文档标注

概述

本文档指导如何设置用于创建区块和扩展 WordPress 区块编辑器的插件开发环境。核心包括代码编辑器、Node.js 开发工具和本地 WordPress 环境。

关键要点

  • 代码编辑器:推荐使用 Visual Studio Code(VS Code),它跨平台、开源且支持 WordPress 开发插件。
  • Node.js 开发工具:必需用于现代 JavaScript 开发,支持安装 wp-scripts、wp-env 等 WordPress 包,推荐使用 Active LTS 版本和 nvm 版本管理器。
  • 本地 WordPress 环境:提供安全高效的开发空间,推荐使用 wp-env 工具,也可选择 WordPress Studio、Local 等替代方案。

注意事项

  • 为 Gutenberg 项目贡献代码需参考代码贡献指南的额外文档。
  • Node.js 工具需正确设置运行时环境,具体安装方法参考相关链接。
  • @wp-playground/cli 是实验性工具,适用于快速测试 WordPress 发布、插件和主题。

📄 原文内容

This guide will help you set up the right development environment to create blocks and other plugins that extend and modify the Block Editor in WordPress.

A block development environment includes the tools you need on your computer to successfully develop for the Block Editor. The three essential requirements are:

To contribute to the Gutenberg project itself, refer to the additional documentation in the code contribution guide.

Code editor

A code editor is used to write code. You can use whichever editor you’re most comfortable with. The key is having a way to open, edit, and save text files.

If you do not already have a preferred code editor, Visual Studio Code (VS Code) is a popular choice for JavaScript development among Core contributors. It works well across the three major platforms (Windows, Linux, and Mac), is open-source, and is actively maintained by Microsoft. VS Code also has a vibrant community providing plugins and extensions, including many for WordPress development.

Node.js development tools

Node.js (node) is an open-source runtime environment that allows you to execute JavaScript outside of the web browser. While Node.js is not required for all WordPress JavaScript development, it’s essential when working with modern JavaScript tools and developing for the Block Editor.

Node.js and its accompanying development tools allow you to:

  • Install and run WordPress packages needed for Block Editor development, such as wp-scripts
  • Set up local WordPress environments with wp-env and @wp-playground/cli
  • Use the latest ECMAScript features and write code in ESNext
  • Lint, format, and test JavaScript code
  • Scaffold custom blocks with the create-block package

The list goes on. While modern JavaScript development can be challenging, WordPress provides several tools, like wp-scripts and create-block, that streamline the process and are made possible by Node.js development tools.

The recommended Node.js version for block development is Active LTS (Long Term Support). However, there are times when you need to use different versions. A Node.js version manager tool like nvm is strongly recommended and allows you to change your node version when required. You will also need Node Package Manager (npm) and the Node Package eXecute (npx) to work with some WordPress packages. Both are installed automatically with Node.js.

To be able to use the Node.js tools and packages provided by WordPress for block development, you’ll need to set a proper Node.js runtime environment on your machine. To learn more about how to do this, refer to the links below.

Local WordPress environment

A local WordPress environment (site) provides a controlled, efficient, and secure space for development, allowing you to build and test your code before deploying it to a production site. The same requirements for WordPress apply to local sites.

In the broader WordPress community, many tools are available for setting up a local WordPress environment on your computer. The Block Editor Handbook covers wp-env, which is open-source and maintained by the WordPress project itself. It’s also the recommended tool for Gutenberg development.

Refer to the Get started with wp-env guide for setup instructions.

Throughout the Handbook, you may also see references to <a href="https://github.com/WordPress/wordpress-playground/tree/trunk/packages/playground/cli">@wp-playground/cli</a>. This is a lightweight tool powered by WordPress Playground that streamlines setting up a simple local WordPress environment. While still experimental, this tool is great for quickly testing WordPress releases, plugins, and themes.

This list is not exhaustive, but here are several additional options to choose from if you prefer not to use wp-env: