From 9b529e9401f43c00d2fa53d4c5511bfd530553ac Mon Sep 17 00:00:00 2001 From: Matthew Haines-Young Date: Thu, 11 Aug 2022 17:43:42 +0100 Subject: [PATCH 1/6] Register runtime as dependency if found --- inc/namespace.php | 9 +++++++++ 1 file changed, 9 insertions(+) diff --git a/inc/namespace.php b/inc/namespace.php index 61f786f..262af45 100644 --- a/inc/namespace.php +++ b/inc/namespace.php @@ -107,6 +107,15 @@ function register_asset( string $manifest_path, string $target_asset, array $opt $asset_handle = $options['handle'] ?? $target_asset; $asset_version = Manifest\get_version( $asset_uri, $manifest_path ); + // If running the development build with runtimeChunk: single, a runtime file will be present in the manifest. + // Register this and ensure it is loaded only once per page. + $runtime = Manifest\get_manifest_resource( $manifest_path, 'runtime.js' ); + if ( $runtime ) { + $runtime_handle = 'runtime-' . hash( 'crc32', $runtime ); // Ensure unique handle based on src. + wp_register_script( $runtime_handle, $runtime ); + $options['dependencies'][] = $runtime_handle; + } + // Track registered handles so we can enqueue the correct assets later. $handles = []; From 4eb04d3fb2bd62311f0c072d1789f53797f3d6c1 Mon Sep 17 00:00:00 2001 From: Matthew Haines-Young Date: Thu, 11 Aug 2022 18:00:29 +0100 Subject: [PATCH 2/6] Prevent runtime set as dep for CSS --- inc/namespace.php | 8 ++++++++ 1 file changed, 8 insertions(+) diff --git a/inc/namespace.php b/inc/namespace.php index 262af45..d08d112 100644 --- a/inc/namespace.php +++ b/inc/namespace.php @@ -120,6 +120,14 @@ function register_asset( string $manifest_path, string $target_asset, array $opt $handles = []; if ( is_css( $asset_uri ) ) { + // Don't set runtime JS as dependency of a CSS file. + if ( isset( $runtime_handle ) ) { + $key = array_search( $runtime_handle, $options['dependencies'] ); + if ( $key !== false ) { + unset( $options['dependencies'][ $key ] ); + } + } + // Register a normal CSS bundle. wp_register_style( $asset_handle, From fa5c7fec2350890678db026bead115b5177974c7 Mon Sep 17 00:00:00 2001 From: Matthew Haines-Young Date: Mon, 15 Aug 2022 16:52:32 +0100 Subject: [PATCH 3/6] Max comment line length Co-authored-by: K Adam White --- inc/namespace.php | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/inc/namespace.php b/inc/namespace.php index d08d112..02078c9 100644 --- a/inc/namespace.php +++ b/inc/namespace.php @@ -107,8 +107,9 @@ function register_asset( string $manifest_path, string $target_asset, array $opt $asset_handle = $options['handle'] ?? $target_asset; $asset_version = Manifest\get_version( $asset_uri, $manifest_path ); - // If running the development build with runtimeChunk: single, a runtime file will be present in the manifest. - // Register this and ensure it is loaded only once per page. + // If running the development build with runtimeChunk: single, a runtime + // file will be present in the manifest. Register this and ensure it is + // loaded only once per page. $runtime = Manifest\get_manifest_resource( $manifest_path, 'runtime.js' ); if ( $runtime ) { $runtime_handle = 'runtime-' . hash( 'crc32', $runtime ); // Ensure unique handle based on src. From 94a3c6c860951d281d0f8c32313557885d6464e0 Mon Sep 17 00:00:00 2001 From: Matthew Haines-Young Date: Mon, 15 Aug 2022 16:54:03 +0100 Subject: [PATCH 4/6] Line length Co-authored-by: K Adam White --- inc/namespace.php | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/inc/namespace.php b/inc/namespace.php index 02078c9..ee8bfdd 100644 --- a/inc/namespace.php +++ b/inc/namespace.php @@ -112,7 +112,8 @@ function register_asset( string $manifest_path, string $target_asset, array $opt // loaded only once per page. $runtime = Manifest\get_manifest_resource( $manifest_path, 'runtime.js' ); if ( $runtime ) { - $runtime_handle = 'runtime-' . hash( 'crc32', $runtime ); // Ensure unique handle based on src. + // Ensure unique handle based on src. + $runtime_handle = 'runtime-' . hash( 'crc32', $runtime ); wp_register_script( $runtime_handle, $runtime ); $options['dependencies'][] = $runtime_handle; } From 7ff3ade6e2929a0256748b6909dee9df1ad60aa3 Mon Sep 17 00:00:00 2001 From: Matthew Haines-Young Date: Mon, 15 Aug 2022 16:55:42 +0100 Subject: [PATCH 5/6] Simplify by checking not CSS instead of undoing --- inc/namespace.php | 10 +--------- 1 file changed, 1 insertion(+), 9 deletions(-) diff --git a/inc/namespace.php b/inc/namespace.php index ee8bfdd..838a388 100644 --- a/inc/namespace.php +++ b/inc/namespace.php @@ -111,7 +111,7 @@ function register_asset( string $manifest_path, string $target_asset, array $opt // file will be present in the manifest. Register this and ensure it is // loaded only once per page. $runtime = Manifest\get_manifest_resource( $manifest_path, 'runtime.js' ); - if ( $runtime ) { + if ( $runtime && ! is_css( $asset_uri ) ) { // Ensure unique handle based on src. $runtime_handle = 'runtime-' . hash( 'crc32', $runtime ); wp_register_script( $runtime_handle, $runtime ); @@ -122,14 +122,6 @@ function register_asset( string $manifest_path, string $target_asset, array $opt $handles = []; if ( is_css( $asset_uri ) ) { - // Don't set runtime JS as dependency of a CSS file. - if ( isset( $runtime_handle ) ) { - $key = array_search( $runtime_handle, $options['dependencies'] ); - if ( $key !== false ) { - unset( $options['dependencies'][ $key ] ); - } - } - // Register a normal CSS bundle. wp_register_style( $asset_handle, From 73528f0c64f2dab04fc3ab56b721620902738dc4 Mon Sep 17 00:00:00 2001 From: Matthew Haines-Young Date: Fri, 26 Aug 2022 16:30:42 +0100 Subject: [PATCH 6/6] Handle style fallback runtime --- inc/namespace.php | 19 +++++++++++++++---- 1 file changed, 15 insertions(+), 4 deletions(-) diff --git a/inc/namespace.php b/inc/namespace.php index 838a388..4818555 100644 --- a/inc/namespace.php +++ b/inc/namespace.php @@ -106,22 +106,25 @@ function register_asset( string $manifest_path, string $target_asset, array $opt // Use the requested asset as the asset handle if no handle was provided. $asset_handle = $options['handle'] ?? $target_asset; $asset_version = Manifest\get_version( $asset_uri, $manifest_path ); + $is_asset_css = is_css( $asset_uri ); // Note returns false for the CSS dev JS fallback. + // If running the development build with runtimeChunk: single, a runtime // file will be present in the manifest. Register this and ensure it is // loaded only once per page. $runtime = Manifest\get_manifest_resource( $manifest_path, 'runtime.js' ); - if ( $runtime && ! is_css( $asset_uri ) ) { + if ( $runtime && ! $is_asset_css ) { // Ensure unique handle based on src. $runtime_handle = 'runtime-' . hash( 'crc32', $runtime ); - wp_register_script( $runtime_handle, $runtime ); - $options['dependencies'][] = $runtime_handle; + if ( ! wp_script_is( $runtime_handle, 'registered' ) ) { + wp_register_script( $runtime_handle, $runtime ); + } } // Track registered handles so we can enqueue the correct assets later. $handles = []; - if ( is_css( $asset_uri ) ) { + if ( $is_asset_css ) { // Register a normal CSS bundle. wp_register_style( $asset_handle, @@ -165,6 +168,14 @@ function register_asset( string $manifest_path, string $target_asset, array $opt $handles['script'] = $asset_handle; } + // Add dependency after registration to work around HM Asset loader not setting dependencies on JS fallback. + if ( $runtime && ! $is_asset_css ) { + $script = wp_scripts()->query( $asset_handle, 'registered' ); + if ( $script && ! in_array( $runtime_handle, $script->deps, true ) ) { + $script->deps[] = $runtime_handle; + } + } + return $handles; }