@wordpress/build 0.23.1-next.v.202608281122.0 → 0.24.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/README.md CHANGED
@@ -6,11 +6,11 @@ Build tool for WordPress plugins.
6
6
 
7
7
  `@wordpress/build` is an opinionated build system designed for WordPress plugins. It provides:
8
8
 
9
- - **Transpilation**: Converts TypeScript/JSX source code to both CommonJS (`build/`) and ESM (`build-module/`) formats using esbuild
10
- - **Style Compilation**: Processes SCSS files and CSS modules, generating LTR and RTL versions
11
- - **Bundling**: Creates browser-ready bundles for WordPress scripts and modules
12
- - **PHP Generation**: Automatically generates PHP registration files for scripts, modules, and styles
13
- - **Watch Mode**: Incremental rebuilds during development
9
+ - **Transpilation**: Converts TypeScript/JSX source code to both CommonJS (`build/`) and ESM (`build-module/`) formats using esbuild
10
+ - **Style Compilation**: Processes SCSS files and CSS modules, generating LTR and RTL versions
11
+ - **Bundling**: Creates browser-ready bundles for WordPress scripts and modules
12
+ - **PHP Generation**: Automatically generates PHP registration files for scripts, modules, and styles
13
+ - **Watch Mode**: Incremental rebuilds during development
14
14
 
15
15
  ## Installation
16
16
 
@@ -54,10 +54,18 @@ or via npm script:
54
54
 
55
55
  ### Testing Generated Styles
56
56
 
