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
4 changes: 4 additions & 0 deletions packages/wp-build/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,10 @@

## Unreleased

### Enhancements

- Limit concurrent `esbuild` build jobs to half of the available CPU parallelism ([#79889](https://github.com/WordPress/gutenberg/pull/79889)).

## 0.26.0 (2026-10-20)

### Breaking Changes
Expand Down
4 changes: 4 additions & 0 deletions packages/wp-build/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,10 @@ or via npm script:
}
```

### Build Concurrency

By default, `wp-build` limits concurrent `esbuild` 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
Expand Down
87 changes: 87 additions & 0 deletions packages/wp-build/lib/build-concurrency.mjs
Original file line number Diff line number Diff line change
@@ -0,0 +1,87 @@
import os from 'node:os';
import esbuild from 'esbuild';

/**
* @typedef {Object} QueuedBuild
* @property {() => Promise<unknown> | unknown} task Task to run.
* @property {(value: unknown) => void} resolve Promise resolver.
* @property {(reason?: unknown) => void} reject Promise rejecter.
*/

let buildConcurrency = 1;
let activeBuilds = 0;
/** @type {QueuedBuild[]} */
const queuedBuilds = [];

/**
* Get the default number of concurrent esbuild builds: half of the available
* parallelism, rounded up.
*
* @param {number} availableParallelism Available parallelism to use.
* @return {number} Default build concurrency.
*/
export function getDefaultBuildConcurrency(
availableParallelism = os.availableParallelism()
) {
return Math.max( 1, Math.ceil( availableParallelism / 2 ) );
}

/**
* Set the maximum number of concurrent esbuild builds.
*
* @param {number} concurrency Build concurrency.
*/
export function setBuildConcurrency( concurrency ) {
buildConcurrency = concurrency;
runNextBuilds();
}

/**
* Runs an esbuild build while respecting the global wp-build concurrency limit.
*
* @param {import('esbuild').BuildOptions} buildOptions Esbuild build options.
* @return {Promise<import('esbuild').BuildResult>} Esbuild build result.
*/
export function buildWithConcurrency( buildOptions ) {
return enqueueBuild( () => esbuild.build( buildOptions ) );
}

/**
* Runs an arbitrary async task while respecting the build concurrency limit.
*
* @template T
* @param {() => Promise<T> | T} task Task to run.
* @return {Promise<T>} Task result.
*/
export function enqueueBuild( task ) {
return new Promise( ( resolve, reject ) => {
queuedBuilds.push( {
task,
resolve: ( value ) =>
resolve( /** @type {T | PromiseLike<T>} */ ( value ) ),
reject,
} );
runNextBuilds();
} );
}

/**
* Starts queued build tasks while capacity is available.
*/
function runNextBuilds() {
while ( activeBuilds < buildConcurrency && queuedBuilds.length > 0 ) {
const queuedBuild = queuedBuilds.shift();
if ( ! queuedBuild ) {
return;
}
const { task, resolve, reject } = queuedBuild;
activeBuilds++;
Promise.resolve()
.then( task )
.then( resolve, reject )
.finally( () => {
activeBuilds--;
runNextBuilds();
} );
}
}
39 changes: 23 additions & 16 deletions packages/wp-build/lib/build.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ import { readFile, writeFile, copyFile, mkdir, unlink } from 'fs/promises';
import path from 'path';
import { createRequire as createNodeRequire } from 'node:module';
import { parseArgs } from 'node:util';
import esbuild from 'esbuild';
import glob from 'fast-glob';
import chokidar from 'chokidar';
import browserslistToEsbuild from 'browserslist-to-esbuild';
Expand Down Expand Up @@ -48,6 +47,11 @@ import {
compileInlineStyle,
dsTokenFallbacks,
} from './compile-inline-style.mjs';
import {
buildWithConcurrency,
getDefaultBuildConcurrency,
setBuildConcurrency,
} from './build-concurrency.mjs';

/**
* Resolve the ESBuild target from the project's Browserslist config.
Expand Down Expand Up @@ -600,7 +604,7 @@ async function bundlePackage( packageName, options = {} ) {
];

builds.push(
esbuild.build( {
buildWithConcurrency( {
...baseConfig,
outfile: path.join( outputDir, 'index.min.js' ),
minify: true,
Expand All @@ -615,7 +619,7 @@ async function bundlePackage( packageName, options = {} ) {
),
],
} ),
esbuild.build( {
buildWithConcurrency( {
...baseConfig,
outfile: path.join( outputDir, 'index.js' ),
minify: false,
Expand Down Expand Up @@ -671,7 +675,7 @@ async function bundlePackage( packageName, options = {} ) {
);

builds.push(
esbuild.build( {
buildWithConcurrency( {
loader: LOADER_CONFIG,
entryPoints: [ entryPoint ],
outfile: path.join(
Expand Down Expand Up @@ -703,7 +707,7 @@ async function bundlePackage( packageName, options = {} ) {

if ( ! isWasmWorker ) {
builds.push(
esbuild.build( {
buildWithConcurrency( {
loader: LOADER_CONFIG,
entryPoints: [ entryPoint ],
outfile: path.join(
Expand Down Expand Up @@ -1424,7 +1428,7 @@ async function transpilePackage( packageName ) {

if ( packageJson.main ) {
builds.push(
esbuild.build( {
buildWithConcurrency( {
loader: LOADER_CONFIG,
entryPoints: srcFiles,
outdir: buildDir,
Expand Down Expand Up @@ -1458,7 +1462,7 @@ async function transpilePackage( packageName ) {

if ( packageJson.module ) {
builds.push(
esbuild.build( {
buildWithConcurrency( {
loader: LOADER_CONFIG,
entryPoints: srcFiles,
outdir: buildModuleDir,
Expand Down Expand Up @@ -1566,7 +1570,7 @@ async function compileStyles( packageName ) {

await mkdir( outputDir, { recursive: true } );

await esbuild.build( {
await buildWithConcurrency( {
entryPoints: [ styleEntryPath ],
outdir: outputDir,
bundle: true,
Expand Down Expand Up @@ -1702,7 +1706,7 @@ async function buildRoute( routeName ) {
if ( routeEntryPoints.length > 0 ) {
// Build both minified and non-minified versions in parallel
await Promise.all( [
esbuild.build( {
buildWithConcurrency( {
loader: LOADER_CONFIG,
entryPoints: routeEntryPoints,
outfile: path.join( outputDir, 'route.min.js' ),
Expand All @@ -1721,7 +1725,7 @@ async function buildRoute( routeName ) {
),
],
} ),
esbuild.build( {
buildWithConcurrency( {
loader: LOADER_CONFIG,
entryPoints: routeEntryPoints,
outfile: path.join( outputDir, 'route.js' ),
Expand Down Expand Up @@ -1756,7 +1760,7 @@ async function buildRoute( routeName ) {

// Build both minified and non-minified versions in parallel
await Promise.all( [
esbuild.build( {
buildWithConcurrency( {
loader: LOADER_CONFIG,
entryPoints: [ tempEntryPath ],
outfile: path.join( outputDir, 'content.min.js' ),
Expand All @@ -1775,7 +1779,7 @@ async function buildRoute( routeName ) {
),
],
} ),
esbuild.build( {
buildWithConcurrency( {
loader: LOADER_CONFIG,
entryPoints: [ tempEntryPath ],
outfile: path.join( outputDir, 'content.js' ),
Expand Down Expand Up @@ -1869,7 +1873,7 @@ async function buildWidget( widgetName ) {
if ( renderEntryPoints.length > 0 ) {
// Build both minified and non-minified versions in parallel
await Promise.all( [
esbuild.build( {
buildWithConcurrency( {
loader: LOADER_CONFIG,
entryPoints: renderEntryPoints,
outfile: path.join( outputDir, 'render.min.js' ),
Expand All @@ -1888,7 +1892,7 @@ async function buildWidget( widgetName ) {
),
],
} ),
esbuild.build( {
buildWithConcurrency( {
loader: LOADER_CONFIG,
entryPoints: renderEntryPoints,
outfile: path.join( outputDir, 'render.js' ),
Expand Down Expand Up @@ -1922,7 +1926,7 @@ async function buildWidget( widgetName ) {
if ( widgetEntryPoints.length > 0 ) {
// Build both minified and non-minified versions in parallel
await Promise.all( [
esbuild.build( {
buildWithConcurrency( {
loader: LOADER_CONFIG,
entryPoints: widgetEntryPoints,
outfile: path.join( outputDir, 'widget.min.js' ),
Expand All @@ -1941,7 +1945,7 @@ async function buildWidget( widgetName ) {
),
],
} ),
esbuild.build( {
buildWithConcurrency( {
loader: LOADER_CONFIG,
entryPoints: widgetEntryPoints,
outfile: path.join( outputDir, 'widget.js' ),
Expand Down Expand Up @@ -2865,6 +2869,9 @@ async function main() {
} );

const baseUrlExpression = values[ 'base-url' ];
const buildConcurrency = getDefaultBuildConcurrency();
setBuildConcurrency( buildConcurrency );
console.log( `⚙️ Using build concurrency: ${ buildConcurrency }\n` );

await buildAll( baseUrlExpression );

Expand Down
75 changes: 75 additions & 0 deletions packages/wp-build/lib/test/build-concurrency.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@
import { afterEach, describe, expect, it, vi } from 'vitest';
import {
enqueueBuild,
getDefaultBuildConcurrency,
setBuildConcurrency,
} from '../build-concurrency.mjs';

vi.mock( import( 'esbuild' ), () => ( {
default: {
build: vi.fn(),
},
} ) );

const flushPromises = () =>
new Promise( ( resolve ) => {
setTimeout( resolve, 0 );
} );

describe( 'getDefaultBuildConcurrency()', () => {
it( 'uses half of the available parallelism, rounded up', () => {
expect( getDefaultBuildConcurrency( 1 ) ).toBe( 1 );
expect( getDefaultBuildConcurrency( 2 ) ).toBe( 1 );
expect( getDefaultBuildConcurrency( 3 ) ).toBe( 2 );
expect( getDefaultBuildConcurrency( 4 ) ).toBe( 2 );
expect( getDefaultBuildConcurrency( 5 ) ).toBe( 3 );
expect( getDefaultBuildConcurrency( 12 ) ).toBe( 6 );
expect( getDefaultBuildConcurrency( 64 ) ).toBe( 32 );
} );
} );

describe( 'enqueueBuild()', () => {
afterEach( () => {
setBuildConcurrency( getDefaultBuildConcurrency( 12 ) );
} );

it( 'runs no more than the configured number of tasks at once', async () => {
setBuildConcurrency( 2 );

const startedTasks = [];
const taskResolvers = [];
const tasks = [ 0, 1, 2, 3 ].map( ( index ) =>
enqueueBuild(
() =>
new Promise( ( resolve ) => {
startedTasks.push( index );
taskResolvers[ index ] = resolve;
} )
)
);

await flushPromises();

expect( startedTasks ).toEqual( [ 0, 1 ] );

taskResolvers[ 0 ]( 'first' );
await flushPromises();

expect( startedTasks ).toEqual( [ 0, 1, 2 ] );

taskResolvers[ 1 ]( 'second' );
await flushPromises();

expect( startedTasks ).toEqual( [ 0, 1, 2, 3 ] );

taskResolvers[ 2 ]( 'third' );
taskResolvers[ 3 ]( 'fourth' );

await expect( Promise.all( tasks ) ).resolves.toEqual( [
'first',
'second',
'third',
'fourth',
] );
} );
} );
10 changes: 5 additions & 5 deletions packages/wp-build/lib/worker-build.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
*/
import { readFile, writeFile, access } from 'fs/promises';
import path from 'path';
import esbuild from 'esbuild';
import { buildWithConcurrency } from './build-concurrency.mjs';

/**
* Creates an esbuild plugin that redirects module loads based on filename patterns.
Expand Down Expand Up @@ -178,7 +178,7 @@ export async function buildWorkers(
// Build ESM worker for build-module (primary for browser use).
if ( packageJson.module ) {
workerBuilds.push(
esbuild.build( {
buildWithConcurrency( {
entryPoints: [ workerEntryPoint ],
outfile: path.join(
buildModuleDir,
Expand Down Expand Up @@ -220,7 +220,7 @@ export async function buildWorkers(
// unused or misleading CJS artifacts.
if ( packageJson.main ) {
workerBuilds.push(
esbuild.build( {
buildWithConcurrency( {
entryPoints: [ workerEntryPoint ],
outfile: path.join( buildDir, `${ workerOutputName }.cjs` ),
bundle: true,
Expand Down Expand Up @@ -327,7 +327,7 @@ export const workerCode = ${ JSON.stringify( workerContent ) };

if ( packageJson.module ) {
retranspileBuilds.push(
esbuild.build( {
buildWithConcurrency( {
entryPoints: [ workerCodeSrcFile ],
outdir: buildModuleDir,
outbase: srcDir,
Expand All @@ -349,7 +349,7 @@ export const workerCode = ${ JSON.stringify( workerContent ) };

if ( packageJson.main ) {
retranspileBuilds.push(
esbuild.build( {
buildWithConcurrency( {
entryPoints: [ workerCodeSrcFile ],
outdir: buildDir,
outbase: srcDir,
Expand Down
Loading