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 2 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
17 changes: 17 additions & 0 deletions inc/namespace.php
Original file line number Diff line number Diff line change
Expand Up @@ -107,10 +107,27 @@ 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.
mattheu marked this conversation as resolved.
Show resolved Hide resolved
$runtime = Manifest\get_manifest_resource( $manifest_path, 'runtime.js' );
if ( $runtime ) {
$runtime_handle = 'runtime-' . hash( 'crc32', $runtime ); // Ensure unique handle based on src.
mattheu marked this conversation as resolved.
Show resolved Hide resolved
wp_register_script( $runtime_handle, $runtime );
$options['dependencies'][] = $runtime_handle;
}

// Track registered handles so we can enqueue the correct assets later.
$handles = [];

if ( is_css( $asset_uri ) ) {
// Don't set runtime JS as dependency of a CSS file.
Copy link
Collaborator

Choose a reason for hiding this comment

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

Could we call is_css( $asset_uri ) again up above, and use that as the condition for looking up the runtime, instead of having to undo it later? Something like,

if ( $runtime && ! is_css( $asset_uri ) ) {

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,
Expand Down