块编辑器开发文档

@wordpress/experiments

💡 云策文档标注

概述

@wordpress/experiments 是一个 WordPress 包,用于在 @wordpress 包之间安全地共享私有实验性 API,避免向 WordPress 扩展者公开暴露。它通过 lock() 和 unlock() 函数实现数据隐藏和访问控制。

关键要点

  • @wordpress/experiments 允许 @wordpress 包私有地访问或暴露实验性 API,而不公开给插件或主题开发者。
  • 包必须通过 __dangerousOptInToUnstableAPIsOnlyForCoreModules 函数选择加入,提供特定同意字符串和包名,每个包只能选择加入一次。
  • 使用 lock() 将私有数据(如函数、字符串)附加到导出对象,使其不可见;unlock() 用于在授权包内访问这些数据。
  • 实验性 API 不遵循 WordPress 核心的向后兼容性原则,同意字符串可能随时更改,导致第三方代码中断。
  • 插件和主题开发者不建议使用此包,因为它设计为仅供核心模块使用,且存在技术限制防止滥用。

代码示例

// 选择加入示例
import { __dangerousOptInToUnstableAPIsOnlyForCoreModules } from '@wordpress/experiments';
export const { lock, unlock } =
    __dangerousOptInToUnstableAPIsOnlyForCoreModules(
        'I know using unstable features means my plugin or theme will inevitably break on the next WordPress release.',
        '@wordpress/block-editor'
    );

// 使用 lock() 和 unlock() 示例
export const publicObject = {};
const __experimentalString = '__experimental information';
lock( publicObject, __experimentalString );
console.log( unlock( publicObject ) ); // 输出: "__experimental information"

注意事项

  • __dangerousOptInToUnstableAPIsOnlyForCoreModules 函数名表明插件不应使用,仅限核心模块。
  • 选择加入时,第一个参数必须精确匹配同意字符串,第二个参数必须是已知且未选择加入的 @wordpress 包名。
  • 实验性 API 的同意字符串可能在任何版本中更改而无通知,这会破坏第三方代码。
  • 此包是 Gutenberg 项目的一部分,采用 monorepo 结构,贡献需参考项目指南。

📄 原文内容

@wordpress/experiments enables sharing private __experimental APIs across @wordpress packages without
publicly exposing them to WordPress extenders.

Getting started

Every @wordpress package wanting to privately access or expose experimental APIs must opt-in to @wordpress/experiments:

// In packages/block-editor/experiments.js:
import { __dangerousOptInToUnstableAPIsOnlyForCoreModules } from '@wordpress/experiments';
export const { lock, unlock } =
    __dangerousOptInToUnstableAPIsOnlyForCoreModules(
        'I know using unstable features means my plugin or theme will inevitably break on the next WordPress release.',
        '@wordpress/block-editor' // Name of the package calling __dangerousOptInToUnstableAPIsOnlyForCoreModules,
                                  // (not the name of the package whose APIs you want to access)
    );

Each package may only opt in once. The function name communicates that plugins are not supposed to use it.

The function will throw an error if the following conditions are not met:

  1. The first argument must exactly match the required consent string: 'I know using unstable features means my plugin or theme will inevitably break on the next WordPress release.'.
  2. The second argument must be a known @wordpress package that hasn’t yet opted into @wordpress/experiments

Once the opt-in is complete, the obtained lock() and unlock() utilities enable hiding __experimental APIs from the naked eye:

// Say this object is exported from a package:
export const publicObject = {};

// However, this string is internal and should not be publicly available:
const __experimentalString = '__experimental information';

// Solution: lock the string "inside" of the object:
lock( publicObject, __experimentalString );

// The string is not nested in the object and cannot be extracted from it:
console.log( publicObject );
// {}

// The only way to access the string is by "unlocking" the object:
console.log( unlock( publicObject ) );
// "__experimental information"

// lock() accepts all data types, not just strings:
export const anotherObject = {};
lock( anotherObject, function __experimentalFn() {} );
console.log( unlock( anotherObject ) );
// function __experimentalFn() {}

Use lock() and unlock() to privately distribute the __experimental APIs across @wordpress packages:

// In packages/package1/index.js:
import { lock } from './experiments';

export const experiments = {};
/* Attach private data to the exported object */
lock(experiments, {
    __experimentalFunction: function() {},
});

// In packages/package2/index.js:
import { experiments } from '@wordpress/package1';
import { unlock } from './experiments';

const {
    __experimentalFunction
} = unlock( experiments );

Shipping experimental APIs

See the Experimental and Unstable APIs chapter of Coding Guidelines to learn how lock() and unlock() can help
you ship private experimental functions, arguments, components, properties, actions, selectors.

Technical limitations

A determined developer who would want to use the private experimental APIs at all costs would have to:

  • Realize a private importing system exists
  • Read the code where the risks would be spelled out in capital letters
  • Explicitly type out he or she is aware of the consequences
  • Pretend to register a @wordpress package (and trigger an error as soon as the real package is loaded)

Dangerously opting in to using these APIs by theme and plugin developers is not recommended. Furthermore, the WordPress Core philosophy to strive to maintain backward compatibility for third-party developers does not apply to experimental APIs registered via this package.

The consent string for opting in to these APIs may change at any time and without notice. This change will break existing third-party code. Such a change may occur in either a major or minor release.

Contributing to this package

This is an individual package that’s part of the Gutenberg project. The project is organized as a monorepo. It’s made up of multiple self-contained software packages, each with a specific purpose. The packages in this monorepo are published to npm and used by WordPress as well as other software projects.

To find out more about contributing to this package or Gutenberg as a whole, please read the project’s main contributor guide.