57
- Generated CSS module output skips automatic style injection when `NODE_ENV` is
58
- `test`. Node-based DOM implementations such as jsdom do not reliably support
59
- modern CSS features, so tests that need actual styles in the DOM should
60
- run in a browser environment.
57
+ Generated CSS output skips automatic style injection in Node-based environments
58
+ when `NODE_ENV` is `test` and `process` is defined. Browsers have no `process`,
59
+ so they always receive styles. Node-based DOM implementations such as jsdom do
60
+ not reliably support modern CSS features, so tests that need actual styles in
61
+ the DOM should run in a browser environment.
62
+
63
+ ## Browser support
64
+
65
+ JavaScript and CSS builds use [Browserslist](https://github.com/browserslist/browserslist)
66
+ when specifying build targets. You can either [provide your own](https://github.com/browserslist/browserslist#config-file),
67
+ or the default [`@wordpress/browserslist-config`](https://www.npmjs.com/package/@wordpress/browserslist-config)
68
+ will be used to follow [WordPress browser support](https://make.wordpress.org/core/handbook/best-practices/browser-support/).
61
69
 
62
70
  ## Package Configuration
63
71
 
@@ -67,9 +75,9 @@ Configure your `package.json` with the following optional fields:
67
75
 
68
76
  Controls whether the package is exposed as a bundled WordPress script/module and accessible via the configured global variable.
69
77
 
70
- - **`true`**: The package will be bundled and exposed as a WordPress script. It will be available in WordPress as part of the configured global (e.g., `wp.blockEditor`, `wp.data`, or a custom global name if configured differently).
78
+ - **`true`**: The package will be bundled and exposed as a WordPress script. It will be available in WordPress as part of the configured global (e.g., `wp.blockEditor`, `wp.data`, or a custom global name if configured differently).
71
79
 
72
- - **Omitted or `false` (default)**: The package will not be exposed as a WordPress script. Use this for packages designed solely as dependencies for other packages. The package can still be used as a dependency via npm imports by other packages.
80
+ - **Omitted or `false` (default)**: The package will not be exposed as a WordPress script. Use this for packages designed solely as dependencies for other packages. The package can still be used as a dependency via npm imports by other packages.
73
81
 
74
82
  ```json
75
83
  {
@@ -232,8 +240,8 @@ The prefix used for WordPress script handles in `.asset.php` files (e.g., `wp-da
232
240
 
233
241
  With this configuration:
234
242
 
235
- - `@my-plugin/editor` → `window.myPlugin.editor` with handle `mp-editor`
236
- - `@my-plugin/data` → `window.myPlugin.data` with handle `mp-data`
243
+ - `@my-plugin/editor` → `window.myPlugin.editor` with handle `mp-editor`
244
+ - `@my-plugin/data` → `window.myPlugin.data` with handle `mp-data`
237
245
 
238
246
  ### `wpPlugin.externalNamespaces`
239
247
 
@@ -258,9 +266,9 @@ Additional package namespaces to externalize (consume as externals, not expose).
258
266
 
259
267
  This allows your packages to consume third-party dependencies as externals:
260
268
 
261
- - `import { Cart } from '@woo/cart'` → `window.woo.cart` with handle `woocommerce-cart`
262
- - `import { Button } from '@acme/ui'` → `window.acme.ui` with handle `acme-plugin-ui`
263
- - Dependencies are tracked in `.asset.php` files
269
+ - `import { Cart } from '@woo/cart'` → `window.woo.cart` with handle `woocommerce-cart`
270
+ - `import { Button } from '@acme/ui'` → `window.acme.ui` with handle `acme-plugin-ui`
271
+ - Dependencies are tracked in `.asset.php` files
264
272
 
265
273
  If `handlePrefix` is omitted, it defaults to the namespace key (e.g., `"woo"` → `woo-cart`).
266
274
 
@@ -286,19 +294,20 @@ Pages can be defined as simple strings or as objects with initialization modules
286
294
 
287
295
  **Page Configuration:**
288
296
 
289
- - **String format**: `"my-admin-page"` - Simple page with no init modules
290
- - **Object format**: `{ "id": "page-slug", "init": ["@scope/package"], "experimental": true }` - Page with optional init modules
291
- - **`id`** (required): The page slug used in WordPress admin URLs
292
- - **`init`** (optional): Array of script module IDs to execute during page initialization
293
- - **`experimental`** (optional, default `false`): When `true`, the page is excluded from WordPress Core builds (`IS_WORDPRESS_CORE=true`), along with any route that belongs only to experimental pages.
297
+ - **String format**: `"my-admin-page"` - Simple page with no init modules
298
+ - **Object format**: `{ "id": "page-slug", "init": ["@scope/package"], "capability": "edit_theme_options", "experimental": true }` - Page with optional init modules
299
+ - **`id`** (required): The page slug used in WordPress admin URLs
300
+ - **`init`** (optional): Array of script module IDs to execute during page initialization
301
+ - **`capability`** (optional, default `manage_options`): Capability a user needs to view the page. Full-page mode enforces this itself, since it renders outside the menu page callback flow; match it to the capability used when registering the page's menu entry.
302
+ - **`experimental`** (optional, default `false`): When `true`, the page is excluded from WordPress Core builds (`IS_WORDPRESS_CORE=true`), along with any route that belongs only to experimental pages.
294
303
 
295
304
  **Generated Files:**
296
305
 
297
306
  This generates two page modes:
298
307
 
299
- - `build/pages/my-admin-page/page.php` - Full-page mode (takes over entire admin screen with custom sidebar)
300
- - `build/pages/my-admin-page/page-wp-admin.php` - WP-Admin mode (integrates within standard wp-admin interface)
301
- - `build/pages.php` - Loader for all pages
308
+ - `build/pages/my-admin-page/page.php` - Full-page mode (takes over entire admin screen with custom sidebar)
309
+ - `build/pages/my-admin-page/page-wp-admin.php` - WP-Admin mode (integrates within standard wp-admin interface)
310
+ - `build/pages.php` - Loader for all pages
302
311
 
303
312
  Each mode provides route/menu registration functions and a render callback. Routes are automatically registered for both modes.
304
313
 
@@ -349,8 +358,8 @@ add_menu_page( 'Title', 'Menu', 'capability', 'my-admin-page', 'my_plugin_my_adm
349
358
  **Init Modules:**
350
359
  Init modules are JavaScript packages that execute during page initialization, after menu items and routes are registered and before the app renders. They're ideal for:
351
360
 
352
- - Adding icons to menu items (icons can't be passed from PHP)
353
- - Registering command palette entries
361
+ - Adding icons to menu items (icons can't be passed from PHP)
362
+ - Registering command palette entries
354
363
 
355
364
  **Creating an Init Module:**
356
365
 
@@ -401,9 +410,9 @@ The `init()` function is **mandatory** - all init modules must export this named
401
410
 
402
411
  This configuration:
403
412
 
404
- - Packages like `@wordpress/data` expose `window.wp.data`
405
- - Packages like `@wordpress/block-editor` expose `window.wp.blockEditor`
406
- - All packages can consume `@wordpress/*` as externals
413
+ - Packages like `@wordpress/data` expose `window.wp.data`
414
+ - Packages like `@wordpress/block-editor` expose `window.wp.blockEditor`
415
+ - All packages can consume `@wordpress/*` as externals
407
416
 
408
417
  ### Example: Third-Party Plugin
409
418
 
@@ -419,18 +428,18 @@ This configuration:
419
428
 
420
429
  This configuration:
421
430
 
422
- - Packages like `@acme/editor` expose `window.acme.editor`
423
- - Packages like `@acme/data` expose `window.acme.data`
424
- - All packages can still consume `@wordpress/*` → `window.wp.*`
425
- - All packages can still consume vendors (react, lodash) → `window.React`, `window.lodash`
431
+ - Packages like `@acme/editor` expose `window.acme.editor`
432
+ - Packages like `@acme/data` expose `window.acme.data`
433
+ - All packages can still consume `@wordpress/*` → `window.wp.*`
434
+ - All packages can still consume vendors (react, lodash) → `window.React`, `window.lodash`
426
435
 
427
436
  ### Behavior
428
437
 
429
- - **Packages with `wpScript: true` matching the namespace**: Bundled with global exposure
430
- - **Packages with `wpScript: true` not matching the namespace**: Bundled without global exposure
431
- - **Dependencies**: `@wordpress/*` packages are always externalized to `wp.*` globals
432
- - **Vendors**: React, lodash, jQuery, moment are always externalized to their standard globals
433
- - **Asset files**: `.asset.php` files are always generated for WordPress dependency management
438
+ - **Packages with `wpScript: true` matching the namespace**: Bundled with global exposure
439
+ - **Packages with `wpScript: true` not matching the namespace**: Bundled without global exposure
440
+ - **Dependencies**: `@wordpress/*` packages are always externalized to `wp.*` globals
441
+ - **Vendors**: React, lodash, jQuery, moment are always externalized to their standard globals
442
+ - **Asset files**: `.asset.php` files are always generated for WordPress dependency management
434
443
 
435
444
  ## Output Structure
436
445
 
@@ -484,8 +493,8 @@ For routes that should appear on multiple pages:
484
493
 
485
494
  The `page` field can be either:
486
495
 
487
- - **String**: Route belongs to a single page
488
- - **Array**: Route appears on multiple pages (the build system will register the route for each page)
496
+ - **String**: Route belongs to a single page
497
+ - **Array**: Route appears on multiple pages (the build system will register the route for each page)
489
498
 
490
499
  Each page ID must match one of the pages defined in `wpPlugin.pages` in your root `package.json`. This tells the build system which page(s) this route belongs to. It can also map to existing pages registered by other plugins.
491
500
 
@@ -519,12 +528,12 @@ The canvas is a full-screen area typically used for editor previews. To use a cu
519
528
 
520
529
  Export a `route` object with optional hooks. Each hook receives `{ params, search }` — path parameters and query string values from the URL.
521
530
 
522
- - **`beforeLoad`** — Runs before navigation completes. Use for auth checks, validation, or redirects. Throw `redirect()` or `notFound()` from `@wordpress/route` to abort navigation.
523
- - **`loader`** — Runs while the route is loading. Use to preload data (for example, `resolveSelect` from `@wordpress/data`) so `stage` components can read from the store without a loading state. May return an object whose properties are merged into the route's loader data. For data that must be available before the JavaScript application loads, use [`rest_preload_api_request()`](https://developer.wordpress.org/reference/functions/rest_preload_api_request/) in your page's PHP render callback instead.
524
- - **`canvas`** — Runs in parallel with `loader`. Controls which canvas is rendered depending on return values:
525
- - `CanvasData` (`{ postType, postId, isPreview?, editLink? }`) → default WordPress editor canvas
526
- - `null` → custom `canvas.tsx` component (if provided)
527
- - `undefined` or omitted → no canvas
531
+ - **`beforeLoad`** — Runs before navigation completes. Use for auth checks, validation, or redirects. Throw `redirect()` or `notFound()` from `@wordpress/route` to abort navigation.
532
+ - **`loader`** — Runs while the route is loading. Use to preload data (for example, `resolveSelect` from `@wordpress/data`) so `stage` components can read from the store without a loading state. May return an object whose properties are merged into the route's loader data. For data that must be available before the JavaScript application loads, use [`rest_preload_api_request()`](https://developer.wordpress.org/reference/functions/rest_preload_api_request/) in your page's PHP render callback instead.
533
+ - **`canvas`** — Runs in parallel with `loader`. Controls which canvas is rendered depending on return values:
534
+ - `CanvasData` (`{ postType, postId, isPreview?, editLink? }`) → default WordPress editor canvas
535
+ - `null` → custom `canvas.tsx` component (if provided)
536
+ - `undefined` or omitted → no canvas
528
537
 
529
538
  ```tsx
530
539
  export const route = {
@@ -556,10 +565,10 @@ export const route = {
556
565
 
557
566
  The build system generates:
558
567
 
559
- - `build/routes/{route-name}/content.js` - Bundled stage/inspector/canvas components
560
- - `build/routes/{route-name}/route.js` - Bundled lifecycle hooks (if present)
561
- - `build/routes/registry.php` - Route registry data
562
- - `build/routes.php` - Route registration logic
568
+ - `build/routes/{route-name}/content.js` - Bundled stage/inspector/canvas components
569
+ - `build/routes/{route-name}/route.js` - Bundled lifecycle hooks (if present)
570
+ - `build/routes/registry.php` - Route registry data
571
+ - `build/routes.php` - Route registration logic
563
572
 
564
573
  The boot package in Gutenberg will automatically use these routes and make them available.
565
574
 
@@ -576,7 +585,7 @@ Widgets provide a file-based discovery system for building self-contained UI com
576
585
  widgets/
577
586
  hello-world/
578
587
  widget.json # Static discovery metadata (required)
579
- widget.ts # Runtime schema entry point (optional)
588
+ widget.ts # Runtime values entry point (optional)
580
589
  render.tsx # UI component entry point (optional)
581
590
  render.scss # Optional styles (bundled inline when imported from render.tsx)
582
591
  package.json # Optional npm dependencies manifest
@@ -586,15 +595,15 @@ widgets/
586
595
 
587
596
  Widgets use a dual-entry pattern, similar in spirit to how blocks split metadata between `block.json` and `edit.js`:
588
597
 
589
- | Concern | Lives in | Reason |
590
- | ------------------------------------------------------------------ | ----------------- | ---------------------------------------------------------------------------------------------------- |
591
- | Identity (`name`) | both (must match) | server needs it to register; client uses it to resolve the runtime entry |
592
- | Translatable metadata (`title`, `description`, `help`, `keywords`) | `widget.json` | translated server-side via `textdomain`, so the host receives localized strings without a JS runtime |
593
- | Framing (`category`, `presentation`) | `widget.json` | plain JSON the host reads without a JS runtime |
594
- | Attribute schema (types, options, labels, `relevance`) | `widget.ts` | needs TypeScript coupling with `render` props; labels are translated with `__()` |
595
- | Icon and `example` | `widget.ts` | runtime values co-located with the attribute schema |
598
+ | Concern | Lives in | Reason |
599
+ | ------------------------------------------------------------------ | ----------------- | -------------------------------------------------------------------------------------------------------------------------------- |
600
+ | Identity (`name`) | both (must match) | server needs it to register; client uses it to resolve the runtime entry |
601
+ | Translatable metadata (`title`, `description`, `help`, `keywords`) | `widget.json` | translated server-side via `textdomain`, so the host receives localized strings without a JS runtime |
602
+ | Framing (`category`, `presentation`) | `widget.json` | plain JSON the host reads without a JS runtime |
603
+ | Attribute schema (types, options, labels, `relevance`) | `widget.json` | plain JSON; labels translated server-side via `textdomain`; a `widget.ts` entry with the same `id` adds what JSON cannot express |
604
+ | `example`, attribute props that need code | `widget.ts` | runtime values |
596
605
 
597
- Rule of thumb: anything the host needs before loading a widget's module — identity, translatable copy, framing — goes in `widget.json`, which the build forwards to `build/widgets/registry.php` and exposes through the REST API. Anything that needs TypeScript or runtime values (attribute schema, icon, `example`) goes in `widget.ts`.
606
+ Rule of thumb: anything the host needs before loading a widget's module — identity, translatable copy, framing, attribute schema — goes in `widget.json`, which the build forwards to `build/widgets/registry.php` and exposes through the REST API. Anything that needs code (`example`, an attribute's `Edit` component or custom validator) goes in `widget.ts`.
598
607
 
599
608
  ### `widget.json` — static discovery metadata
600
609
 
@@ -613,23 +622,23 @@ Rule of thumb: anything the host needs before loading a widget's module — iden
613
622
 
614
623
  **Fields:**
615
624
 
616
- - **`name`** (required): Namespaced identifier (e.g., `"my-plugin/hello-world"`)
617
- - **`title`** (optional): Human-readable title. Translated server-side using `textdomain`.
618
- - **`description`** (optional): Short description. Translated server-side using `textdomain`.
619
- - **`help`** (optional): Contextual help note for compact surfaces. An object with `content` (may carry `<em>`/`<strong>`) and optional `links` (`label`, `href`). Translated server-side using `textdomain`.
620
- - **`keywords`** (optional): Search aliases. Translated server-side using `textdomain`.
621
- - **`category`** (optional): Grouping category for filtering
622
- - **`presentation`** (optional): Rendering intent (`framed`, `content-bleed`, `full-bleed`)
623
- - **`textdomain`** (optional): Gettext text domain for translating `title`, `description`, `help`, and `keywords`
625
+ - **`name`** (required): Namespaced identifier (e.g., `"my-plugin/hello-world"`)
626
+ - **`title`** (optional): Human-readable title. Translated server-side using `textdomain`.
627
+ - **`description`** (optional): Short description. Translated server-side using `textdomain`.
628
+ - **`help`** (optional): Contextual help note for compact surfaces. An object with `content` (may carry `<em>`/`<strong>`) and optional `links` (`label`, `href`). Translated server-side using `textdomain`.
629
+ - **`keywords`** (optional): Search aliases. Translated server-side using `textdomain`.
630
+ - **`attributes`** (optional): Attribute schema, one entry per attribute with the JSON-expressible subset of a DataViews `Field` (`id`, `type`, `label`, `elements`, `isValid` without `custom`, ...) plus an optional `relevance` (`high`, `medium`, `low`). `type` may name a registered field type. `label`, `header`, `description`, `placeholder`, and option labels and descriptions are translated server-side using `textdomain`.
631
+ - **`category`** (optional): Grouping category for filtering
632
+ - **`presentation`** (optional): Rendering intent (`framed`, `content-bleed`, `full-bleed`)
633
+ - **`textdomain`** (optional): Gettext text domain for translating `title`, `description`, `help`, `keywords`, and attribute labels
624
634
 
625
- ### `widget.ts` — runtime schema
635
+ ### `widget.ts` — runtime values
626
636
 
627
- Exports a default object with the widget's runtime contract: typed attributes (with translated labels), icon, and example data. Identity and translatable copy (`title`, `description`, `keywords`) live in `widget.json`. The build system injects the `render_module` handle at registration time, so authors don't need to declare it.
637
+ Optional. Exports a default object with what needs code: `example` data, and attribute entries that add an `Edit` component or a custom validator to a `widget.json` attribute with the same `id`. The build system injects the `render_module` handle at registration time, so authors don't need to declare it.
628
638
 
629
639
  ```ts
630
- import { wordpress } from '@wordpress/icons';
631
- import { __ } from '@wordpress/i18n';
632
640
  import type { WidgetAttributeField } from '@wordpress/widget-primitives';
641
+ import { GreetingControl } from './greeting-control';
633
642
 
634
643
  type HelloWorldAttributes = {
635
644
  greeting: string;
@@ -638,20 +647,9 @@ type HelloWorldAttributes = {
638
647
 
639
648
  const widget = {
640
649
  name: 'my-plugin/hello-world',
641
- icon: wordpress,
642
- attributes: [
643
- {
644
- id: 'greeting',
645
- type: 'text',
646
- label: __( 'Greeting', 'my-plugin' ),
647
- relevance: 'high',
648
- },
649
- {
650
- id: 'showDate',
651
- type: 'boolean',
652
- label: __( 'Show current date', 'my-plugin' ),
653
- },
654
- ] satisfies WidgetAttributeField< HelloWorldAttributes >[],
650
+ attributes: [ { id: 'greeting', Edit: GreetingControl } ] satisfies Partial<
651
+ WidgetAttributeField< HelloWorldAttributes >
652
+ >[],
655
653
  example: {
656
654
  attributes: {
657
655
  greeting: 'World',
@@ -663,13 +661,11 @@ const widget = {
663
661
  export default widget;
664
662
  ```
665
663
 
666
- Each attribute entry is a DataViews `Field` plus an optional `relevance` hint (`'high' | 'medium' | 'low'`). The widget declares importance; the host decides where to expose each field. When absent, `relevance` defaults to `'low'`.
667
-
668
- The same `HelloWorldAttributes` type is consumed by `render.tsx`, giving the author a single source of truth for the data contract.
664
+ Entries merge by `id` with the `widget.json` schema: the JSON value wins a shared key, the module adds the rest, and `isValid` merges rule by rule so JSON rules and a module `custom` validator coexist. A widget whose attributes are plain data needs no `widget.ts`.
669
665
 
670
666
  ### `render.tsx` — UI component
671
667
 
672
- The render entry receives `attributes` matching the schema declared in `widget.ts`. It is bundled with CSS support (import `.scss` / `.css` directly).
668
+ The render entry receives `attributes` matching the schema declared in `widget.json`. It is bundled with CSS support (import `.scss` / `.css` directly).
673
669
 
674
670
  ```tsx
675
671
  interface HelloWorldRenderProps {
@@ -697,12 +693,12 @@ All non-JSON entries are optional. The build system checks for files with extens
697
693
 
698
694
  The build system generates:
699
695
 
700
- - `build/widgets/{widget-name}/render.min.js` + `render.js` — Bundled UI component (ESM)
701
- - `build/widgets/{widget-name}/render.min.asset.php` — Asset metadata for render module
702
- - `build/widgets/{widget-name}/widget.min.js` + `widget.js` — Bundled metadata (ESM)
703
- - `build/widgets/{widget-name}/widget.min.asset.php` — Asset metadata for widget module
704
- - `build/widgets/registry.php` — Widget registry data
705
- - `build/widgets.php` — Script module registration logic
696
+ - `build/widgets/{widget-name}/render.min.js` + `render.js` — Bundled UI component (ESM)
697
+ - `build/widgets/{widget-name}/render.min.asset.php` — Asset metadata for render module
698
+ - `build/widgets/{widget-name}/widget.min.js` + `widget.js` — Bundled metadata (ESM)
699
+ - `build/widgets/{widget-name}/widget.min.asset.php` — Asset metadata for widget module
700
+ - `build/widgets/registry.php` — Widget registry data
701
+ - `build/widgets.php` — Script module registration logic
706
702
 
707
703
  ### PHP Registration
708
704
 
@@ -0,0 +1,15 @@
1
+ import browserslist from 'browserslist';
2
+ import wordpressBrowserslistConfig from '@wordpress/browserslist-config';
3
+
4
+ /**
5
+ * Resolve the Browserslist queries for a project.
6
+ *
7
+ * Uses the project's own config if one is present, otherwise falling back to
8
+ * `@wordpress/browserslist-config` instead of Browserslist's implicit defaults.
9
+ *
10
+ * @return {string[]} Browserslist queries.
11
+ */
12
+ export function getBrowserslistQueries() {
13
+ const config = browserslist.findConfig( process.cwd() );
14
+ return config?.defaults || wordpressBrowserslistConfig;
15
+ }