Skip to content
Closed
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: 2 additions & 2 deletions src/wp-includes/script-loader.php
Original file line number Diff line number Diff line change
Expand Up @@ -281,11 +281,11 @@ function wp_default_packages_scripts( $scripts ) {
* 'annotations.js' => array('dependencies' => array(...), 'version' => '...'),
* 'api-fetch.js' => array(...
*/
$assets_file = ABSPATH . WPINC . "/assets/script-loader-packages{$suffix}.php";
$assets_file = ABSPATH . WPINC . '/assets/script-loader-packages.php';
$assets = file_exists( $assets_file ) ? include $assets_file : array();

foreach ( $assets as $file_name => $package_data ) {
$basename = str_replace( $suffix . '.js', '', basename( $file_name ) );
$basename = str_replace( '.js', '', basename( $file_name ) );
$handle = 'wp-' . $basename;
$path = "/wp-includes/js/dist/{$basename}{$suffix}.js";

Expand Down
12 changes: 7 additions & 5 deletions src/wp-includes/script-modules.php
Original file line number Diff line number Diff line change
Expand Up @@ -149,11 +149,11 @@ function wp_default_script_modules() {
/*
* Expects multidimensional array like:
*
* 'interactivity/index.min.js' => array('dependencies' => array(…), 'version' => '…'),
* 'interactivity-router/index.min.js' => array('dependencies' => array(…), 'version' => '…'),
* 'block-library/navigation/view.min.js' => …
* 'interactivity/index.js' => array('dependencies' => array(…), 'version' => '…'),
* 'interactivity-router/index.js' => array('dependencies' => array(…), 'version' => '…'),
* 'block-library/navigation/view.js' => …
*/
$assets_file = ABSPATH . WPINC . "/assets/script-modules-packages{$suffix}.php";
$assets_file = ABSPATH . WPINC . '/assets/script-modules-packages.php';
$assets = file_exists( $assets_file ) ? include $assets_file : array();

foreach ( $assets as $file_name => $script_module_data ) {
Expand Down Expand Up @@ -192,8 +192,10 @@ function wp_default_script_modules() {

// VIPS files are always minified — the non-minified versions are not
// shipped because they are ~10MB of inlined WASM with no debugging value.
if ( str_starts_with( $file_name, 'vips/' ) && ! str_contains( $file_name, '.min.' ) ) {
if ( str_starts_with( $file_name, 'vips/' ) ) {
$file_name = str_replace( '.js', '.min.js', $file_name );
} elseif ( '' !== $suffix ) {
$file_name = str_replace( '.js', $suffix . '.js', $file_name );
}

$path = includes_url( "js/dist/script-modules/{$file_name}" );
Expand Down
95 changes: 27 additions & 68 deletions tools/gutenberg/copy.js
Original file line number Diff line number Diff line change
Expand Up @@ -351,14 +351,13 @@ function copyBlockAssets( config ) {
}

/**
* Generate script-modules-packages.min.php from individual asset files.
* Reads all view.min.asset.php files from modules/block-library and combines them
* into a single PHP file.
* Generate script-modules-packages.php from individual asset files.
* Recursively scans the Gutenberg modules/ directory for *.min.asset.php files
* and combines their contents into a single PHP file.
*/
function generateScriptModulesPackages() {
const modulesDir = path.join( gutenbergBuildDir, 'modules' );
const assetsMin = {};
const assetsRegular = {};
const assets = {};

/**
* Recursively process directory to find .asset.php files.
Expand All @@ -384,16 +383,13 @@ function generateScriptModulesPackages() {
const normalizedPath = relativePath
.split( path.sep )
.join( '/' );
const jsPathMin = normalizedPath.replace(
/\.asset\.php$/,
'.js'
);
const jsPathRegular = jsPathMin.replace( /\.min\.js$/, '.js' );
const jsPath = normalizedPath
.replace( /\.asset\.php$/, '.js' )
.replace( /\.min\.js$/, '.js' );

try {
const assetData = readReturnedValueFromPHPFile( fullPath );
assetsMin[ jsPathMin ] = assetData;
assetsRegular[ jsPathRegular ] = assetData;
assets[ jsPath ] = assetData;
} catch ( error ) {
console.error(
` ⚠️ Error reading ${ relativePath }:`,
Expand All @@ -406,52 +402,35 @@ function generateScriptModulesPackages() {

processDirectory( modulesDir, modulesDir );

// Generate both minified and non-minified PHP files using json2php.
const phpContentMin =
'<?php return ' +
json2php.make( {
linebreak: '\n',
indent: ' ',
shortArraySyntax: false,
} )( assetsMin ) +
';';

const phpContentRegular =
const phpContent =
'<?php return ' +
json2php.make( {
linebreak: '\n',
indent: ' ',
indent: '\t',
shortArraySyntax: false,
} )( assetsRegular ) +
} )( assets ) +
';';

const outputPathMin = path.join(
wpIncludesDir,
'assets/script-modules-packages.min.php'
);
const outputPathRegular = path.join(
const outputPath = path.join(
wpIncludesDir,
'assets/script-modules-packages.php'
);

fs.mkdirSync( path.dirname( outputPathMin ), { recursive: true } );
fs.writeFileSync( outputPathMin, phpContentMin );
fs.writeFileSync( outputPathRegular, phpContentRegular );
fs.mkdirSync( path.dirname( outputPath ), { recursive: true } );
fs.writeFileSync( outputPath, phpContent );

console.log(
` ✅ Generated with ${ Object.keys( assetsMin ).length } modules`
` ✅ Generated with ${ Object.keys( assets ).length } modules`
);
}

/**
* Generate script-loader-packages.php and script-loader-packages.min.php from individual asset files.
* Reads all .min.asset.php files from scripts/ and combines them into PHP files for script registration.
* Generates both minified and non-minified versions.
* Generate script-loader-packages.php from individual asset files.
* Reads all .min.asset.php files from scripts/ and combines them into a PHP file for script registration.
*/
function generateScriptLoaderPackages() {
const scriptsDir = path.join( gutenbergBuildDir, 'scripts' );
const assetsMin = {};
const assetsRegular = {};
const assets = {};

if ( ! fs.existsSync( scriptsDir ) ) {
console.log( ' ⚠️ Scripts directory not found' );
Expand Down Expand Up @@ -482,12 +461,7 @@ function generateScriptLoaderPackages() {
assetData.dependencies = [];
}

// Create entries for both minified and non-minified versions.
const jsPathMin = `${ entry.name }.min.js`;
const jsPathRegular = `${ entry.name }.js`;

assetsMin[ jsPathMin ] = assetData;
assetsRegular[ jsPathRegular ] = assetData;
assets[ `${ entry.name }.js` ] = assetData;
} catch ( error ) {
console.error(
` ⚠️ Error reading ${ entry.name }/index.min.asset.php:`,
Expand All @@ -496,40 +470,25 @@ function generateScriptLoaderPackages() {
}
}

// Generate both minified and non-minified PHP files using json2php.
const phpContentMin =
'<?php return ' +
json2php.make( {
linebreak: '\n',
indent: ' ',
shortArraySyntax: false,
} )( assetsMin ) +
';';

const phpContentRegular =
const phpContent =
'<?php return ' +
json2php.make( {
linebreak: '\n',
indent: ' ',
indent: '\t',
shortArraySyntax: false,
} )( assetsRegular ) +
} )( assets ) +
';';

const outputPathMin = path.join(
wpIncludesDir,
'assets/script-loader-packages.min.php'
);
const outputPathRegular = path.join(
const outputPath = path.join(
wpIncludesDir,
'assets/script-loader-packages.php'
);

fs.mkdirSync( path.dirname( outputPathMin ), { recursive: true } );
fs.writeFileSync( outputPathMin, phpContentMin );
fs.writeFileSync( outputPathRegular, phpContentRegular );
fs.mkdirSync( path.dirname( outputPath ), { recursive: true } );
fs.writeFileSync( outputPath, phpContent );

console.log(
` ✅ Generated with ${ Object.keys( assetsMin ).length } packages`
` ✅ Generated with ${ Object.keys( assets ).length } packages`
);
}

Expand Down Expand Up @@ -669,7 +628,7 @@ function generateBlocksJson() {
'<?php return ' +
json2php.make( {
linebreak: '\n',
indent: ' ',
indent: '\t',
shortArraySyntax: false,
} )( blocks ) +
';';
Expand Down
Loading