@@ -55,6 +55,8 @@ module.exports = function(grunt) {
5555 'wp-includes/css/dist' ,
5656 'wp-includes/blocks/**/*' ,
5757 '!wp-includes/blocks/index.php' ,
58+ 'wp-includes/images/icon-library' ,
59+ // Old location kept temporarily to ensure they are cleaned up.
5860 'wp-includes/icons' ,
5961 ] ,
6062
@@ -602,7 +604,7 @@ module.exports = function(grunt) {
602604 src : 'vendor/composer/ca-bundle/res/cacert.pem' ,
603605 dest : SOURCE_DIR + 'wp-includes/certificates/ca-bundle.crt'
604606 } ,
605- // Gutenberg PHP infrastructure files (routes.php, pages.php, constants.php, pages/, routes/ ).
607+ // Gutenberg PHP infrastructure files (routes.php, pages.php, constants.php, pages/).
606608 'gutenberg-php' : {
607609 options : {
608610 process : function ( content ) {
@@ -621,18 +623,32 @@ module.exports = function(grunt) {
621623 'pages.php' ,
622624 'constants.php' ,
623625 'pages/**/*.php' ,
624- 'routes/**/*.php' ,
625626 ] ,
626627 dest : WORKING_DIR + 'wp-includes/build/' ,
627628 } ] ,
628629 } ,
630+ /*
631+ * Only copy files relevant to the routes specified in the registry file.
632+ *
633+ * While the registry file does not contain any experimental routes, the `gutenberg/build/routes` directory
634+ * includes the files for all registered routes. Only the files related to the routes specified in the
635+ * registry should be included in the WordPress build.
636+ *
637+ * The `src` list is populated at task runtime by `routes:setup`, which reads the registry after
638+ * `gutenberg:download` has run. See the `routes:setup` task registration for implementation details.
639+ */
640+ routes : {
641+ expand : true ,
642+ cwd : 'gutenberg/build' ,
643+ src : [ ] ,
644+ dest : WORKING_DIR + 'wp-includes/build/' ,
645+ } ,
629646 'gutenberg-js' : {
630647 files : [ {
631648 expand : true ,
632649 cwd : 'gutenberg/build' ,
633650 src : [
634651 'pages/**/*.js' ,
635- 'routes/**/*.js' ,
636652 ] ,
637653 dest : WORKING_DIR + 'wp-includes/build/' ,
638654 } ] ,
@@ -644,9 +660,7 @@ module.exports = function(grunt) {
644660 src : [
645661 '**/*' ,
646662 '!**/*.map' ,
647- // Skip non-minified VIPS files — they are ~16MB of inlined WASM
648- // with no debugging value over the minified versions.
649- '!vips/!(*.min).js' ,
663+ '!vips/**' ,
650664 ] ,
651665 dest : WORKING_DIR + 'wp-includes/js/dist/script-modules/' ,
652666 } ] ,
@@ -655,7 +669,12 @@ module.exports = function(grunt) {
655669 files : [ {
656670 expand : true ,
657671 cwd : 'gutenberg/build/styles' ,
658- src : [ '**/*' , '!**/*.map' ] ,
672+ src : [
673+ '**/*' ,
674+ '!**/*.map' ,
675+ // Per-block CSS is copied to wp-includes/blocks/ by tools/gutenberg/copy.js.
676+ '!block-library/*/**' ,
677+ ] ,
659678 dest : WORKING_DIR + 'wp-includes/css/dist/' ,
660679 } ] ,
661680 } ,
@@ -683,31 +702,35 @@ module.exports = function(grunt) {
683702 } ,
684703 ] ,
685704 } ,
686- 'gutenberg-icons' : {
705+ 'icon-library-images' : {
706+ files : [ {
707+ expand : true ,
708+ cwd : 'gutenberg/packages/icons/src/library' ,
709+ src : '*.svg' ,
710+ dest : WORKING_DIR + 'wp-includes/images/icon-library' ,
711+ } ] ,
712+ } ,
713+ 'icon-library-manifest' : {
687714 options : {
688- process : function ( content , srcpath ) {
689- // Remove the 'gutenberg' text domain from _x() calls in manifest.php.
690- if ( path . basename ( srcpath ) === 'manifest.php' ) {
691- return content . replace (
715+ process : function ( content ) {
716+ return content
717+ // Remove the 'gutenberg' text domain from _x() calls.
718+ . replace (
692719 / _ x \( \s * ( [ ^ , ] + ) , \s * ( [ ^ , ] + ) , \s * [ ' " ] g u t e n b e r g [ ' " ] \s * \) / g,
693720 '_x( $1, $2 )'
721+ )
722+ // Strip the 'library/' prefix from filePath values so they
723+ // resolve correctly relative to wp-includes/images/icon-library/.
724+ . replace (
725+ / ' f i l e P a t h ' = > ' l i b r a r y \/ / g,
726+ '\'filePath\' => \''
694727 ) ;
695- }
696- return content ;
697728 }
698729 } ,
699- files : [
700- {
701- src : 'gutenberg/packages/icons/src/manifest.php' ,
702- dest : WORKING_DIR + 'wp-includes/icons/manifest.php' ,
703- } ,
704- {
705- expand : true ,
706- cwd : 'gutenberg/packages/icons/src/library' ,
707- src : '*.svg' ,
708- dest : WORKING_DIR + 'wp-includes/icons/library/' ,
709- } ,
710- ] ,
730+ files : [ {
731+ src : 'gutenberg/packages/icons/src/manifest.php' ,
732+ dest : WORKING_DIR + 'wp-includes/assets/icon-library-manifest.php' ,
733+ } ] ,
711734 } ,
712735 } ,
713736 sass : {
@@ -2057,14 +2080,59 @@ module.exports = function(grunt) {
20572080 } ) ;
20582081 } ) ;
20592082
2083+ grunt . registerTask ( 'routes:setup' , 'Reads the routes registry and configures the copy:routes task.' , function ( ) {
2084+ const registryPath = 'gutenberg/build/routes/registry.php' ;
2085+ let registryContent ;
2086+ try {
2087+ registryContent = fs . readFileSync ( registryPath , 'utf8' ) ;
2088+ } catch ( e ) {
2089+ grunt . fatal (
2090+ 'Route registry not found at ' + registryPath + '. Run `grunt gutenberg:download` first.'
2091+ ) ;
2092+ }
2093+ const namePattern = / ' n a m e ' \s * = > \s * ' ( [ ^ ' ] + ) ' / g;
2094+ const routeNames = [ ] ;
2095+ let match ;
2096+ while ( ( match = namePattern . exec ( registryContent ) ) !== null ) {
2097+ routeNames . push ( match [ 1 ] ) ;
2098+ }
2099+
2100+ if ( routeNames . length === 0 ) {
2101+ grunt . fatal (
2102+ 'No route names found in ' + registryPath + '. The format of the file may have changed.'
2103+ ) ;
2104+ }
2105+
2106+ const validName = / ^ [ A - Z a - z 0 - 9 _ - ] + $ / ;
2107+ routeNames . forEach ( function ( name ) {
2108+ if ( ! validName . test ( name ) ) {
2109+ grunt . fatal (
2110+ 'Invalid route name \'' + name + '\' in ' + registryPath + '. Expected only letters, digits, hyphens, and underscores.'
2111+ ) ;
2112+ }
2113+ } ) ;
2114+
2115+ grunt . config ( [ 'copy' , 'routes' , 'src' ] , [ 'routes/registry.php' ] . concat (
2116+ routeNames . flatMap ( function ( name ) {
2117+ return [
2118+ 'routes/' + name + '/**/*.php' ,
2119+ 'routes/' + name + '/**/*.js' ,
2120+ ] ;
2121+ } )
2122+ ) ) ;
2123+ } ) ;
2124+
20602125 grunt . registerTask ( 'build:gutenberg' , [
20612126 'copy:gutenberg-php' ,
2127+ 'routes:setup' ,
2128+ 'copy:routes' ,
20622129 'copy:gutenberg-js' ,
20632130 'gutenberg:copy' ,
20642131 'copy:gutenberg-modules' ,
20652132 'copy:gutenberg-styles' ,
20662133 'copy:gutenberg-theme-json' ,
2067- 'copy:gutenberg-icons' ,
2134+ 'copy:icon-library-images' ,
2135+ 'copy:icon-library-manifest' ,
20682136 ] ) ;
20692137
20702138 grunt . registerTask ( 'build' , function ( ) {
0 commit comments