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

Correct css enqueuing logic #26

Merged
merged 3 commits into from
Jul 21, 2020
Merged
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
6 changes: 3 additions & 3 deletions inc/manifest.php
Original file line number Diff line number Diff line change
Expand Up @@ -106,10 +106,10 @@ function get_version( string $asset_uri, string $manifest_path ) : ?string {
if ( $manifest_hash ) {
$manifest_hashes[ $manifest_path ] = $manifest_hash;
return $manifest_hashes[ $manifest_path ];
} else {
// Set "null" to prevent trying to re-hash after hashing has failed once.
$manifest_hashes[ $manifest_path ] = null;
}
} else {
// Set "null" to prevent subsequent file_exists checks during this request.
$manifest_hashes[ $manifest_path ] = null;
}

// Finally, use the Altis deployment revision when available. This is a
Expand Down
53 changes: 30 additions & 23 deletions inc/namespace.php
Original file line number Diff line number Diff line change
Expand Up @@ -185,7 +185,7 @@ function( $script_key ) use ( $target_bundle ) {
$css_bundle = $build_path . preg_replace( '/\.js$/', '.css', $target_bundle );

// Production mode. Manually register script bundles.
if ( file_exists( $js_bundle ) ) {
if ( is_readable( $js_bundle ) ) {
wp_register_script(
$options['handle'],
Paths\get_file_uri( $js_bundle ),
Expand All @@ -196,7 +196,7 @@ function( $script_key ) use ( $target_bundle ) {
$registered['scripts'][] = $options['handle'];
}

if ( file_exists( $css_bundle ) ) {
if ( is_readable( $css_bundle ) ) {
wp_register_style(
$options['handle'],
Paths\get_file_uri( $css_bundle ),
Expand Down Expand Up @@ -310,11 +310,12 @@ function _register_or_update_script( string $handle, string $asset_uri, array $d
* @param string $manifest_path File system path for an asset manifest JSON file.
* @param string $target_asset Asset to retrieve within the specified manifest.
* @param array $options {
* @type string $handle Handle to use when enqueuing the asset. Required.
* @type string $handle Handle to use when enqueuing the asset. Optional.
* @type array $dependencies Script or Style dependencies. Optional.
* }
* @return array Array detailing which script and/or style handles got registered.
*/
function register_asset( string $manifest_path, string $target_asset, array $options = [] ) : void {
function register_asset( string $manifest_path, string $target_asset, array $options = [] ) : array {
$defaults = [
'dependencies' => [],
];
Expand All @@ -338,82 +339,88 @@ function register_asset( string $manifest_path, string $target_asset, array $opt

// If asset is not present in manifest, attempt to resolve the $target_asset
// relative to the folder containing the manifest file.
if ( empty( $asset_uri ) && file_exists( $manifest_folder . $target_asset ) ) {
// TODO: Consider checking file_exists( $manifest_folder . $target_asset )
if ( empty( $asset_uri ) ) {
// TODO: Consider checking is_readable( $manifest_folder . $target_asset )
// and warning (in console or error log) if it is not present on disk.
$asset_uri = $target_asset;
}

// Use the asset URI as the asset handle if no handle was provided.
if ( empty( $options['handle'] ) ) {
$options['handle'] = $asset_uri;
}

// Reconcile static asset build paths relative to the manifest's directory.
if ( strpos( $asset_uri, '//' ) === false ) {
$asset_uri = Paths\get_file_uri( $manifest_folder . $asset_uri );
}

// 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 );

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

if ( is_css( $asset_uri ) ) {
// Register a normal CSS bundle.
wp_register_style(
$options['handle'],
$asset_handle,
$asset_uri,
$options['dependencies'],
$asset_version
);
$handles['style'] = $asset_handle;
} elseif ( $is_js_style_fallback ) {
// We're registering a JS bundle when we originally asked for a CSS bundle.
// Register the JS, but if any dependencies were passed in, also register a
// dummy style bundle so that those style dependencies still get loaded.
Admin\maybe_setup_ssl_cert_error_handling( $asset_uri );
_register_or_update_script(
$options['handle'],
$asset_handle,
$asset_uri,
[],
$asset_version,
true
);
$handles['script'] = $asset_handle;
if ( ! empty( $options['dependencies'] ) ) {
wp_register_style(
$options['handle'],
$asset_handle,
false,
$options['dependencies'],
$asset_version
);
$handles['style'] = $asset_handle;
}
} else {
// Register a normal JS bundle.
Admin\maybe_setup_ssl_cert_error_handling( $asset_uri );
_register_or_update_script(
$options['handle'],
$asset_handle,
$asset_uri,
$options['dependencies'],
$asset_version,
true
);
$handles['script'] = $asset_handle;
}

return $handles;
}

/**
* Attempt to register and then enqueue a particular script bundle from a manifest.
* Register and immediately enqueue a particular asset within a manifest.
*
* @param string $manifest_path File system path for an asset manifest JSON file.
* @param string $target_asset Asset to retrieve within the specified manifest.
* @param array $options {
* @type string $handle Handle to use when enqueuing the asset. Required.
* @type string $handle Handle to use when enqueuing the asset. Optional.
* @type array $dependencies Script or Style dependencies. Optional.
* }
*/
function enqueue_asset( string $manifest_path, string $target_asset, array $options = [] ) : void {
register_asset( $manifest_path, $target_asset, $options );
$registered_handles = register_asset( $manifest_path, $target_asset, $options );

// $target_asset will share a filename extension with the enqueued asset.
if ( is_css( $target_asset ) ) {
wp_enqueue_style( $options['handle'] );
} else {
wp_enqueue_script( $options['handle'] );
if ( isset( $registered_handles['script'] ) ) {
wp_enqueue_script( $registered_handles['script'] );
}
if ( isset( $registered_handles['style'] ) ) {
wp_enqueue_style( $registered_handles['style'] );
}
}
Loading