Skip to content
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

Register runtime as dependency if found #48

Open
wants to merge 6 commits into
base: main
Choose a base branch
from
Open
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
24 changes: 23 additions & 1 deletion inc/namespace.php
Original file line number Diff line number Diff line change
Expand Up @@ -106,11 +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.


Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It's a small thing, but the double-spacing here is breaking linting.

// 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_asset_css ) {
// Ensure unique handle based on src.
$runtime_handle = 'runtime-' . hash( 'crc32', $runtime );
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,
Expand Down Expand Up @@ -154,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;
}

Expand Down