@wordpress/build 0.26.0 → 0.28.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/CHANGELOG.md +40 -0
- package/README.md +58 -31
- package/eslint-overrides.cjs +1 -1
- package/lib/build-concurrency.mjs +87 -0
- package/lib/build.mjs +686 -555
- package/lib/compile-inline-style.mjs +48 -12
- package/lib/copy-files.mjs +165 -0
- package/lib/package-utils.mjs +16 -2
- package/lib/php-generator.mjs +5 -0
- package/lib/route-utils.mjs +1 -1
- package/lib/sass-compiler.mjs +41 -0
- package/lib/widget-utils.mjs +7 -1
- package/lib/wordpress-externals-plugin.mjs +332 -313
- package/lib/worker-build.mjs +85 -75
- package/package.json +11 -11
- package/templates/build.php.template +4 -0
- package/templates/constants.php.template +4 -0
- package/templates/module-registration.php.template +4 -0
- package/templates/module-registry.php.template +4 -0
- package/templates/page-wp-admin.php.template +67 -54
- package/templates/page.php.template +27 -21
- package/templates/pages.php.template +4 -0
- package/templates/route-registry.php.template +4 -0
- package/templates/routes-registration.php.template +4 -0
- package/templates/script-registration.php.template +4 -0
- package/templates/script-registry.php.template +4 -0
- package/templates/style-registration.php.template +4 -0
- package/templates/style-registry.php.template +4 -0
- package/templates/widget-registration.php.template +4 -0
- package/templates/widget-registry.php.template +4 -0
package/CHANGELOG.md
CHANGED
|
@@ -2,6 +2,45 @@
|
|
|
2
2
|
|
|
3
3
|
## Unreleased
|
|
4
4
|
|
|
5
|
+
## 0.28.0 (2026-11-18)
|
|
6
|
+
|
|
7
|
+
### Enhancements
|
|
8
|
+
|
|
9
|
+
- In watch mode, copy a changed PHP file from `wpCopyFiles` to the build on its own instead of rebuilding its whole package, so the change reaches the build in milliseconds rather than seconds. Adding or removing a PHP file still rebuilds the package ([#86271](https://github.com/WordPress/gutenberg/pull/86271)).
|
|
10
|
+
|
|
11
|
+
### Bug Fixes
|
|
12
|
+
|
|
13
|
+
- Exit the generated PHP files when they are accessed directly, print the inline styles and scripts of generated pages through the WordPress enqueue functions, and escape the message shown when a user cannot access a page. Plugins shipping the build now pass the WordPress.org Plugin Directory review and Plugin Check ([#86699](https://github.com/WordPress/gutenberg/pull/86699)).
|
|
14
|
+
- Require PostCSS `^8.5.33` for CSS parser fixes and source-map file-loading protections ([#86899](https://github.com/WordPress/gutenberg/pull/86899)).
|
|
15
|
+
- Let a full-page screen show the admin bar again. The page template's rule that hides it now loads after the page's own styles, so it has zero specificity to keep it overridable ([#86992](https://github.com/WordPress/gutenberg/pull/86992)).
|
|
16
|
+
- Load a `wp-admin` page's scripts and styles only on its own screen, not on a Core screen whose screen ID matches the page slug. The `dashboard` page no longer hides the content of the Core Dashboard (`index.php`) ([#86995](https://github.com/WordPress/gutenberg/pull/86995)).
|
|
17
|
+
|
|
18
|
+
### Internal
|
|
19
|
+
|
|
20
|
+
- Widen the `@wordpress/theme` peer dependency range to include 5.x ([#86720](https://github.com/WordPress/gutenberg/pull/86720)).
|
|
21
|
+
- Update the Vitest development dependency to 5.1.0 ([#86509](https://github.com/WordPress/gutenberg/pull/86509)).
|
|
22
|
+
|
|
23
|
+
## 0.27.0 (2026-11-04)
|
|
24
|
+
|
|
25
|
+
### Enhancements
|
|
26
|
+
|
|
27
|
+
- Copy `example` from `widget.json` into `build/widgets/registry.php`, next to `attributes`, so a widget's example attributes reach the server registry without a metadata module ([#85240](https://github.com/WordPress/gutenberg/pull/85240)).
|
|
28
|
+
- Limit concurrent `esbuild` build jobs to half of the available CPU parallelism ([#79889](https://github.com/WordPress/gutenberg/pull/79889)).
|
|
29
|
+
|
|
30
|
+
### Bug Fixes
|
|
31
|
+
|
|
32
|
+
- Derive CSS module class names from the stylesheet's CSS as well as its path, so two builds of a package share class names only where their CSS is identical. This stops an older bundled copy of a package from styling a newer copy's elements. All CSS module class names change once with this release ([#85900](https://github.com/WordPress/gutenberg/pull/85900)).
|
|
33
|
+
|
|
34
|
+
### Internal
|
|
35
|
+
|
|
36
|
+
- Pass `false` instead of an empty string as `$src` in `wp_register_script()` in page templates ([#86106](https://github.com/WordPress/gutenberg/pull/86106)).
|
|
37
|
+
- Update the Vitest development dependency to 5.0.5 ([#86173](https://github.com/WordPress/gutenberg/pull/86173)).
|
|
38
|
+
- Bundle and transpile with Rolldown instead of esbuild ([#85476](https://github.com/WordPress/gutenberg/pull/85476)).
|
|
39
|
+
- Load design token fallback plugins from the new `@wordpress/theme-build-tools` dependency instead of the `@wordpress/theme` entry points ([#83568](https://github.com/WordPress/gutenberg/pull/83568)).
|
|
40
|
+
- Declare `@wordpress/theme` as an optional peer dependency again, so the fallback plugins use the version the consumer installs ([#85388](https://github.com/WordPress/gutenberg/pull/85388)).
|
|
41
|
+
- Point the page templates' preload comments at the TypeScript `entities.ts` in `@wordpress/core-data` ([#85070](https://github.com/WordPress/gutenberg/pull/85070)).
|
|
42
|
+
- Note beside `applyTemplateReplacements()` that Gutenberg's PHPStan analysis of the PHP templates keeps a sample value for each placeholder, which must be updated along with it ([#66693](https://github.com/WordPress/gutenberg/pull/66693)).
|
|
43
|
+
|
|
5
44
|
## 0.26.0 (2026-10-20)
|
|
6
45
|
|
|
7
46
|
### Breaking Changes
|
|
@@ -16,6 +55,7 @@
|
|
|
16
55
|
|
|
17
56
|
### Enhancements
|
|
18
57
|
|
|
58
|
+
- Expose a script's exports on its global as read-only data properties, holding the same values, instead of esbuild's getters, which forwarded to the getters of the module's own exports. Every use of a script's export by another script went through them, which was costly while the page loads ([#85180](https://github.com/WordPress/gutenberg/pull/85180)).
|
|
19
59
|
- Resolve development bundles with the `development` and `module` export conditions, enabling runtime diagnostics such as unsupported Emotion component selectors ([#84707](https://github.com/WordPress/gutenberg/pull/84707)).
|
|
20
60
|
|
|
21
61
|
### Bug Fixes
|
package/README.md
CHANGED
|
@@ -6,7 +6,7 @@ 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
|
|
9
|
+
- **Transpilation**: Converts TypeScript/JSX source code to both CommonJS (`build/`) and ESM (`build-module/`) formats using Rolldown
|
|
10
10
|
- **Style Compilation**: Processes SCSS files and CSS modules, generating LTR and RTL versions
|
|
11
11
|
- **Bundling**: Creates browser-ready bundles for WordPress scripts and modules
|
|
12
12
|
- **PHP Generation**: Automatically generates PHP registration files for scripts, modules, and styles
|
|
@@ -52,6 +52,10 @@ or via npm script:
|
|
|
52
52
|
}
|
|
53
53
|
```
|
|
54
54
|
|
|
55
|
+
### Build Concurrency
|
|
56
|
+
|
|
57
|
+
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.
|
|
58
|
+
|
|
55
59
|
### Testing Generated Styles
|
|
56
60
|
|
|
57
61
|
Generated CSS output skips automatic style injection in Node-based environments
|
|
@@ -133,20 +137,37 @@ Custom SCSS entry point patterns:
|
|
|
133
137
|
|
|
134
138
|
### `wpCopyFiles`
|
|
135
139
|
|
|
136
|
-
Files to copy with optional PHP transformations:
|
|
140
|
+
Files to copy to `build/scripts/{package}/`, with optional PHP transformations:
|
|
137
141
|
|
|
138
142
|
```json
|
|
139
143
|
{
|
|
140
|
-
"wpCopyFiles":
|
|
141
|
-
|
|
142
|
-
|
|
143
|
-
"
|
|
144
|
-
|
|
144
|
+
"wpCopyFiles": {
|
|
145
|
+
"files": [ "src/**/*.php", "src/*/block.json" ],
|
|
146
|
+
"transforms": {
|
|
147
|
+
"php": {
|
|
148
|
+
"functionPrefix": "gutenberg_",
|
|
149
|
+
"classSuffix": "_Gutenberg",
|
|
150
|
+
"prefixFunctions": [ "wp_apply_colors_support" ],
|
|
151
|
+
"suffixClasses": [ "WP_Navigation_Block_Renderer" ],
|
|
152
|
+
"addActionPriority": 20,
|
|
153
|
+
"flattenIndexFiles": true
|
|
154
|
+
}
|
|
145
155
|
}
|
|
146
|
-
|
|
156
|
+
}
|
|
147
157
|
}
|
|
148
158
|
```
|
|
149
159
|
|
|
160
|
+
`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:
|
|
161
|
+
|
|
162
|
+
- `functionPrefix`: Prefix for the functions a file defines, replacing a `wp_` prefix, and for their uses.
|
|
163
|
+
- `prefixFunctions`: Other functions to prefix with `functionPrefix`.
|
|
164
|
+
- `classSuffix` and `suffixClasses`: Suffix to add to the given classes.
|
|
165
|
+
- `addActionPriority`: Priority to add to the `init` action registering a block.
|
|
166
|
+
- `filenameSuffix`: Suffix for the copied file's name, e.g. `-gutenberg` for `class-wp-style-engine-gutenberg.php`.
|
|
167
|
+
- `flattenIndexFiles`: Copy `{name}/index.php` to `{name}.php`.
|
|
168
|
+
|
|
169
|
+
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.
|
|
170
|
+
|
|
150
171
|
### `wpWorkers`
|
|
151
172
|
|
|
152
173
|
Worker bundle definitions for packages that need self-contained Web Worker files.
|
|
@@ -476,7 +497,7 @@ routes/
|
|
|
476
497
|
stage.tsx # Main content component
|
|
477
498
|
inspector.tsx # Optional sidebar component
|
|
478
499
|
canvas.tsx # Optional custom canvas component
|
|
479
|
-
route.tsx # Optional lifecycle hooks (beforeLoad, loader, canvas)
|
|
500
|
+
route.tsx # Optional lifecycle hooks (beforeLoad, loader, canvas, title)
|
|
480
501
|
```
|
|
481
502
|
|
|
482
503
|
### Route Configuration
|
|
@@ -546,6 +567,7 @@ Export a `route` object with optional hooks. Each hook receives `{ params, searc
|
|
|
546
567
|
- `CanvasData` (`{ postType, postId, isPreview?, editLink? }`) → default WordPress editor canvas
|
|
547
568
|
- `null` → custom `canvas.tsx` component (if provided)
|
|
548
569
|
- `undefined` or omitted → no canvas
|
|
570
|
+
- **`title`** — Runs in parallel with `loader`. Returns the page's title as a string, or a promise of a string. After each navigation within the application, the title is announced to screen readers and used to update the document title. Every route should define it: without it, screen reader users do not hear that the page changed, and the browser tab keeps the title of the previous page.
|
|
549
571
|
|
|
550
572
|
```tsx
|
|
551
573
|
export const route = {
|
|
@@ -570,6 +592,10 @@ export const route = {
|
|
|
570
592
|
// Return undefined to show no canvas
|
|
571
593
|
// return undefined;
|
|
572
594
|
},
|
|
595
|
+
title: ( { params, search } ) => {
|
|
596
|
+
// Return the page title
|
|
597
|
+
return __( 'Settings', 'my-plugin' );
|
|
598
|
+
},
|
|
573
599
|
};
|
|
574
600
|
```
|
|
575
601
|
|
|
@@ -596,11 +622,11 @@ Widgets provide a file-based discovery system for building self-contained UI com
|
|
|
596
622
|
```text
|
|
597
623
|
widgets/
|
|
598
624
|
hello-world/
|
|
599
|
-
widget.json
|
|
600
|
-
widget.ts
|
|
601
|
-
render.tsx
|
|
602
|
-
|
|
603
|
-
package.json
|
|
625
|
+
widget.json # Static discovery metadata (required)
|
|
626
|
+
widget.ts # Runtime values entry point (optional)
|
|
627
|
+
render.tsx # UI component entry point (optional)
|
|
628
|
+
style.module.css # Optional styles, imported from render.tsx and injected at runtime
|
|
629
|
+
package.json # Optional npm dependencies manifest
|
|
604
630
|
```
|
|
605
631
|
|
|
606
632
|
### Why two entries?
|
|
@@ -609,13 +635,15 @@ Widgets use a dual-entry pattern, similar in spirit to how blocks split metadata
|
|
|
609
635
|
|
|
610
636
|
| Concern | Lives in | Reason |
|
|
611
637
|
| ------------------------------------------------------------------ | ----------------- | -------------------------------------------------------------------------------------------------------------------------------- |
|
|
612
|
-
| Identity (`name`) |
|
|
638
|
+
| Identity (`name`) | `widget.json` | server needs it to register; the record carries it to the client |
|
|
613
639
|
| Translatable metadata (`title`, `description`, `help`, `keywords`) | `widget.json` | translated server-side via `textdomain`, so the host receives localized strings without a JS runtime |
|
|
614
640
|
| Framing (`category`, `presentation`) | `widget.json` | plain JSON the host reads without a JS runtime |
|
|
641
|
+
| Icon and actions (`icon`, `actions`) | `widget.json` | plain JSON; the icon is a registered name resolved client-side, action labels are translated server-side via `textdomain` |
|
|
615
642
|
| 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 |
|
|
616
|
-
| `example
|
|
643
|
+
| `example` attributes | `widget.json` | plain JSON; a `widget.ts` `example` wins when its values need code |
|
|
644
|
+
| Attribute props that need code | `widget.ts` | runtime values |
|
|
617
645
|
|
|
618
|
-
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 (
|
|
646
|
+
Rule of thumb: anything the host needs before loading a widget's module — identity, translatable copy, framing, attribute schema, example attributes — goes in `widget.json`, which the build forwards to `build/widgets/registry.php` and exposes through the REST API. Anything that needs code (an attribute's `Edit` component or custom validator, an `example` with runtime values) goes in `widget.ts`.
|
|
619
647
|
|
|
620
648
|
### `widget.json` — static discovery metadata
|
|
621
649
|
|
|
@@ -627,6 +655,9 @@ Rule of thumb: anything the host needs before loading a widget's module — iden
|
|
|
627
655
|
"help": {
|
|
628
656
|
"content": "A short note for compact surfaces such as tooltips."
|
|
629
657
|
},
|
|
658
|
+
"example": {
|
|
659
|
+
"attributes": { "greeting": "World", "showDate": true }
|
|
660
|
+
},
|
|
630
661
|
"category": "demo",
|
|
631
662
|
"textdomain": "my-plugin"
|
|
632
663
|
}
|
|
@@ -638,15 +669,18 @@ Rule of thumb: anything the host needs before loading a widget's module — iden
|
|
|
638
669
|
- **`title`** (optional): Human-readable title. Translated server-side using `textdomain`.
|
|
639
670
|
- **`description`** (optional): Short description. Translated server-side using `textdomain`.
|
|
640
671
|
- **`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`.
|
|
672
|
+
- **`icon`** (optional): Registered icon name (`collection/icon-name`), resolved client-side through the site's icon registry. A name that does not match the shape is dropped at registration.
|
|
673
|
+
- **`actions`** (optional): Declarative actions, each with `id`, `label` and `href`, plus optional `icon`, `relevance` (`high`, `medium`, `low`), `download` and `openInNewTab`. Labels are translated server-side using `textdomain`. A relative `href` naming a file in the widget directory resolves to its URL at registration; `data:` and `javascript:` hrefs are rejected.
|
|
641
674
|
- **`keywords`** (optional): Search aliases. Translated server-side using `textdomain`.
|
|
642
675
|
- **`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`.
|
|
676
|
+
- **`example`** (optional): Structured example, `{ "attributes": { … } }`: the attributes hosts preview the widget with, and apply to a new instance created without any. JSON values only; an `example` in `widget.ts` wins over it.
|
|
643
677
|
- **`category`** (optional): Grouping category for filtering
|
|
644
678
|
- **`presentation`** (optional): Rendering intent (`framed`, `content-bleed`, `full-bleed`)
|
|
645
|
-
- **`textdomain`** (optional): Gettext text domain for translating `title`, `description`, `help`, `keywords`, and attribute labels
|
|
679
|
+
- **`textdomain`** (optional): Gettext text domain for translating `title`, `description`, `help`, `keywords`, action labels and attribute labels
|
|
646
680
|
|
|
647
681
|
### `widget.ts` — runtime values
|
|
648
682
|
|
|
649
|
-
Optional. Exports a default object with what needs code:
|
|
683
|
+
Optional. Exports a default object with what needs code: attribute entries that add an `Edit` component or a custom validator to a `widget.json` attribute with the same `id`, and an `example` whose values only exist at runtime, which wins over the `widget.json` one. The build system injects the `render_module` handle at registration time, so authors don't need to declare it.
|
|
650
684
|
|
|
651
685
|
```ts
|
|
652
686
|
import type { WidgetAttributeField } from '@wordpress/widget-primitives';
|
|
@@ -658,26 +692,19 @@ type HelloWorldAttributes = {
|
|
|
658
692
|
};
|
|
659
693
|
|
|
660
694
|
const widget = {
|
|
661
|
-
name: 'my-plugin/hello-world',
|
|
662
695
|
attributes: [ { id: 'greeting', Edit: GreetingControl } ] satisfies Partial<
|
|
663
696
|
WidgetAttributeField< HelloWorldAttributes >
|
|
664
697
|
>[],
|
|
665
|
-
example: {
|
|
666
|
-
attributes: {
|
|
667
|
-
greeting: 'World',
|
|
668
|
-
showDate: true,
|
|
669
|
-
},
|
|
670
|
-
},
|
|
671
698
|
};
|
|
672
699
|
|
|
673
700
|
export default widget;
|
|
674
701
|
```
|
|
675
702
|
|
|
676
|
-
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`.
|
|
703
|
+
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 and example are plain data needs no `widget.ts`.
|
|
677
704
|
|
|
678
705
|
### `render.tsx` — UI component
|
|
679
706
|
|
|
680
|
-
The render entry receives `attributes` matching the schema declared in `widget.json`.
|
|
707
|
+
The render entry receives `attributes` matching the schema declared in `widget.json` and, when the host allows edits, `setAttributes`. Styles are imported from the entry, as a CSS Module (`style.module.css`) or a plain `.css` / `.scss` file, and injected at runtime.
|
|
681
708
|
|
|
682
709
|
```tsx
|
|
683
710
|
interface HelloWorldRenderProps {
|
|
@@ -699,15 +726,15 @@ export default function HelloWorld( { attributes }: HelloWorldRenderProps ) {
|
|
|
699
726
|
}
|
|
700
727
|
```
|
|
701
728
|
|
|
702
|
-
All non-JSON entries are optional.
|
|
729
|
+
All non-JSON entries are optional. Either entry may use any of `.js`, `.jsx`, `.mjs`, `.cjs`, `.ts`, `.tsx`, `.mts` or `.cts`; ship one file per entry.
|
|
703
730
|
|
|
704
731
|
### Widgets Build Output
|
|
705
732
|
|
|
706
733
|
The build system generates:
|
|
707
734
|
|
|
708
|
-
- `build/widgets/{widget-name}/render.min.js` + `render.js` — Bundled UI component (ESM)
|
|
735
|
+
- `build/widgets/{widget-name}/render.min.js` + `render.js` — Bundled UI component (ESM), when the widget ships a `render.*` entry
|
|
709
736
|
- `build/widgets/{widget-name}/render.min.asset.php` — Asset metadata for render module
|
|
710
|
-
- `build/widgets/{widget-name}/widget.min.js` + `widget.js` — Bundled metadata (ESM)
|
|
737
|
+
- `build/widgets/{widget-name}/widget.min.js` + `widget.js` — Bundled metadata (ESM), when the widget ships a `widget.*` entry
|
|
711
738
|
- `build/widgets/{widget-name}/widget.min.asset.php` — Asset metadata for widget module
|
|
712
739
|
- `build/widgets/registry.php` — Widget registry data
|
|
713
740
|
- `build/widgets.php` — Script module registration logic
|
package/eslint-overrides.cjs
CHANGED
|
@@ -4,7 +4,7 @@ module.exports = [
|
|
|
4
4
|
rules: {
|
|
5
5
|
// Console should be allowed in a build tool.
|
|
6
6
|
'no-console': 'off',
|
|
7
|
-
// These rules are designed for source files processed by
|
|
7
|
+
// These rules are designed for source files processed by the build,
|
|
8
8
|
// not for the build script itself, which runs directly in Node.js.
|
|
9
9
|
'@wordpress/no-wp-process-env': 'off',
|
|
10
10
|
'@wordpress/wp-global-usage': 'off',
|
|
@@ -0,0 +1,87 @@
|
|
|
1
|
+
import os from 'node:os';
|
|
2
|
+
import { build } from 'rolldown';
|
|
3
|
+
|
|
4
|
+
/**
|
|
5
|
+
* @typedef {Object} QueuedBuild
|
|
6
|
+
* @property {() => Promise<unknown> | unknown} task Task to run.
|
|
7
|
+
* @property {(value: unknown) => void} resolve Promise resolver.
|
|
8
|
+
* @property {(reason?: unknown) => void} reject Promise rejecter.
|
|
9
|
+
*/
|
|
10
|
+
|
|
11
|
+
let buildConcurrency = 1;
|
|
12
|
+
let activeBuilds = 0;
|
|
13
|
+
/** @type {QueuedBuild[]} */
|
|
14
|
+
const queuedBuilds = [];
|
|
15
|
+
|
|
16
|
+
/**
|
|
17
|
+
* Get the default number of concurrent esbuild builds: half of the available
|
|
18
|
+
* parallelism, rounded up.
|
|
19
|
+
*
|
|
20
|
+
* @param {number} availableParallelism Available parallelism to use.
|
|
21
|
+
* @return {number} Default build concurrency.
|
|
22
|
+
*/
|
|
23
|
+
export function getDefaultBuildConcurrency(
|
|
24
|
+
availableParallelism = os.availableParallelism()
|
|
25
|
+
) {
|
|
26
|
+
return Math.max( 1, Math.ceil( availableParallelism / 2 ) );
|
|
27
|
+
}
|
|
28
|
+
|
|
29
|
+
/**
|
|
30
|
+
* Set the maximum number of concurrent esbuild builds.
|
|
31
|
+
*
|
|
32
|
+
* @param {number} concurrency Build concurrency.
|
|
33
|
+
*/
|
|
34
|
+
export function setBuildConcurrency( concurrency ) {
|
|
35
|
+
buildConcurrency = concurrency;
|
|
36
|
+
runNextBuilds();
|
|
37
|
+
}
|
|
38
|
+
|
|
39
|
+
/**
|
|
40
|
+
* Runs a Rolldown build while respecting the global wp-build concurrency limit.
|
|
41
|
+
*
|
|
42
|
+
* @param {import('rolldown').BuildOptions} buildOptions Rolldown build options.
|
|
43
|
+
* @return {Promise<import('rolldown').RolldownOutput>} Rolldown build output.
|
|
44
|
+
*/
|
|
45
|
+
export function buildWithConcurrency( buildOptions ) {
|
|
46
|
+
return enqueueBuild( () => build( buildOptions ) );
|
|
47
|
+
}
|
|
48
|
+
|
|
49
|
+
/**
|
|
50
|
+
* Runs an arbitrary async task while respecting the build concurrency limit.
|
|
51
|
+
*
|
|
52
|
+
* @template T
|
|
53
|
+
* @param {() => Promise<T> | T} task Task to run.
|
|
54
|
+
* @return {Promise<T>} Task result.
|
|
55
|
+
*/
|
|
56
|
+
export function enqueueBuild( task ) {
|
|
57
|
+
return new Promise( ( resolve, reject ) => {
|
|
58
|
+
queuedBuilds.push( {
|
|
59
|
+
task,
|
|
60
|
+
resolve: ( value ) =>
|
|
61
|
+
resolve( /** @type {T | PromiseLike<T>} */ ( value ) ),
|
|
62
|
+
reject,
|
|
63
|
+
} );
|
|
64
|
+
runNextBuilds();
|
|
65
|
+
} );
|
|
66
|
+
}
|
|
67
|
+
|
|
68
|
+
/**
|
|
69
|
+
* Starts queued build tasks while capacity is available.
|
|
70
|
+
*/
|
|
71
|
+
function runNextBuilds() {
|
|
72
|
+
while ( activeBuilds < buildConcurrency && queuedBuilds.length > 0 ) {
|
|
73
|
+
const queuedBuild = queuedBuilds.shift();
|
|
74
|
+
if ( ! queuedBuild ) {
|
|
75
|
+
return;
|
|
76
|
+
}
|
|
77
|
+
const { task, resolve, reject } = queuedBuild;
|
|
78
|
+
activeBuilds++;
|
|
79
|
+
Promise.resolve()
|
|
80
|
+
.then( task )
|
|
81
|
+
.then( resolve, reject )
|
|
82
|
+
.finally( () => {
|
|
83
|
+
activeBuilds--;
|
|
84
|
+
runNextBuilds();
|
|
85
|
+
} );
|
|
86
|
+
}
|
|
87
|
+
}
|