Skip to content

Commit e8a0aa5

Browse files
youknowriadclaude
andcommitted
Build: Improve Gutenberg integration workflow.
This commit improves the Gutenberg build integration in several ways: 1. Adds a new `gutenberg:sync` script that follows the same pattern as `install-changed` - storing a hash of the built ref in `.gutenberg-hash` and only rebuilding when the ref changes. This ensures that when the `gutenberg.ref` in package.json changes (e.g., after `git pull`), the next build will automatically rebuild Gutenberg. 2. Updates `postinstall` to run the full checkout + build + copy flow, so `npm install` produces a working development environment. 3. Adds conditional `file_exists()` checks around the PHP files from `wp-includes/build/` so WordPress doesn't fatal error if these files don't exist yet. 4. Restores Gutenberg's `package.json` after the build completes using `git checkout`, keeping the Gutenberg checkout clean. 5. Removes the `gutenberg:integrate` script and `gutenberg-integrate` grunt task since their functionality is now handled by `gutenberg:sync`. This simplifies the developer workflow and restores a similar flow to how package dependencies worked before - where `npm install` handles everything and subsequent builds are fast because they skip the already- built Gutenberg. See #64393. 🤖 Generated with [Claude Code](https://claude.com/claude-code) Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>
1 parent 470fcfe commit e8a0aa5

14 files changed

Lines changed: 235 additions & 21 deletions

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,7 @@ wp-tests-config.php
4343
/src/wp-includes/class-wp-block-parser-frame.php
4444
/src/wp-includes/theme.json
4545
/packagehash.txt
46+
/.gutenberg-hash
4647
/artifacts
4748
/setup.log
4849
/coverage

Gruntfile.js

Lines changed: 14 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1456,10 +1456,16 @@ module.exports = function(grunt) {
14561456
} );
14571457
} );
14581458

1459-
grunt.registerTask( 'gutenberg-integrate', 'Complete Gutenberg integration workflow.', [
1460-
'gutenberg-build',
1461-
'gutenberg-copy'
1462-
] );
1459+
grunt.registerTask( 'gutenberg-sync', 'Syncs Gutenberg checkout and build if ref has changed.', function() {
1460+
const done = this.async();
1461+
grunt.util.spawn( {
1462+
cmd: 'node',
1463+
args: [ 'tools/gutenberg/sync-gutenberg.js' ],
1464+
opts: { stdio: 'inherit' }
1465+
}, function( error ) {
1466+
done( ! error );
1467+
} );
1468+
} );
14631469

