# @wordpress/build
Build tool for WordPress plugins.
## Description
`@wordpress/build` is an opinionated build system designed for WordPress plugins. It provides:
- **Transpilation**: Converts TypeScript/JSX source code to both CommonJS (`build/`) and ESM (`build-module/`) formats using Rolldown
- **Style Compilation**: Processes SCSS files and CSS modules, generating LTR and RTL versions
- **Bundling**: Creates browser-ready bundles for WordPress scripts and modules
- **PHP Generation**: Automatically generates PHP registration files for scripts, modules, and styles
- **Watch Mode**: Incremental rebuilds during development
## Installation
```bash
npm install @wordpress/build --save-dev
```
## Usage
### Production Build
```bash
wp-build
```
or via npm script:
```json
{
"scripts": {
"build": "wp-build"
}
}
```
### Development Mode (Watch)
```bash
wp-build --watch
```
or via npm script:
```json
{
"scripts": {
"dev": "wp-build --watch"
}
}
```
### Build Concurrency
By default, `wp-build` limits concurrent Rolldown build jobs to half of the available CPU parallelism, rounded up. This prevents large projects from starting every package, route, and widget bundle at once.
### Testing Generated Styles
Generated CSS output skips automatic style injection in Node-based environments
when `NODE_ENV` is `test` and `process` is defined. Browsers have no `process`,
so they always receive styles. Node-based DOM implementations such as jsdom do
not reliably support modern CSS features, so tests that need actual styles in
the DOM should run in a browser environment.
## Browser support
JavaScript and CSS builds use [Browserslist](https://github.com/browserslist/browserslist)
when specifying build targets. You can either [provide your own](https://github.com/browserslist/browserslist#config-file),
or the default [`@wordpress/browserslist-config`](https://www.npmjs.com/package/@wordpress/browserslist-config)
will be used to follow [WordPress browser support](https://make.wordpress.org/core/handbook/best-practices/browser-support/).
## Package Configuration
Configure your `package.json` with the following optional fields:
### `wpScript`
Controls whether the package is exposed as a bundled WordPress script/module and accessible via the configured global variable.
- **`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).
- **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.
```json
{
"wpScript": true
}
```
For more details on when to omit or set this to `false`, see the [package guidelines](../README.md#when-to-omit-or-set-wpscript-to-false).
### `wpScriptModuleExports`
Define script module entry points:
```json
{
"wpScriptModuleExports": {
"./interactivity": "./build-module/interactivity/index.js"
}
}
```
### `wpScriptDefaultExport`
Handle default export wrapping:
```json
{
"wpScriptDefaultExport": true
}
```
### `wpScriptExtraDependencies`
Additional script dependencies:
```json
{
"wpScriptExtraDependencies": [ "wp-polyfill" ]
}
```
### `wpStyleEntryPoints`
Custom SCSS entry point patterns:
```json
{
"wpStyleEntryPoints": {
"style": "src/style.scss"
}
}
```
### `wpCopyFiles`
Files to copy to `build/scripts/{package}/`, with optional PHP transformations:
```json
{
"wpCopyFiles": {
"files": [ "src/**/*.php", "src/*/block.json" ],
"transforms": {
"php": {
"functionPrefix": "gutenberg_",
"classSuffix": "_Gutenberg",
"prefixFunctions": [ "wp_apply_colors_support" ],
"suffixClasses": [ "WP_Navigation_Block_Renderer" ],
"addActionPriority": 20,
"flattenIndexFiles": true
}
}
}
}
```
`files` are glob patterns relative to the package, and each file keeps its path relative to `src/`. The PHP transforms apply only to `.php` files, and are skipped when `IS_WORDPRESS_CORE` is set:
- `functionPrefix`: Prefix for the functions a file defines, replacing a `wp_` prefix, and for their uses.
- `prefixFunctions`: Other functions to prefix with `functionPrefix`.
- `classSuffix` and `suffixClasses`: Suffix to add to the given classes.
- `addActionPriority`: Priority to add to the `init` action registering a block.
- `filenameSuffix`: Suffix for the copied file's name, e.g. `-gutenberg` for `class-wp-style-engine-gutenberg.php`.
- `flattenIndexFiles`: Copy `{name}/index.php` to `{name}.php`.
In watch mode, a change to one of these PHP files copies only that file to the build, without rebuilding the package. Adding or removing a file rebuilds the package, but the build's copy of a removed file is not deleted.
### `wpWorkers`
Worker bundle definitions for packages that need self-contained Web Worker files.
Workers are bundled with all dependencies included and can be loaded via Blob URLs.
**String shorthand** — entry path only:
```json
{
"wpWorkers": {
"./worker": "./src/worker.ts"
}
}
```
**Object format** — entry path with module resolve redirects:
```json
{
"wpWorkers": {
"./worker": {
"entry": "./src/worker.ts",
"resolve": {
"vips-es6.js": "vips.js"
}
}
}
}
```
The `resolve` map redirects module loads during bundling. Keys are filename
patterns to match; values are replacement filenames in the same directory.
This is useful when a dependency's ES module entry point uses `import.meta.url`,
which fails in Blob URL Worker contexts. By redirecting to an alternative
entry point (e.g., a CommonJS version), the issue is avoided.
## Root Configuration
Configure your root `package.json` with a `wpPlugin` object to control global namespace and externalization behavior:
### `wpPlugin.name`
Name used to prefix generated PHP functions. Must follow function name rules in PHP, i.e. valid name starts with a letter or underscore, followed by any number of letters, numbers, or underscores.
```json
{
"wpPlugin": {
"name": "myPlugin"
}
}
```
### `wpPlugin.scriptGlobal`
The global variable name for your packages (e.g., `"wp"`, `"myPlugin"`). Set to `false` to disable global exposure:
```json
{
"wpPlugin": {
"scriptGlobal": "myPlugin"
}
}
```
### `wpPlugin.packageNamespace`
The package scope to match for global exposure (without `@` prefix). Only packages matching `@{packageNamespace}/*` will expose globals:
```json
{
"wpPlugin": {
"scriptGlobal": "myPlugin",
"packageNamespace": "my-plugin"
}
}
```
### `wpPlugin.handlePrefix`
The prefix used for WordPress script handles in `.asset.php` files (e.g., `wp-data`, `my-plugin-editor`). Defaults to `packageNamespace`:
```json
{
"wpPlugin": {
"scriptGlobal": "myPlugin",
"packageNamespace": "my-plugin",
"handlePrefix": "mp"
}
}
```
With this configuration:
- `@my-plugin/editor` → `window.myPlugin.editor` with handle `mp-editor`
- `@my-plugin/data` → `window.myPlugin.data` with handle `mp-data`
### `wpPlugin.externalNamespaces`
Additional package namespaces to externalize (consume as externals, not expose). Each namespace must be an object with `global` and optional `handlePrefix`:
```json
{
"wpPlugin": {
"externalNamespaces": {
"woo": {
"global": "woo",
"handlePrefix": "woocommerce"
},
"acme": {
"global": "acme",
"handlePrefix": "acme-plugin"
}
}
}
}
```
This allows your packages to consume third-party dependencies as externals:
- `import { Cart } from '@woo/cart'` → `window.woo.cart` with handle `woocommerce-cart`
- `import { Button } from '@acme/ui'` → `window.acme.ui` with handle `acme-plugin-ui`
- Dependencies are tracked in `.asset.php` files
If `handlePrefix` is omitted, it defaults to the namespace key (e.g., `"woo"` → `woo-cart`).
### `wpPlugin.pages` (Experimental)
Define admin pages that support routes. Each page gets generated PHP functions for route registration and can be extended by other plugins.
Pages can be defined as simple strings or as objects with initialization modules:
```json
{
"wpPlugin": {
"pages": [
"my-admin-page",
{
"id": "my-other-page",
"init": [ "@my-plugin/my-page-init" ]
}
]
}
}
```
**Page Configuration:**
- **String format**: `"my-admin-page"` - Simple page with no init modules
- **Object format**: `{ "id": "page-slug", "init": ["@scope/package"], "capability": "edit_theme_options", "experimental": true }` - Page with optional init modules
- **`id`** (required): The page slug used in WordPress admin URLs
- **`init`** (optional): Array of script module IDs to execute during page initialization
- **`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.
- **`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.
**Generated Files:**
This generates two page modes:
- `build/pages/my-admin-page/page.php` - Full-page mode (takes over entire admin screen with custom sidebar)
- `build/pages/my-admin-page/page-wp-admin.php` - WP-Admin mode (integrates within standard wp-admin interface)
- `build/pages.php` - Loader for all pages
Each mode provides route/menu registration functions and a render callback. Routes are automatically registered for both modes.
**Boot module:**
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`.
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.
**Registering a menu item for WP-Admin mode:**
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:
```php
add_submenu_page(
'themes.php', // Parent menu
__( 'My Page', 'my-plugin' ), // Page title
__( 'My Page', 'my-plugin' ), // Menu title
'edit_theme_options', // Capability
'my-admin-page-wp-admin', // Menu slug (simple)
'my_plugin_my_admin_page_wp_admin_render_page' // Callback from generated PHP (prefixed)
);
```
Note: The callback function name is prefixed with your plugin name (from `wpPlugin.name` in root `package.json`). For example, if your plugin name is `my-plugin`, the function will be `my_plugin_my_admin_page_wp_admin_render_page`.
The page slug is `my-admin-page-wp-admin` (your page ID + `-wp-admin`). WordPress routes all requests to this callback, and the JavaScript router handles internal navigation.
**Deep linking with the `p` query parameter:**
Users and extensions can link directly to specific routes using the `p` query parameter:
```php
// Link to a specific route
$url = admin_url( 'admin.php?page=my-admin-page-wp-admin&p=' . urlencode( '/settings' ) );
```
When the page loads, the JavaScript boot system reads the `p` parameter and navigates to that route automatically.
**Registering a menu item for full-page mode:**
Full-page mode takes over the entire admin screen with a custom sidebar:
```php
add_menu_page( 'Title', 'Menu', 'capability', 'my-admin-page', 'my_plugin_my_admin_page_render_page', 'icon', 20 );
```
**Init Modules:**
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:
- Adding icons to menu items (icons can't be passed from PHP)
- Registering command palette entries
**Creating an Init Module:**
In `packages/my-page-init/package.json`:
```json
{
"name": "@my-plugin/my-page-init",
"wpScriptModuleExports": "./build-module/index.js",
"dependencies": {
"@wordpress/boot": "file:../boot",
"@wordpress/data": "file:../data",
"@wordpress/icons": "file:../icons"
}
}
```
In `packages/my-page-init/src/index.ts`:
```typescript
import { home, styles } from '@wordpress/icons';
import { dispatch } from '@wordpress/data';
import { store as bootStore } from '@wordpress/boot';
/**
* Initialize page - this function is mandatory.
* All init modules must export an 'init' function.
*/
export async function init() {
// Add icons to menu items
dispatch( bootStore ).updateMenuItem( 'home', { icon: home } );
dispatch( bootStore ).updateMenuItem( 'styles', { icon: styles } );
}
```
The `init()` function is **mandatory** - all init modules must export this named function. Init modules are loaded as static dependencies and executed sequentially after the boot system registers menu items and routes, before the app renders.
### Example: WordPress Core (Gutenberg)
```json
{
"wpPlugin": {
"scriptGlobal": "wp",
"packageNamespace": "wordpress"
}
}
```
This configuration:
- Packages like `@wordpress/data` expose `window.wp.data`
- Packages like `@wordpress/block-editor` expose `window.wp.blockEditor`
- All packages can consume `@wordpress/*` as externals
### Example: Third-Party Plugin
```json
{
"wpPlugin": {
"name": "acme",
"scriptGlobal": "acme",
"packageNamespace": "acme"
}
}
```
This configuration:
- Packages like `@acme/editor` expose `window.acme.editor`
- Packages like `@acme/data` expose `window.acme.data`
- All packages can still consume `@wordpress/*` → `window.wp.*`
- All packages can still consume vendors (react, lodash) → `window.React`, `window.lodash`
### Behavior
- **Packages with `wpScript: true` matching the namespace**: Bundled with global exposure
- **Packages with `wpScript: true` not matching the namespace**: Bundled without global exposure
- **Dependencies**: `@wordpress/*` packages are always externalized to `wp.*` globals
- **Vendors**: React, lodash, jQuery, moment are always externalized to their standard globals
- **Asset files**: `.asset.php` files are always generated for WordPress dependency management
## SVG imports
SVG files can be imported from JavaScript. The file is inlined into the JavaScript and the import resolves to a `data:` URI:
```js
import logo from './logo.svg';
const Example = () => ;
```
Because the SVG travels with the JavaScript, it works in bundles and in transpiled packages (`build/`, `build-module/`) without a public path or an extra file to ship. Other image formats are not supported yet.
## Output Structure
The built tool generates several files in the `build/` directory, but the primary output is the PHP registration file.
Make sure to include the generated PHP file in your plugin file.
```php
require_once plugin_dir_path( __FILE__ ) . 'build/build.php';
```
## Routes (Experimental)
Routes provide a file-based routing system for WordPress admin pages. Each route must be associated with a page defined in `wpPlugin.pages` (see above). Create a `routes/` directory at your repository root with subdirectories for each route.
### Route Structure
```
routes/
home/
package.json # Route configuration
stage.tsx # Main content component
inspector.tsx # Optional sidebar component
canvas.tsx # Optional custom canvas component
route.tsx # Optional lifecycle hooks (beforeLoad, loader, canvas, title)
```
### Route Configuration
In `routes/{route-name}/package.json`:
```json
{
"route": {
"path": "/",
"page": "my-admin-page"
}
}
```
For routes that should appear on multiple pages:
```json
{
"route": {
"path": "/settings",
"page": [ "my-admin-page", "other-page" ]
}
}
```
The `page` field can be either:
- **String**: Route belongs to a single page
- **Array**: Route appears on multiple pages (the build system will register the route for each page)
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.
Multi-page routes are useful for shared functionality across different admin pages, such as settings routes accessible from both a main page and a dedicated settings page.
Routes inherit their experimental status from their pages: a route is excluded from WordPress Core builds only when every page it belongs to is marked `"experimental": true` in `wpPlugin.pages`.
### Components
**stage.tsx** - Main content (required):
```tsx
export const stage = () =>