@@ -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
@@ -601,7 +603,7 @@ module.exports = function(grunt) {
601603 src : 'vendor/composer/ca-bundle/res/cacert.pem' ,
602604 dest : SOURCE_DIR + 'wp-includes/certificates/ca-bundle.crt'
603605 } ,
604- // Gutenberg PHP infrastructure files (routes.php, pages.php, constants.php, pages/, routes/ ).
606+ // Gutenberg PHP infrastructure files (routes.php, pages.php, constants.php, pages/).
605607 'gutenberg-php' : {
606608 options : {
607609 process : function ( content ) {
@@ -620,18 +622,32 @@ module.exports = function(grunt) {
620622 'pages.php' ,
621623 'constants.php' ,
622624 'pages/**/*.php' ,
623- 'routes/**/*.php' ,
624625 ] ,
625626 dest : WORKING_DIR + 'wp-includes/build/' ,
626627 } ] ,
627628 } ,
629+ /*
630+ * Only copy files relevant to the routes specified in the registry file.
631+ *
632+ * While the registry file does not contain any experimental routes, the `gutenberg/build/routes` directory
633+ * includes the files for all registered routes. Only the files related to the routes specified in the
634+ * registry should be included in the WordPress build.
635+ *
636+ * The `src` list is populated at task runtime by `routes:setup`, which reads the registry after
637+ * `gutenberg:download` has run. See the `routes:setup` task registration for implementation details.
638+ */
639+ routes : {
640+ expand : true ,
641+ cwd : 'gutenberg/build' ,
642+ src : [ ] ,
643+ dest : WORKING_DIR + 'wp-includes/build/' ,
644+ } ,
628645 'gutenberg-js' : {
629646 files : [ {
630647 expand : true ,
631648 cwd : 'gutenberg/build' ,
632649 src : [
633650 'pages/**/*.js' ,
634- 'routes/**/*.js' ,
635651 ] ,
636652 dest : WORKING_DIR + 'wp-includes/build/' ,
637653 } ] ,
@@ -643,9 +659,7 @@ module.exports = function(grunt) {
643659 src : [
644660 '**/*' ,
645661 '!**/*.map' ,
646- // Skip non-minified VIPS files — they are ~16MB of inlined WASM
647- // with no debugging value over the minified versions.
648- '!vips/!(*.min).js' ,
662+ '!vips/**' ,
649663 ] ,
650664 dest : WORKING_DIR + 'wp-includes/js/dist/script-modules/' ,
651665 } ] ,
@@ -682,31 +696,35 @@ module.exports = function(grunt) {
682696 } ,
683697 ] ,
684698 } ,
685- 'gutenberg-icons' : {
699+ 'icon-library-images' : {
700+ files : [ {
701+ expand : true ,
702+ cwd : 'gutenberg/packages/icons/src/library' ,
703+ src : '*.svg' ,
704+ dest : WORKING_DIR + 'wp-includes/images/icon-library' ,
705+ } ] ,
706+ } ,
707+ 'icon-library-manifest' : {
686708 options : {
687- process : function ( content , srcpath ) {
688- // Remove the 'gutenberg' text domain from _x() calls in manifest.php.
689- if ( path . basename ( srcpath ) === 'manifest.php' ) {
690- return content . replace (
709+ process : function ( content ) {
710+ return content
711+ // Remove the 'gutenberg' text domain from _x() calls.
712+ . replace (
691713 / _ x \( \s * ( [ ^ , ] + ) , \s * ( [ ^ , ] + ) , \s * [ ' " ] g u t e n b e r g [ ' " ] \s * \) / g,
692714 '_x( $1, $2 )'
715+ )
716+ // Strip the 'library/' prefix from filePath values so they
717+ // resolve correctly relative to wp-includes/images/icon-library/.
718+ . replace (
719+ / ' f i l e P a t h ' = > ' l i b r a r y \/ / g,
720+ '\'filePath\' => \''
693721 ) ;
694- }
695- return content ;
696722 }
697723 } ,
698- files : [
699- {
700- src : 'gutenberg/packages/icons/src/manifest.php' ,
701- dest : WORKING_DIR + 'wp-includes/icons/manifest.php' ,
702- } ,
703- {
704- expand : true ,
705- cwd : 'gutenberg/packages/icons/src/library' ,
706- src : '*.svg' ,
707- dest : WORKING_DIR + 'wp-includes/icons/library/' ,
708- } ,
709- ] ,
724+ files : [ {
725+ src : 'gutenberg/packages/icons/src/manifest.php' ,
726+ dest : WORKING_DIR + 'wp-includes/assets/icon-library-manifest.php' ,
727+ } ] ,
710728 } ,
711729 } ,
712730 sass : {
@@ -2052,14 +2070,59 @@ module.exports = function(grunt) {
20522070 } ) ;
20532071 } ) ;
20542072
2073+ grunt . registerTask ( 'routes:setup' , 'Reads the routes registry and configures the copy:routes task.' , function ( ) {
2074+ const registryPath = 'gutenberg/build/routes/registry.php' ;
2075+ let registryContent ;
2076+ try {
2077+ registryContent = fs . readFileSync ( registryPath , 'utf8' ) ;
2078+ } catch ( e ) {
2079+ grunt . fatal (
2080+ 'Route registry not found at ' + registryPath + '. Run `grunt gutenberg:download` first.'
2081+ ) ;
2082+ }
2083+ const namePattern = / ' n a m e ' \s * = > \s * ' ( [ ^ ' ] + ) ' / g;
2084+ const routeNames = [ ] ;
2085+ let match ;
2086+ while ( ( match = namePattern . exec ( registryContent ) ) !== null ) {
2087+ routeNames . push ( match [ 1 ] ) ;
2088+ }
2089+
2090+ if ( routeNames . length === 0 ) {
2091+ grunt . fatal (
2092+ 'No route names found in ' + registryPath + '. The format of the file may have changed.'
2093+ ) ;
2094+ }
2095+
2096+ const validName = / ^ [ A - Z a - z 0 - 9 _ - ] + $ / ;
2097+ routeNames . forEach ( function ( name ) {
2098+ if ( ! validName . test ( name ) ) {
2099+ grunt . fatal (
2100+ 'Invalid route name \'' + name + '\' in ' + registryPath + '. Expected only letters, digits, hyphens, and underscores.'
2101+ ) ;
2102+ }
2103+ } ) ;
2104+
2105+ grunt . config ( [ 'copy' , 'routes' , 'src' ] , [ 'routes/registry.php' ] . concat (
2106+ routeNames . flatMap ( function ( name ) {
2107+ return [
2108+ 'routes/' + name + '/**/*.php' ,
2109+ 'routes/' + name + '/**/*.js' ,
2110+ ] ;
2111+ } )
2112+ ) ) ;
2113+ } ) ;
2114+
20552115 grunt . registerTask ( 'build:gutenberg' , [
20562116 'copy:gutenberg-php' ,
2117+ 'routes:setup' ,
2118+ 'copy:routes' ,
20572119 'copy:gutenberg-js' ,
20582120 'gutenberg:copy' ,
20592121 'copy:gutenberg-modules' ,
20602122 'copy:gutenberg-styles' ,
20612123 'copy:gutenberg-theme-json' ,
2062- 'copy:gutenberg-icons' ,
2124+ 'copy:icon-library-images' ,
2125+ 'copy:icon-library-manifest' ,
20632126 ] ) ;
20642127
20652128 grunt . registerTask ( 'build' , function ( ) {
0 commit comments