@wordpress/build 0.21.0 → 0.23.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/.cache/tsconfig.tsbuildinfo +1 -1
- package/CHANGELOG.md +38 -0
- package/README.md +97 -80
- package/lib/browserslist.mjs +15 -0
- package/lib/build.mjs +59 -99
- package/lib/compile-inline-style.mjs +99 -0
- package/lib/source-files.mjs +25 -0
- package/lib/test/browserslist.js +34 -0
- package/lib/test/compile-inline-style.test.js +69 -0
- package/lib/test/package-utils.js +10 -5
- package/lib/test/php-generator.js +40 -0
- package/lib/test/source-files.js +58 -0
- package/lib/test/style-injection.browser.test.js +34 -0
- package/lib/test/style-injection.jsdom.test.js +11 -0
- package/lib/widget-utils.mjs +7 -7
- package/lib/worker-build.mjs +0 -2
- package/package.json +8 -5
- package/templates/page-wp-admin.php.template +11 -3
- package/templates/page.php.template +21 -1
- package/tsconfig.json +1 -1
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
|
-
-
|
|
10
|
-
-
|
|
11
|
-
-
|
|
12
|
-
-
|
|
13
|
-
-
|
|
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
|
|
58
|
-
`
|
|
59
|
-
|
|
60
|
-
|
|
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
|
-
-
|
|
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
|
-
-
|
|
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
|
{
|
|
@@ -107,7 +115,7 @@ Additional script dependencies:
|
|
|
107
115
|
|
|
108
116
|
```json
|
|
109
117
|
{
|
|
110
|
-
"wpScriptExtraDependencies": ["wp-polyfill"]
|
|
118
|
+
"wpScriptExtraDependencies": [ "wp-polyfill" ]
|
|
111
119
|
}
|
|
112
120
|
```
|
|
113
121
|
|
|
@@ -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
|
-
-
|
|
236
|
-
-
|
|
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
|
-
-
|
|
262
|
-
-
|
|
263
|
-
-
|
|
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
|
|
|
@@ -277,7 +285,7 @@ Pages can be defined as simple strings or as objects with initialization modules
|
|
|
277
285
|
"my-admin-page",
|
|
278
286
|
{
|
|
279
287
|
"id": "my-other-page",
|
|
280
|
-
"init": ["@my-plugin/my-page-init"]
|
|
288
|
+
"init": [ "@my-plugin/my-page-init" ]
|
|
281
289
|
}
|
|
282
290
|
]
|
|
283
291
|
}
|
|
@@ -286,22 +294,29 @@ Pages can be defined as simple strings or as objects with initialization modules
|
|
|
286
294
|
|
|
287
295
|
**Page Configuration:**
|
|
288
296
|
|
|
289
|
-
-
|
|
290
|
-
-
|
|
291
|
-
-
|
|
292
|
-
-
|
|
293
|
-
-
|
|
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
|
-
-
|
|
300
|
-
-
|
|
301
|
-
-
|
|
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
|
|
|
314
|
+
**Boot module:**
|
|
315
|
+
|
|
316
|
+
Pages boot through the `@wordpress/boot` script module, which ships with WordPress Core 7.0+ and the Gutenberg plugin. Plugins do not need their own `packages/boot`.
|
|
317
|
+
|
|
318
|
+
The generated page files use a plugin-local `build/modules/boot/index.min.asset.php` if it exists, and Core's copy in `wp-includes/js/dist/script-modules/boot/` otherwise. If neither is available, the page enqueues nothing and shows only the "This screen requires JavaScript" notice.
|
|
319
|
+
|
|
305
320
|
**Registering a menu item for WP-Admin mode:**
|
|
306
321
|
|
|
307
322
|
WP-Admin mode integrates within the standard WordPress admin interface (keeping the sidebar and header). Menu items should be registered with a simple slug and callback:
|
|
@@ -343,8 +358,8 @@ add_menu_page( 'Title', 'Menu', 'capability', 'my-admin-page', 'my_plugin_my_adm
|
|
|
343
358
|
**Init Modules:**
|
|
344
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:
|
|
345
360
|
|
|
346
|
-
-
|
|
347
|
-
-
|
|
361
|
+
- Adding icons to menu items (icons can't be passed from PHP)
|
|
362
|
+
- Registering command palette entries
|
|
348
363
|
|
|
349
364
|
**Creating an Init Module:**
|
|
350
365
|
|
|
@@ -395,9 +410,9 @@ The `init()` function is **mandatory** - all init modules must export this named
|
|
|
395
410
|
|
|
396
411
|
This configuration:
|
|
397
412
|
|
|
398
|
-
-
|
|
399
|
-
-
|
|
400
|
-
-
|
|
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
|
|
401
416
|
|
|
402
417
|
### Example: Third-Party Plugin
|
|
403
418
|
|
|
@@ -413,18 +428,18 @@ This configuration:
|
|
|
413
428
|
|
|
414
429
|
This configuration:
|
|
415
430
|
|
|
416
|
-
-
|
|
417
|
-
-
|
|
418
|
-
-
|
|
419
|
-
-
|
|
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`
|
|
420
435
|
|
|
421
436
|
### Behavior
|
|
422
437
|
|
|
423
|
-
-
|
|
424
|
-
-
|
|
425
|
-
-
|
|
426
|
-
-
|
|
427
|
-
-
|
|
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
|
|
428
443
|
|
|
429
444
|
## Output Structure
|
|
430
445
|
|
|
@@ -471,15 +486,15 @@ For routes that should appear on multiple pages:
|
|
|
471
486
|
{
|
|
472
487
|
"route": {
|
|
473
488
|
"path": "/settings",
|
|
474
|
-
"page": ["my-admin-page", "other-page"]
|
|
489
|
+
"page": [ "my-admin-page", "other-page" ]
|
|
475
490
|
}
|
|
476
491
|
}
|
|
477
492
|
```
|
|
478
493
|
|
|
479
494
|
The `page` field can be either:
|
|
480
495
|
|
|
481
|
-
-
|
|
482
|
-
-
|
|
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)
|
|
483
498
|
|
|
484
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.
|
|
485
500
|
|
|
@@ -513,22 +528,22 @@ The canvas is a full-screen area typically used for editor previews. To use a cu
|
|
|
513
528
|
|
|
514
529
|
Export a `route` object with optional hooks. Each hook receives `{ params, search }` — path parameters and query string values from the URL.
|
|
515
530
|
|
|
516
|
-
-
|
|
517
|
-
-
|
|
518
|
-
-
|
|
519
|
-
-
|
|
520
|
-
-
|
|
521
|
-
-
|
|
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
|
|
522
537
|
|
|
523
538
|
```tsx
|
|
524
539
|
export const route = {
|
|
525
|
-
beforeLoad: ({ params, search }) => {
|
|
540
|
+
beforeLoad: ( { params, search } ) => {
|
|
526
541
|
// Pre-navigation validation, auth checks
|
|
527
542
|
},
|
|
528
|
-
loader: ({ params, search }) => {
|
|
543
|
+
loader: ( { params, search } ) => {
|
|
529
544
|
// Data preloading
|
|
530
545
|
},
|
|
531
|
-
canvas: ({ params, search }) => {
|
|
546
|
+
canvas: ( { params, search } ) => {
|
|
532
547
|
// Return CanvasData to use default canvas (editor)
|
|
533
548
|
return {
|
|
534
549
|
postType: params.type,
|
|
@@ -542,7 +557,7 @@ export const route = {
|
|
|
542
557
|
|
|
543
558
|
// Return undefined to show no canvas
|
|
544
559
|
// return undefined;
|
|
545
|
-
}
|
|
560
|
+
},
|
|
546
561
|
};
|
|
547
562
|
```
|
|
548
563
|
|
|
@@ -550,10 +565,10 @@ export const route = {
|
|
|
550
565
|
|
|
551
566
|
The build system generates:
|
|
552
567
|
|
|
553
|
-
-
|
|
554
|
-
-
|
|
555
|
-
-
|
|
556
|
-
-
|
|
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
|
|
557
572
|
|
|
558
573
|
The boot package in Gutenberg will automatically use these routes and make them available.
|
|
559
574
|
|
|
@@ -580,13 +595,13 @@ widgets/
|
|
|
580
595
|
|
|
581
596
|
Widgets use a dual-entry pattern, similar in spirit to how blocks split metadata between `block.json` and `edit.js`:
|
|
582
597
|
|
|
583
|
-
| Concern
|
|
584
|
-
|
|
|
585
|
-
| Identity (`name`)
|
|
586
|
-
| Translatable metadata (`title`, `description`, `help`, `keywords`) | `widget.json`
|
|
587
|
-
| Framing (`category`, `presentation`)
|
|
588
|
-
| Attribute schema (types, options, labels, `relevance`)
|
|
589
|
-
| Icon and `example`
|
|
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.ts` | needs TypeScript coupling with `render` props; labels are translated with `__()` |
|
|
604
|
+
| Icon and `example` | `widget.ts` | runtime values co-located with the attribute schema |
|
|
590
605
|
|
|
591
606
|
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`.
|
|
592
607
|
|
|
@@ -607,14 +622,14 @@ Rule of thumb: anything the host needs before loading a widget's module — iden
|
|
|
607
622
|
|
|
608
623
|
**Fields:**
|
|
609
624
|
|
|
610
|
-
-
|
|
611
|
-
-
|
|
612
|
-
-
|
|
613
|
-
-
|
|
614
|
-
-
|
|
615
|
-
-
|
|
616
|
-
-
|
|
617
|
-
-
|
|
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
|
+
- **`category`** (optional): Grouping category for filtering
|
|
631
|
+
- **`presentation`** (optional): Rendering intent (`framed`, `content-bleed`, `full-bleed`)
|
|
632
|
+
- **`textdomain`** (optional): Gettext text domain for translating `title`, `description`, `help`, and `keywords`
|
|
618
633
|
|
|
619
634
|
### `widget.ts` — runtime schema
|
|
620
635
|
|
|
@@ -657,7 +672,7 @@ const widget = {
|
|
|
657
672
|
export default widget;
|
|
658
673
|
```
|
|
659
674
|
|
|
660
|
-
Each attribute entry is a DataViews `Field` plus an optional `relevance` hint (`'high' | 'low'`). The widget declares importance; the host decides where to expose each field. When absent, `relevance` defaults to `'low'`.
|
|
675
|
+
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'`.
|
|
661
676
|
|
|
662
677
|
The same `HelloWorldAttributes` type is consumed by `render.tsx`, giving the author a single source of truth for the data contract.
|
|
663
678
|
|
|
@@ -677,7 +692,9 @@ export default function HelloWorld( { attributes }: HelloWorldRenderProps ) {
|
|
|
677
692
|
return (
|
|
678
693
|
<div>
|
|
679
694
|
Hello, { attributes.greeting ?? 'World' }!
|
|
680
|
-
{ attributes.showDate &&
|
|
695
|
+
{ attributes.showDate && (
|
|
696
|
+
<time>{ new Date().toLocaleDateString() }</time>
|
|
697
|
+
) }
|
|
681
698
|
</div>
|
|
682
699
|
);
|
|
683
700
|
}
|
|
@@ -689,12 +706,12 @@ All non-JSON entries are optional. The build system checks for files with extens
|
|
|
689
706
|
|
|
690
707
|
The build system generates:
|
|
691
708
|
|
|
692
|
-
-
|
|
693
|
-
-
|
|
694
|
-
-
|
|
695
|
-
-
|
|
696
|
-
-
|
|
697
|
-
-
|
|
709
|
+
- `build/widgets/{widget-name}/render.min.js` + `render.js` — Bundled UI component (ESM)
|
|
710
|
+
- `build/widgets/{widget-name}/render.min.asset.php` — Asset metadata for render module
|
|
711
|
+
- `build/widgets/{widget-name}/widget.min.js` + `widget.js` — Bundled metadata (ESM)
|
|
712
|
+
- `build/widgets/{widget-name}/widget.min.asset.php` — Asset metadata for widget module
|
|
713
|
+
- `build/widgets/registry.php` — Widget registry data
|
|
714
|
+
- `build/widgets.php` — Script module registration logic
|
|
698
715
|
|
|
699
716
|
### PHP Registration
|
|
700
717
|
|
|
@@ -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
|
+
}
|