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

fix(Assets) enqueue the minified version of a package if available #2096

Closed
wants to merge 4 commits into from
Closed
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
28 changes: 24 additions & 4 deletions src/Tribe/Assets.php
Original file line number Diff line number Diff line change
Expand Up @@ -347,17 +347,37 @@ public function register( $origin, $slug, $file, $deps = [], $action = null, $ar
return false;
}

$extension = substr( $file, strrpos( $file, '.' ) + 1 );
lucatume marked this conversation as resolved.
Show resolved Hide resolved
$plugin_path = ! empty( $origin->plugin_path ) ? $origin->plugin_path : $origin->pluginPath;
lucatume marked this conversation as resolved.
Show resolved Hide resolved

// Infer the type from the file extension, if not passed.
$type = empty( $arguments['type'] ) ?
substr( $file, strrpos( $file, '.' ) + 1 )
: $arguments['type'];
$type = empty( $arguments['type'] ) ? $extension : $arguments['type'];
lucatume marked this conversation as resolved.
Show resolved Hide resolved

// Try to enqueue the minified version of the file if not debugging scripts or the file is not available.
if ( ! str_contains( $file, '.min.' ) ) {
// From `something.js` to `something.min.js`.
$minified_file = substr( $file, 0, - ( strlen( $extension ) + 1 ) ) . '.min.' . $extension;
lucatume marked this conversation as resolved.
Show resolved Hide resolved
$min_file_abspath = $plugin_path . $minified_file;

if (
is_file( $min_file_abspath )
&& (
// The original file is not available.
! is_file( $file )
// Not in script debug mode.
|| ( ! defined( 'SCRIPT_DEBUG' ) || ! SCRIPT_DEBUG )
)
) {
$file = $minified_file;
}
}

// Work out the root path from the origin.
$root_path = str_replace(
dirname( WP_CONTENT_DIR ) ?: WP_CONTENT_DIR,
'',
// phpcs:ignore WordPress.NamingConventions.ValidVariableName.UsedPropertyNotSnakeCase
trailingslashit( ! empty( $origin->plugin_path ) ? $origin->plugin_path : $origin->pluginPath )
trailingslashit( $plugin_path )
);

// Fetches the version on the Origin Version constant if not passed.
Expand Down
Loading