Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 6 additions & 5 deletions projects/packages/wp-build-polyfills/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ It is intended to be used while Jetpack supports WordPress versions whose bundle

## Problem

WordPress 7.0 introduces several new packages (`@wordpress/boot`, `@wordpress/route`, `@wordpress/theme`, etc.) that plugins built with [`@wordpress/build`](https://github.com/WordPress/gutenberg/tree/trunk/packages/wp-build) depend on. On older WordPress versions, these packages are missing or ship incomplete implementations — for example, `wp-private-apis` has an allowlist that rejects `@wordpress/theme`, `@wordpress/route`, and newer dashboard packages, and `wp-notices` lacks component exports that `@wordpress/boot` requires.
Plugins built with [`@wordpress/build`](https://github.com/WordPress/gutenberg/tree/trunk/packages/wp-build) depend on `@wordpress/*` packages newer than the ones WordPress 7.0, Jetpack's minimum, bundles. WordPress 7.0 ships no `wp-views` script or `@wordpress/widget-primitives` module, its `wp-private-apis` allowlist rejects newer dashboard packages such as `@wordpress/views` and `@wordpress/widget-dashboard`, and its `wp-rich-text` locks only `useRichText` into `privateApis`.

This package provides those missing or updated packages so that plugins using `@wordpress/build` can work across Jetpack's supported WordPress versions.

Expand All @@ -23,7 +23,7 @@ This package provides those missing or updated packages so that plugins using `@
| `wp-theme` | `@wordpress/theme` | No — only registered if absent |
| `wp-views` | `@wordpress/views` | No — only registered if absent |

`wp-rich-text`, `wp-theme` and `wp-views` require `wp-private-apis`, because each opts into private APIs under a package name Core's allowlist rejects (WP 6.9 rejects all three; WP 7.0 still rejects `@wordpress/compose`, which the rich-text polyfill bundles). Requesting any of them implicitly requests `wp-private-apis` too — see `WP_Build_Polyfills::SCRIPT_DEPENDENCIES`.
`wp-rich-text`, `wp-theme` and `wp-views` require `wp-private-apis`: requesting any of them implicitly requests `wp-private-apis` too — see `WP_Build_Polyfills::SCRIPT_DEPENDENCIES` for why.

### Script modules (ESM)

Expand All @@ -32,15 +32,16 @@ This package provides those missing or updated packages so that plugins using `@
| `@wordpress/boot` | `@wordpress/boot` |
| `@wordpress/route` | `@wordpress/route` |
| `@wordpress/a11y` | `@wordpress/a11y` |
| `@wordpress/widget-primitives` | `@wordpress/widget-primitives` |

Script modules use "first-wins" semantics — if Core or Gutenberg already registered the module, the polyfill is silently ignored.
Script modules use "first-wins" semantics — if Core or Gutenberg already registered the module, the polyfill is silently ignored. The exception is `@wordpress/widget-primitives`, which replaces the copy an active Gutenberg older than 23.9.0 registers.

## How it works

1. `WP_Build_Polyfills::register()` hooks into `wp_default_scripts` at **priority 20**, after Core (priority 0) and Gutenberg (priority 10) have registered their scripts.
1. `WP_Build_Polyfills::register()` hooks into `wp_default_scripts` at **priority 20**, after Core and Gutenberg (both at the default priority 10) have registered their scripts.
2. For each polyfill, it checks whether a built asset file exists (`build/scripts/*/index.asset.php` or `build/modules/*/index.asset.php`).
3. For classic scripts, it checks whether the handle is already registered. Scripts marked for force replacement are deregistered and re-registered with the polyfill version when the WordPress version is below the script's threshold and active Gutenberg is not known to provide a compatible implementation. Non-force scripts are skipped if already registered.
4. For script modules, it calls `wp_register_script_module()`, which silently ignores duplicates.
4. For script modules, it calls `wp_register_script_module()`, which ignores duplicates, except that `@wordpress/widget-primitives` first deregisters the copy an active Gutenberg older than 23.9.0 registers.

`wp-private-apis` has an additional Gutenberg-version guard because the dashboard packages require a private-apis allowlist that includes `@wordpress/widget-dashboard`. Gutenberg 23.4.0 and older do not include that allowlist entry; Gutenberg 23.5.0 is expected to be the first active-Gutenberg version that matches the current `@next` package build used here.

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,10 +20,9 @@
* each stamped file's sibling `*.asset.php` version is recomputed too; see
* `refreshAssetVersion()`.
*
* Note: WordPress core merged script-module i18n
* (`wp_set_script_module_translations()`, WP 7.0). Once Jetpack's WP floor
* reaches 7.0, this post-build stamp — and the runtime catalog loader that
* pairs with it — can likely be retired.
* Note: every supported WordPress ships script-module i18n
* (`wp_set_script_module_translations()`, since 7.0), so this post-build stamp —
* and the runtime catalog loader that pairs with it — can likely be retired.
*/

const crypto = require( 'crypto' );
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
Significance: patch
Type: changed
Comment: Comment-only update to reflect the WordPress 7.0 floor.


Original file line number Diff line number Diff line change
Expand Up @@ -73,9 +73,8 @@ public static function print_styles() {
* with the admin menu: a menu taller than the viewport stretches the app
* and scrolls its sticky header away, so cap `#wpbody` to the viewport to
* give the app back its own scroller. Below 783px the menu is off-canvas
* and the template scrolls `#wpwrap` instead. Drop this once every boot
* this package can run against pins its own layout: Core's bundled copy
* from WordPress 7.0, and the polyfilled one below that
* and the template scrolls `#wpwrap` instead. Drop this once the minimum
* supported WordPress bundles a boot that pins its own layout
* (WordPress/gutenberg#82114).
*/
@media (min-width: 783px) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,8 @@
/**
* Polyfill registration for Core packages not available or incomplete in older WordPress versions.
*
* Conditionally registers wp-notices, wp-private-apis, wp-theme (classic scripts) and
* `@wordpress/boot`, `@wordpress/route`, `@wordpress/a11y`, `@wordpress/widget-primitives`
* (script modules) ONLY when they are not already provided by Core or Gutenberg.
* On WordPress 7.0 at the default threshold, only wp-private-apis and wp-rich-text replace Core's
* copies; every other polyfill fills in what Core lacks, or replaces a Gutenberg copy too old to use.
*
* @package automattic/jetpack-wp-build-polyfills
*/
Expand All @@ -31,11 +30,11 @@ class WP_Build_Polyfills {
*
* These bundles call `__dangerousOptInToUnstableAPIsOnlyForCoreModules()` at
* module scope, which throws unless the `wp-private-apis` implementation that
* actually loads allowlists their package name. Core's allowlist does not:
* WP 6.9 omits `@wordpress/rich-text`, `@wordpress/theme` and
* `@wordpress/views`, and WP 7.0 still omits `@wordpress/compose` (bundled
* into the rich-text polyfill). Requesting one of these without
* `wp-private-apis` therefore throws at load time and blanks the page.
* actually loads allowlists their package name. WP 7.0's allowlist omits
* `@wordpress/views` and `@wordpress/compose` (bundled into the rich-text
* polyfill), so requesting either polyfill without `wp-private-apis` blanks
* the page. It does allow `@wordpress/theme`, and WP 7.0 registers `wp-theme`
* itself, so the theme polyfill never loads there.
*
* The `wp-private-apis` script dependency in each `.asset.php` is not enough
* on its own — it makes WordPress enqueue the *handle*, which resolves to
Expand Down Expand Up @@ -81,7 +80,7 @@ class WP_Build_Polyfills {
private static $requested = array();

/**
* Whether the wp_default_scripts hook has already been added.
* Whether registration has already run, or been hooked to wp_default_scripts.
*
* @var bool
*/
Expand Down Expand Up @@ -167,13 +166,9 @@ public static function register( $consumer, $polyfills, $wp_version_threshold =
}
self::$hooked = true;

// `wp_default_scripts` fires once when the WP_Scripts singleton is
// instantiated. If something has already initialized `wp_scripts()` —
// common on admin requests where WP or other plugins register scripts
// before `admin_menu` priority 1 runs — adding this hook here is too
// late and the polyfills never register. Detect that case and run the
// registration synchronously so consumers can rely on the script
// handles and module IDs being available regardless of init order.
// `wp_default_scripts` fires when `wp_scripts()` creates the WP_Scripts singleton, and again on
// `init` if it was created before then. Once it has fired (common on admin requests by
// `admin_menu`), a hook added now may never run, so register synchronously instead.
if ( did_action( 'wp_default_scripts' ) ) {
self::register_scripts( wp_scripts(), $build_dir, $base_file, self::$wp_version_threshold );
self::register_modules( $build_dir, $base_file );
Expand Down Expand Up @@ -216,32 +211,28 @@ private static function register_scripts( $scripts, $build_dir, $base_file, $wp_
'wp-notices' => array(
'path' => 'notices',
'force_threshold' => '7.0',
// Only force-replace on older WP without Gutenberg: older Core
// versions ship notices without SnackbarNotices and InlineNotices
// component exports that @wordpress/boot depends on.
// WP 7.0 ships the SnackbarNotices and InlineNotices exports
// @wordpress/boot depends on, so on a supported site this forces only
// when a consumer raises the threshold, and never with Gutenberg active.
),
'wp-private-apis' => array(
'path' => 'private-apis',
'force_threshold' => '7.1',
'gutenberg_min_version' => self::GUTENBERG_PRIVATE_APIS_MIN_VERSION,
// WP 7.0 and older versions ship private-apis with an incomplete
// allowlist that rejects @wordpress/theme, @wordpress/route, and
// newer dashboard packages. Active Gutenberg is only a safe
// substitute once its private-apis allowlist includes those
// dashboard packages too.
// WP 7.0's private-apis allowlist rejects newer dashboard packages
// such as @wordpress/views and @wordpress/widget-dashboard. Active
// Gutenberg is only a safe substitute once its private-apis
// allowlist includes those dashboard packages too.
),
'wp-rich-text' => array(
'path' => 'rich-text',
'force_threshold' => '7.1',
'gutenberg_min_version' => self::GUTENBERG_RICH_TEXT_MIN_VERSION,
// WP 7.0 and older ship a rich-text whose `privateApis` current
// dashboard dependencies cannot use (e.g. @wordpress/dataviews
// >= 17.2 dataform controls, which unlock it at module scope).
// WP 6.9 exports no `privateApis` at all, which throws "Cannot
// unlock an undefined object"; WP 7.0 exports one locked with
// only `useRichText`, so destructuring the other keys yields
// undefined. Either way the page blanks. Older Gutenberg is not
// a safe substitute either — see the constant's doc.
// WP 7.0 ships a rich-text that locks only `useRichText` into
// `privateApis`, so current dashboard dependencies that unlock more
// keys at module scope (e.g. @wordpress/dataviews >= 17.2 dataform
// controls) get undefined and the page blanks. Older Gutenberg is
// not a safe substitute either — see the constant's doc.
),
'wp-theme' => array(
'path' => 'theme',
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,9 @@
*
* The wp-build (esbuild) pipeline externalizes `@wordpress/i18n` to the shared
* `window.wp.i18n` singleton but never wires up loading of the translation
* catalog, and WordPress core (before 7.0's script-module i18n) has no
* `wp_set_script_translations()` equivalent for script modules. Each dashboard
* registers a boot init module that calls this helper; boot awaits init
* modules before rendering routes, so catalogs install before any translated
* string evaluates.
* catalog. Each dashboard registers a boot init module that calls this helper;
* boot awaits init modules before rendering routes, so catalogs install before
* any translated string evaluates.
*
* The set of bundles to load catalogs for comes from the `i18n-manifest.json`
* that the `stamp-textdomains` build step emits into the build/ directory —
Expand All @@ -32,8 +30,8 @@
* WordPress names JS translation files, fetches the catalog, and installs it
* via `setLocaleData()`.
*
* Once Jetpack's WP floor reaches 7.0, `wp_set_script_module_translations()`
* can likely replace this helper along with the build-time text-domain stamp.
* Every supported WordPress ships `wp_set_script_module_translations()` (since 7.0),
* which can likely replace this helper along with the build-time text-domain stamp.
*/

interface JpI18nLoader {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,7 @@
* Runtime tests for the `wp-rich-text` polyfill's private-APIs contract.
*
* The polyfill exists because Core's rich-text `privateApis` is unusable by
* current dashboard dependencies: WP 6.9 exports none at all ("Cannot unlock
* an undefined object"), and WP 7.0 exports one locked with only
* current dashboard dependencies: WP 7.0 exports one locked with only
* `useRichText`, so the other keys destructure to `undefined`.
*
* Two things can silently break it, and static assertions on the bundle text
Expand All @@ -14,8 +13,7 @@
* (compose is bundled in, because Core's compose lacks the
* `subscribeDelegatedListener` private API rich-text unlocks). Those calls throw
* unless the `wp-private-apis` implementation that actually loads allowlists
* both names. Core's does not — WP 6.9 allowlists neither, and WP 7.0
* allowlists rich-text but still not compose. Hence
* both names. WP 7.0's allowlists rich-text but not compose. Hence
* `WP_Build_Polyfills::SCRIPT_DEPENDENCIES` forcing `wp-private-apis` alongside
* `wp-rich-text`.
*
Expand Down Expand Up @@ -214,10 +212,9 @@ describe( 'wp-rich-text polyfill: private-APIs contract', { skip: missingBuild }
} );

it( 'still opts in for @wordpress/compose, so it cannot run on Core private-apis', () => {
// Documents WHY `SCRIPT_DEPENDENCIES` forces `wp-private-apis`: even the
// newest Core allowlist rejects the bundled compose opt-in. If Core ever
// allowlists `@wordpress/compose`, this test fails — at which point the
// PHP dependency can be revisited rather than silently kept forever.
// Documents WHY `SCRIPT_DEPENDENCIES` forces `wp-private-apis`: WP 7.0, the
// newest Core whose private-apis the polyfill replaces, rejects the bundled
// compose opt-in.
assert.throws(
() => runRichTextAgainst( createPrivateApis( WP_70_ALLOWLIST ) ),
/opt-in to unstable APIs as module "@wordpress\/compose"/,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -503,7 +503,7 @@ public function test_register_scripts_does_not_force_replace_wp_notices_with_act
}

/**
* Test that wp-rich-text is force-replaced on WP 6.9 (core ships no rich-text private APIs).
* Test that wp-rich-text is force-replaced on WP 6.9.
*/
public function test_register_scripts_force_replaces_wp_rich_text_on_old_wp() {
$GLOBALS['wp_version'] = '6.9';
Expand Down
8 changes: 4 additions & 4 deletions projects/packages/wp-build-polyfills/webpack.config.js
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
/**
* Webpack configuration for wp-build-polyfills.
*
* Bundles `@wordpress` packages not available in WordPress Core < 7.0
* as both classic scripts (IIFE) and script modules (ESM).
* Bundles the `@wordpress` packages `WP_Build_Polyfills` registers, as both
* classic scripts (IIFE) and script modules (ESM).
*
* IIFE builds use `@wordpress/dependency-extraction-webpack-plugin` (via
* jetpack-webpack-config's StandardPlugins) for externals and .asset.php.
Expand Down Expand Up @@ -158,8 +158,8 @@ const sharedConfig = {
};

// Plugins disabled for all polyfill builds: no CSS is bundled, and the i18n
// loader/checker are unnecessary since Core doesn't provide translations for
// these packages (they aren't shipped with Core in the first place).
// loader/checker serve this package's own text domain, which the bundles never
// use — their strings are upstream ones in Core's `default` domain.
const disabledPlugins = {
MiniCssExtractPlugin: false,
MiniCssWithRtlPlugin: false,
Expand Down
Loading