Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 12 additions & 0 deletions src/wp-includes/script-loader.php
Original file line number Diff line number Diff line change
Expand Up @@ -2797,6 +2797,18 @@ function wp_enqueue_registered_block_scripts_and_styles() {
wp_enqueue_script( $script_handle );
}

/**
* Enqueue view script modules when block assets are loaded eagerly.
*
* This ensures parity with script and style handles, as modules are not
* automatically handled elsewhere when on-demand loading is disabled.
*/
if ( ! empty( $block_type->view_script_module_ids ) && function_exists( 'wp_enqueue_script_module' ) ) {
foreach ( $block_type->view_script_module_ids as $view_script_module_id ) {
wp_enqueue_script_module( $view_script_module_id );
}
}

if ( $load_editor_scripts_and_styles ) {
// Editor styles.
foreach ( $block_type->editor_style_handles as $editor_style_handle ) {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,78 @@
<?php
/**
* Tests for wp_enqueue_registered_block_scripts_and_styles().
*
* @package WordPress
* @subpackage Blocks
* @since 6.5.0
*
* @group blocks
*/

/**
* Tests for wp_enqueue_registered_block_scripts_and_styles().
*/
class Tests_Blocks_WpEnqueueRegisteredBlockScriptsAndStyles extends WP_UnitTestCase {

/**
* Original script modules instance.
*
* @var WP_Script_Modules
*/
private $original_script_modules;

/**
* Set up before each test.
*/
public function set_up() {
parent::set_up();
global $wp_script_modules;
$this->original_script_modules = $wp_script_modules;
$wp_script_modules = null;
}

/**
* Save the original script modules instance.
*/
public function tear_down() {
global $wp_script_modules;
$wp_script_modules = $this->original_script_modules;

$registry = WP_Block_Type_Registry::get_instance();
if ( $registry->is_registered( 'test/block' ) ) {
$registry->unregister( 'test/block' );
}

remove_filter( 'should_load_block_assets_on_demand', '__return_false' );

parent::tear_down();
}

/**
* Tests that view script modules are enqueued when block assets are not loaded on demand.
*
* @ticket 64812
*/
public function test_view_script_modules_enqueued_when_not_on_demand() {
add_filter( 'should_load_block_assets_on_demand', '__return_false' );

$module_id = 'test-module';

wp_register_script_module( $module_id, '/test.js' );

register_block_type(
'test/block',
array(
'view_script_module_ids' => array( $module_id ),
)
);

wp_enqueue_registered_block_scripts_and_styles();

$this->assertContains(
$module_id,
wp_script_modules()->get_queue(),
'Expected view script module to be enqueued.'
);
}
}
Loading