14641470
grunt.registerTask( 'copy-vendor-scripts', 'Copies vendor scripts from node_modules to wp-includes/js/dist/vendor/.', function() {
14651471
const done = this.async();
@@ -1894,7 +1900,8 @@ module.exports = function(grunt) {
18941900
grunt.task.run( [
18951901
'build:js',
18961902
'build:css',
1897-
'gutenberg-integrate',
1903+
'gutenberg-sync',
1904+
'gutenberg-copy',
18981905
'copy-vendor-scripts',
18991906
'build:certificates'
19001907
] );
@@ -1904,7 +1911,8 @@ module.exports = function(grunt) {
19041911
'build:files',
19051912
'build:js',
19061913
'build:css',
1907-
'gutenberg-integrate',
1914+
'gutenberg-sync',
1915+
'gutenberg-copy',
19081916
'copy-vendor-scripts',
19091917
'replace:source-maps',
19101918
'verify:build'

package.json

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -97,7 +97,7 @@
9797
"wicg-inert": "3.1.3"
9898
},
9999
"scripts": {
100-
"postinstall": "npm run gutenberg:checkout",
100+
"postinstall": "npm run gutenberg:sync && npm run gutenberg:copy -- --dev",
101101
"build": "grunt build",
102102
"build:dev": "grunt build --dev",
103103
"dev": "grunt watch --dev",
@@ -124,7 +124,7 @@
124124
"gutenberg:checkout": "node tools/gutenberg/checkout-gutenberg.js",
125125
"gutenberg:build": "node tools/gutenberg/build-gutenberg.js",
126126
"gutenberg:copy": "node tools/gutenberg/copy-gutenberg-build.js",
127-
"gutenberg:integrate": "npm run gutenberg:checkout && npm run gutenberg:build && npm run gutenberg:copy",
127+
"gutenberg:sync": "node tools/gutenberg/sync-gutenberg.js",
128128
"vendor:copy": "node tools/vendors/copy-vendors.js",
129129
"sync-gutenberg-packages": "grunt sync-gutenberg-packages",
130130
"postsync-gutenberg-packages": "grunt wp-packages:sync-stable-blocks && grunt build --dev && grunt build"

src/index.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@
1515
* Load the actual index.php file if the assets were already built.
1616
* Note: WPINC is not defined yet, it is defined later in wp-settings.php.
1717
*/
18-
if ( file_exists( ABSPATH . 'wp-includes/js/dist/edit-post.js' ) ) {
18+
if ( file_exists( ABSPATH . 'wp-includes/js/dist/edit-post.js' ) && file_exists( ABSPATH . 'wp-includes/build' ) ) {
1919
require_once ABSPATH . '_index.php';
2020
return;
2121
}

src/wp-admin/font-library.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@
2222
if ( ! function_exists( 'font_library_wp_admin_render_page' ) ) {
2323
wp_die(
2424
'<h1>' . __( 'Font Library is not available.' ) . '</h1>' .
25-
'<p>' . __( 'The Font Library requires Gutenberg integration. Please run <code>npm run gutenberg:integrate</code> to build the necessary files.' ) . '</p>',
25+
'<p>' . __( 'The Font Library requires Gutenberg build files. Please run <code>npm install</code> to build the necessary files.' ) . '</p>',
2626
503
2727
);
2828
}

src/wp-admin/index.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
* please refer to wp-admin/_index.php.
77
*/
88

9-
if ( file_exists( __DIR__ . '/../wp-includes/js/dist/edit-post.js' ) ) {
9+
if ( file_exists( __DIR__ . '/../wp-includes/js/dist/edit-post.js' ) && file_exists( __DIR__ . '/../wp-includes/build' ) ) {
1010
require_once __DIR__ . '/_index.php';
1111
return;
1212
}

src/wp-includes/blocks.php

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2421,6 +2421,10 @@ function parse_blocks( $content ) {
24212421
*/
24222422
$parser_class = apply_filters( 'block_parser_class', 'WP_Block_Parser' );
24232423

2424+
if ( ! class_exists( $parser_class ) ) {
2425+
return array();
2426+
}
2427+
24242428
$parser = new $parser_class();
24252429
return $parser->parse( $content );
24262430
}

src/wp-includes/blocks/index.php

Lines changed: 18 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -13,9 +13,15 @@
1313
define( 'BLOCKS_PATH', ABSPATH . WPINC . '/blocks/' );
1414

1515
// Include files required for core blocks registration.
16-
require BLOCKS_PATH . 'legacy-widget.php';
17-
require BLOCKS_PATH . 'widget-group.php';
18-
require BLOCKS_PATH . 'require-dynamic-blocks.php';
16+
if ( file_exists( BLOCKS_PATH . 'legacy-widget.php' ) ) {
17+
require BLOCKS_PATH . 'legacy-widget.php';
18+
}
19+
if ( file_exists( BLOCKS_PATH . 'widget-group.php' ) ) {
20+
require BLOCKS_PATH . 'widget-group.php';
21+
}
22+
if ( file_exists( BLOCKS_PATH . 'require-dynamic-blocks.php' ) ) {
23+
require BLOCKS_PATH . 'require-dynamic-blocks.php';
24+
}
1925

2026
/**
2127
* Registers core block style handles.
@@ -43,6 +49,9 @@ function register_core_block_style_handles() {
4349

4450
static $core_blocks_meta;
4551
if ( ! $core_blocks_meta ) {
52+
if ( ! file_exists( BLOCKS_PATH . 'blocks-json.php' ) ) {
53+
return;
54+
}
4655
$core_blocks_meta = require BLOCKS_PATH . 'blocks-json.php';
4756
}
4857

@@ -150,6 +159,9 @@ static function ( $file ) use ( $normalized_blocks_path ) {
150159
* @since 5.5.0
151160
*/
152161
function register_core_block_types_from_metadata() {
162+
if ( ! file_exists( BLOCKS_PATH . 'require-static-blocks.php' ) ) {
163+
return;
164+
}
153165
$block_folders = require BLOCKS_PATH . 'require-static-blocks.php';
154166
foreach ( $block_folders as $block_folder ) {
155167
register_block_type_from_metadata(
@@ -169,6 +181,9 @@ function register_core_block_types_from_metadata() {
169181
* @since 6.7.0
170182
*/
171183
function wp_register_core_block_metadata_collection() {
184+
if ( ! file_exists( BLOCKS_PATH . 'blocks-json.php' ) ) {
185+
return;
186+
}
172187
wp_register_block_metadata_collection(
173188
BLOCKS_PATH,
174189
BLOCKS_PATH . 'blocks-json.php'

src/wp-includes/formatting.php

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5227,6 +5227,11 @@ function wp_pre_kses_less_than_callback( $matches ) {
52275227
* @return string Filtered text to run through KSES.
52285228
*/
52295229
function wp_pre_kses_block_attributes( $content, $allowed_html, $allowed_protocols ) {
5230+
// If the block parser isn't available, skip block attribute filtering.
5231+
if ( ! class_exists( 'WP_Block_Parser' ) ) {
5232+
return $content;
5233+
}
5234+
52305235
/*
52315236
* `filter_block_content` is expected to call `wp_kses`. Temporarily remove
52325237
* the filter to avoid recursion.

src/wp-includes/script-loader.php

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -237,7 +237,8 @@ function wp_default_packages_scripts( $scripts ) {
237237
* 'annotations.js' => array('dependencies' => array(...), 'version' => '...'),
238238
* 'api-fetch.js' => array(...
239239
*/
240-
$assets = include ABSPATH . WPINC . "/assets/script-loader-packages{$suffix}.php";
240+
$assets_file = ABSPATH . WPINC . "/assets/script-loader-packages{$suffix}.php";
241+
$assets = file_exists( $assets_file ) ? include $assets_file : array();
241242

242243
foreach ( $assets as $file_name => $package_data ) {
243244
$basename = str_replace( $suffix . '.js', '', basename( $file_name ) );

0 commit comments

Comments
 (0)