Skip to content

Commit e626725

Browse files
committed
Build: Improve Gutenberg integration workflow.
This changeset improves the Gutenberg build integration to simplify the developer workflow and reinstore a flow similar to how package dependencies worked before the Gutenberg checkout-and-build approach was introduced. Key improvements: * Automatic rebuild on ref change: Adds a new `gutenberg:sync` script that stores a hash of the built ref in `.gutenberg-hash` and only rebuilds when the ref changes. * Full integration on `npm install`: Running `npm install` now produces a fully working development environment with Gutenberg assets in `src/`. * Clean Gutenberg checkout: Restores Gutenberg's `package.json` after the build completes. * Stops copying `.js.map` files to `wp-includes/js/dist` since they reference non-existent paths. * Remove package.json files from the build folder. * Avoid closures and use prefixed functions. * Updates build checks to use `jquery.js` instead of `edit-post.js` as the build indicator. Props youknowriad, ellatrix, mcsf, dmsnell, ntsekouras, jorgefilipecosta, tobiasbg, peterwilsoncc. Fixes #64393. git-svn-id: https://develop.svn.wordpress.org/trunk@61492 602fd350-edb4-49c9-b593-d223f7449a82
1 parent bee3356 commit e626725

File tree

15 files changed

+257
-61
lines changed

15 files changed

+257
-61
lines changed

.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
@@ -1458,10 +1458,16 @@ module.exports = function(grunt) {
14581458
} );
14591459
} );
14601460

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

14661472
grunt.registerTask( 'copy-vendor-scripts', 'Copies vendor scripts from node_modules to wp-includes/js/dist/vendor/.', function() {
14671473
const done = this.async();
@@ -1896,7 +1902,8 @@ module.exports = function(grunt) {
18961902
grunt.task.run( [
18971903
'build:js',
18981904
'build:css',
1899-
'gutenberg-integrate',
1905+
'gutenberg-sync',
1906+
'gutenberg-copy',
19001907
'copy-vendor-scripts',
19011908
'build:certificates'
19021909
] );
@@ -1906,7 +1913,8 @@ module.exports = function(grunt) {
19061913
'build:files',
19071914
'build:js',
19081915
'build:css',
1909-
'gutenberg-integrate',
1916+
'gutenberg-sync',
1917+
'gutenberg-copy',
19101918
'copy-vendor-scripts',
19111919
'replace:source-maps',
19121920
'verify:build'

package.json

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
"url": "https://develop.svn.wordpress.org/trunk"
88
},
99
"gutenberg": {
10-
"ref": "892bfad51d2261f44f3a21f934b1c72bd29a2449"
10+
"ref": "7bf80ea84eb8b62eceb1bb3fe82e42163673ca79"
1111
},
1212
"engines": {
1313
"node": ">=20.10.0",
@@ -99,7 +99,7 @@
9999
"wicg-inert": "3.1.3"
100100
},
101101
"scripts": {
102-
"postinstall": "npm run gutenberg:checkout",
102+
"postinstall": "npm run gutenberg:sync && npm run gutenberg:copy -- --dev",
103103
"build": "grunt build",
104104
"build:dev": "grunt build --dev",
105105
"dev": "grunt watch --dev",
@@ -126,7 +126,7 @@
126126
"gutenberg:checkout": "node tools/gutenberg/checkout-gutenberg.js",
127127
"gutenberg:build": "node tools/gutenberg/build-gutenberg.js",
128128
"gutenberg:copy": "node tools/gutenberg/copy-gutenberg-build.js",
129-
"gutenberg:integrate": "npm run gutenberg:checkout && npm run gutenberg:build && npm run gutenberg:copy",
129+
"gutenberg:sync": "node tools/gutenberg/sync-gutenberg.js",
130130
"vendor:copy": "node tools/vendors/copy-vendors.js",
131131
"sync-gutenberg-packages": "grunt sync-gutenberg-packages",
132132
"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/jquery/jquery.js' ) && is_dir( ABSPATH . 'wp-includes/build' ) ) {
1919
require_once ABSPATH . '_index.php';
2020
return;
2121
}

src/wp-admin/font-library.php

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -19,10 +19,10 @@
1919
}
2020

2121
// Check if Gutenberg build files are available
22-
if ( ! function_exists( 'font_library_wp_admin_render_page' ) ) {
22+
if ( ! function_exists( 'wp_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
}
@@ -33,6 +33,6 @@
3333
require_once ABSPATH . 'wp-admin/admin-header.php';
3434

3535
// Render the Font Library page
36-
font_library_wp_admin_render_page();
36+
wp_font_library_wp_admin_render_page();
3737

3838
require_once ABSPATH . 'wp-admin/admin-footer.php';

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/jquery/jquery.js' ) && is_dir( __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
@@ -281,7 +281,8 @@ function wp_default_packages_scripts( $scripts ) {
281281
* 'annotations.js' => array('dependencies' => array(...), 'version' => '...'),
282282
* 'api-fetch.js' => array(...
283283
*/
284-
$assets = include ABSPATH . WPINC . "/assets/script-loader-packages{$suffix}.php";
284+
$assets_file = ABSPATH . WPINC . "/assets/script-loader-packages{$suffix}.php";
285+
$assets = file_exists( $assets_file ) ? include $assets_file : array();
285286

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

0 commit comments

Comments
 (0)