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
120 changes: 85 additions & 35 deletions bin/packages/build-v2.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -134,46 +134,96 @@ async function bundlePackage( packageName ) {
const packageJsonPath = path.join( packageDir, 'package.json' );
const packageJson = JSON.parse( await readFile( packageJsonPath, 'utf8' ) );

// Skip if package doesn't need WordPress bundling
if ( ! packageJson.wpScript ) {
return false;
const builds = [];

// Bundle wpScript (IIFE format for global wp.* namespace)
if ( packageJson.wpScript ) {
const entryPoint = path.join( packageDir, 'build-module', 'index.js' );
const outputDir = path.join( PACKAGES_DIR, '..', 'build', packageName );
const target = browserslistToEsbuild();
const globalName = `wp.${ kebabToCamelCase( packageName ) }`;

const baseConfig = {
entryPoints: [ entryPoint ],
bundle: true,
sourcemap: true,
format: 'iife',
target,
platform: 'browser',
globalName,
};

// For packages with default exports, add a footer to properly expose the default
if ( packageJson.wpScriptDefaultExport ) {
baseConfig.footer = {
js: `if (typeof ${ globalName } === 'object' && ${ globalName }.default) { ${ globalName } = ${ globalName }.default; }`,
};
}

builds.push(
esbuild.build( {
...baseConfig,
outfile: path.join( outputDir, 'index.min.js' ),
minify: true,
plugins: [ wordpressExternalsPlugin() ],
} ),
esbuild.build( {
...baseConfig,
outfile: path.join( outputDir, 'index.js' ),
minify: false,
} )
);
}

const entryPoint = path.join( packageDir, 'build-module', 'index.js' );
const outputDir = path.join( PACKAGES_DIR, '..', 'build', packageName );
const target = browserslistToEsbuild();
const globalName = `wp.${ kebabToCamelCase( packageName ) }`;

const baseConfig = {
entryPoints: [ entryPoint ],
bundle: true,
sourcemap: true,
format: 'iife',
target,
platform: 'browser',
globalName,
};
// Bundle wpScriptModuleExports (ESM format for Script Modules API)
if ( packageJson.wpScriptModuleExports ) {
const target = browserslistToEsbuild();
const rootBuildModuleDir = path.join(
PACKAGES_DIR,
'..',
'build-module',
packageName
);

// For packages with default exports, add a footer to properly expose the default
if ( packageJson.wpScriptDefaultExport ) {
baseConfig.footer = {
js: `if (typeof ${ globalName } === 'object' && ${ globalName }.default) { ${ globalName } = ${ globalName }.default; }`,
};
// Normalize to object format
const exports =
typeof packageJson.wpScriptModuleExports === 'string'
? { '.': packageJson.wpScriptModuleExports }
: packageJson.wpScriptModuleExports;

// Bundle each export
for ( const [ exportName, exportPath ] of Object.entries( exports ) ) {
// Convert export name to file name: '.' -> 'index', './debug' -> 'debug'
const fileName =
exportName === '.'
? 'index'
: exportName.replace( /^\.\//, '' );
const entryPoint = path.join( packageDir, exportPath );

builds.push(
esbuild.build( {
entryPoints: [ entryPoint ],
outfile: path.join(
rootBuildModuleDir,
`${ fileName }.min.js`
),
bundle: true,
sourcemap: true,
format: 'esm',
target,
platform: 'browser',
minify: true,
plugins: [ wordpressExternalsPlugin() ],
} )
);
}
}

await Promise.all( [
esbuild.build( {
...baseConfig,
outfile: path.join( outputDir, 'index.min.js' ),
minify: true,
plugins: [ wordpressExternalsPlugin() ],
} ),
esbuild.build( {
...baseConfig,
outfile: path.join( outputDir, 'index.js' ),
minify: false,
} ),
] );
if ( builds.length === 0 ) {
return false;
}

await Promise.all( builds );

return true;
}
Expand Down
1 change: 1 addition & 0 deletions bin/packages/v2-packages.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
* @type {string[]}
*/
const V2_PACKAGES = [
'a11y',
'api-fetch',
'autop',
'blob',
Expand Down
25 changes: 23 additions & 2 deletions lib/client-assets.php
Original file line number Diff line number Diff line change
Expand Up @@ -617,16 +617,37 @@ function gutenberg_register_vendor_scripts( $scripts ) {
* @since 19.3.0
*/
function gutenberg_default_script_modules() {
$all_assets = array();

/*
* Load combined assets.php for v1 packages (webpack build).
* Expects multidimensional array like:
*
* 'interactivity/index.min.js' => array('dependencies' => array(…), 'version' => '…'),
* 'interactivity/debug.min.js' => array('dependencies' => array(…), 'version' => '…'),
* 'interactivity-router/index.min.js' => …
*/
$assets = include gutenberg_dir_path() . '/build-module/assets.php';
$combined_assets_file = gutenberg_dir_path() . '/build-module/assets.php';
if ( file_exists( $combined_assets_file ) ) {
$all_assets = include $combined_assets_file;
}

/*
* Load individual asset files for v2 packages (esbuild build).
* Follows the same pattern as regular scripts in gutenberg_register_packages_scripts().
*/
foreach ( glob( gutenberg_dir_path() . 'build-module/*/*.min.js' ) as $path ) {
$asset_file = substr( $path, 0, -3 ) . '.asset.php';
if ( ! file_exists( $asset_file ) ) {
continue;
}

$asset = require $asset_file;
$file_name = str_replace( gutenberg_dir_path() . 'build-module/', '', $path );
$all_assets[ $file_name ] = $asset;
}

foreach ( $assets as $file_name => $script_module_data ) {
foreach ( $all_assets as $file_name => $script_module_data ) {
/*
* Build the WordPress Script Module ID from the file name.
* Prepend `@wordpress/` and remove extensions and `/index` if present:
Expand Down
1 change: 0 additions & 1 deletion package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

9 changes: 8 additions & 1 deletion packages/a11y/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -26,12 +26,19 @@
},
"main": "build/index.js",
"module": "build-module/index.js",
"exports": {
".": {
"types": "./build-types/index.d.ts",
"import": "./build-module/index.js",
"require": "./build/index.js"
},
"./package.json": "./package.json"
},
"react-native": "src/index",
"wpScript": true,
"wpScriptModuleExports": "./build-module/module/index.js",
"types": "build-types",
"dependencies": {
"@babel/runtime": "7.25.7",
"@wordpress/dom-ready": "file:../dom-ready",
"@wordpress/i18n": "file:../i18n"
},
Expand Down
6 changes: 6 additions & 0 deletions tools/webpack/script-modules.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ const DependencyExtractionWebpackPlugin = require( '@wordpress/dependency-extrac
* Internal dependencies
*/
const { baseConfig, plugins } = require( './shared' );
const { V2_PACKAGES } = require( '../../bin/packages/v2-packages' );

const WORDPRESS_NAMESPACE = '@wordpress/';

Expand All @@ -26,6 +27,11 @@ const packageDirs = readdirSync(
/** @type {Map<string, string>} */
const gutenbergScriptModules = new Map();
for ( const packageDir of packageDirs ) {
// Skip v2 packages - they're built with build-v2.mjs
if ( V2_PACKAGES.includes( packageDir ) ) {
continue;
}

const packageJson = require( `@wordpress/${ packageDir }/package.json` );

if ( ! Object.hasOwn( packageJson, 'wpScriptModuleExports' ) ) {
Expand Down