-
Notifications
You must be signed in to change notification settings - Fork 4.3k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Block lazy loading #51778
base: trunk
Are you sure you want to change the base?
Block lazy loading #51778
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -57,3 +57,55 @@ | |
); | ||
} | ||
add_action( 'admin_menu', 'gutenberg_menu', 9 ); | ||
|
||
// disable loading and enqueuing block editor scripts and styles | ||
add_filter( 'should_load_block_editor_scripts_and_styles', '__return_false', 11 ); | ||
|
||
function get_block_importmap() { | ||
$block_registry = WP_Block_Type_Registry::get_instance(); | ||
$scripts = wp_scripts(); | ||
$styles = wp_styles(); | ||
$blocks = array(); | ||
|
||
foreach ( $block_registry->get_all_registered() as $block_name => $block_type ) { | ||
$imports = array(); | ||
if ( isset( $block_type->editor_script_handles ) ) { | ||
foreach ( $block_type->editor_script_handles as $handle ) { | ||
$spec = $scripts->registered[ $handle ]; | ||
$imports[] = array( | ||
'type' => 'script', | ||
'handle' => $spec->handle, | ||
'src' => $spec->src, | ||
'ver' => $spec->ver | ||
Check warning on line 79 in lib/init.php GitHub Actions / PHP coding standards
|
||
); | ||
} | ||
} | ||
if ( isset( $block_type->editor_style_handles ) ) { | ||
foreach ( $block_type->editor_style_handles as $handle ) { | ||
if ( ! isset( $styles->registered[ $handle ] ) ) { | ||
continue; | ||
} | ||
$spec = $styles->registered[ $handle ]; | ||
$imports[] = array( | ||
'type' => 'style', | ||
'handle' => $spec->handle, | ||
'src' => $spec->src, | ||
'ver' => $spec->ver | ||
Check warning on line 93 in lib/init.php GitHub Actions / PHP coding standards
|
||
); | ||
} | ||
} | ||
if ( ! empty( $imports ) ) { | ||
$blocks[ $block_name ] = $imports; | ||
} | ||
} | ||
|
||
return $blocks; | ||
} | ||
|
||
function emit_importmap() { | ||
wp_register_script( 'wp-importmap', ''); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Since we're calling |
||
wp_add_inline_script( 'wp-importmap', 'wp.importmap = ' . wp_json_encode( get_block_importmap() ) . ';' ); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Are we aiming to use webpack and not native ES modules and import maps? I'd love to hear what your thoughts are about this choice. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Same here, would love to understand why we're reinventing import maps? |
||
wp_enqueue_script('wp-importmap'); | ||
Check failure on line 108 in lib/init.php GitHub Actions / PHP coding standards
Check failure on line 108 in lib/init.php GitHub Actions / PHP coding standards
|
||
} | ||
|
||
add_action( 'enqueue_block_editor_assets', 'emit_importmap' ); | ||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
To connect the efforts. I was looking in WordPress/wordpress-develop#5118 at ways to allow referencing the same file path by multiple blocks by storing a handle name in the asset file to use it as a way to prevent registering the same file under different names. As of today, WP core would register the same script file under different handles because the name is generated based on the block name.