Skip to content
Closed
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
64 changes: 61 additions & 3 deletions Gruntfile.js
Original file line number Diff line number Diff line change
Expand Up @@ -603,7 +603,7 @@ module.exports = function(grunt) {
src: 'vendor/composer/ca-bundle/res/cacert.pem',
dest: SOURCE_DIR + 'wp-includes/certificates/ca-bundle.crt'
},
// Gutenberg PHP infrastructure files (routes.php, pages.php, constants.php, pages/, routes/).
// Gutenberg PHP infrastructure files (routes.php, pages.php, constants.php, pages/).
'gutenberg-php': {
options: {
process: function( content ) {
Expand All @@ -622,18 +622,32 @@ module.exports = function(grunt) {
'pages.php',
'constants.php',
Comment thread
desrosj marked this conversation as resolved.
'pages/**/*.php',
'routes/**/*.php',
],
dest: WORKING_DIR + 'wp-includes/build/',
} ],
},
/*
* Only copy files relevant to the routes specified in the registry file.
*
* While the registry file does not contain any experimental routes, the `gutenberg/build/routes` directory
* includes the files for all registered routes. Only the files related to the routes specified in the
* registry should be included in the WordPress build.
*
* The `src` list is populated at task runtime by `routes:setup`, which reads the registry after
* `gutenberg:download` has run. See the `routes:setup` task registration for implementation details.
*/
routes: {
expand: true,
cwd: 'gutenberg/build',
src: [],
dest: WORKING_DIR + 'wp-includes/build/',
},
'gutenberg-js': {
files: [ {
expand: true,
cwd: 'gutenberg/build',
src: [
'pages/**/*.js',
'routes/**/*.js',
],
dest: WORKING_DIR + 'wp-includes/build/',
} ],
Expand Down Expand Up @@ -2058,8 +2072,52 @@ module.exports = function(grunt) {
} );
} );

grunt.registerTask( 'routes:setup', 'Reads the routes registry and configures the copy:routes task.', function() {
const registryPath = 'gutenberg/build/routes/registry.php';
let registryContent;
try {
registryContent = fs.readFileSync( registryPath, 'utf8' );
} catch ( e ) {
grunt.fatal(
'Route registry not found at ' + registryPath + '. Run `grunt gutenberg:download` first.'
);
}
const namePattern = /'name'\s*=>\s*'([^']+)'/g;
Copy link
Copy Markdown
Member

@sirreal sirreal Mar 20, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can this rely on PHP being available? It would be nice to do do something like php -r '$route_registry = include __DIR__ . "/gutenberg/build/routes/registry.php"; echo json_encode( $route_registry, JSON_UNESCAPED_SLASHES );' and work with the data in JavaScript rather than pattern matching PHP code.

Copy link
Copy Markdown
Member Author

@desrosj desrosj Mar 20, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can this rely on PHP being available?

Good question. I don't know that we can. But I agree that would be preferable.

I should have noted this in the PR and the commit message, but my intention is for this to be a temporary fix. The real problem is that the wp-build package is including these files at all when the context does not ask for experimental or inactive routes. I opened WordPress/gutenberg#76715 to explore this, but it became clear that would have not been ready in time.

const routeNames = [];
let match;
while ( ( match = namePattern.exec( registryContent ) ) !== null ) {
routeNames.push( match[ 1 ] );
}
Comment thread
desrosj marked this conversation as resolved.

if ( routeNames.length === 0 ) {
grunt.fatal(
'No route names found in ' + registryPath + '. The format of the file may have changed.'
);
}

const validName = /^[A-Za-z0-9_-]+$/;
routeNames.forEach( function( name ) {
if ( ! validName.test( name ) ) {
grunt.fatal(
'Invalid route name \'' + name + '\' in ' + registryPath + '. Expected only letters, digits, hyphens, and underscores.'
);
}
} );

grunt.config( [ 'copy', 'routes', 'src' ], [ 'routes/registry.php' ].concat(
routeNames.flatMap( function( name ) {
return [
'routes/' + name + '/**/*.php',
'routes/' + name + '/**/*.js',
];
Comment thread
desrosj marked this conversation as resolved.
} )
) );
} );

grunt.registerTask( 'build:gutenberg', [
'copy:gutenberg-php',
'routes:setup',
'copy:routes',
'copy:gutenberg-js',
'gutenberg:copy',
'copy:gutenberg-modules',
Expand Down
Loading