From cc3b95fdeaab99efeeba964fbee2c5bd589680ce Mon Sep 17 00:00:00 2001 From: Weston Ruter Date: Sat, 16 Nov 2024 16:03:42 -0800 Subject: [PATCH 1/5] Add missing JS minification for new PL script --- .../performance-lab/includes/admin/load.php | 2 +- webpack.config.js | 35 +++++++++++++++++++ 2 files changed, 36 insertions(+), 1 deletion(-) diff --git a/plugins/performance-lab/includes/admin/load.php b/plugins/performance-lab/includes/admin/load.php index 26b9e9775e..b420fdc69b 100644 --- a/plugins/performance-lab/includes/admin/load.php +++ b/plugins/performance-lab/includes/admin/load.php @@ -228,7 +228,7 @@ function perflab_enqueue_features_page_scripts(): void { // Enqueue plugin activate AJAX script and localize script data. wp_enqueue_script( 'perflab-plugin-activate-ajax', - plugin_dir_url( PERFLAB_MAIN_FILE ) . 'includes/admin/plugin-activate-ajax.js', + plugin_dir_url( PERFLAB_MAIN_FILE ) . 'includes/admin/plugin-activate-ajax' . wp_scripts_get_suffix() . '.js', array( 'wp-i18n', 'wp-a11y', 'wp-api-fetch' ), PERFLAB_VERSION, true diff --git a/webpack.config.js b/webpack.config.js index 409f70c37b..2ed13745ca 100644 --- a/webpack.config.js +++ b/webpack.config.js @@ -35,12 +35,46 @@ const sharedConfig = { // Store plugins that require build process. const pluginsWithBuild = [ + 'performance-lab', 'embed-optimizer', 'image-prioritizer', 'optimization-detective', 'web-worker-offloading', ]; +/** + * Webpack Config: Performance Lab + * + * @param {*} env Webpack environment + * @return {Object} Webpack configuration + */ +const performanceLab = ( env ) => { + if ( env.plugin && env.plugin !== 'performance-lab' ) { + return defaultBuildConfig; + } + + const pluginDir = path.resolve( __dirname, 'plugins/performance-lab' ); + + return { + ...sharedConfig, + name: 'performance-lab', + plugins: [ + new CopyWebpackPlugin( { + patterns: [ + { + from: `${ pluginDir }/includes/admin/plugin-activate-ajax.js`, + to: `${ pluginDir }/includes/admin/plugin-activate-ajax.min.js`, + }, + ], + } ), + new WebpackBar( { + name: 'Building Performance Lab Assets', + color: '#2196f3', + } ), + ], + }; +}; + /** * Webpack Config: Embed Optimizer * @@ -286,6 +320,7 @@ const buildPlugin = ( env ) => { }; module.exports = [ + performanceLab, embedOptimizer, imagePrioritizer, optimizationDetective, From 1ca7c7740a69798f1c9095dbbc5743576dddfbf2 Mon Sep 17 00:00:00 2001 From: Weston Ruter Date: Tue, 19 Nov 2024 13:59:36 -0800 Subject: [PATCH 2/5] Introduce perflab_get_asset_src() --- .../performance-lab/includes/admin/load.php | 44 ++++++++++++++++++- 1 file changed, 43 insertions(+), 1 deletion(-) diff --git a/plugins/performance-lab/includes/admin/load.php b/plugins/performance-lab/includes/admin/load.php index b420fdc69b..3ca1ab0619 100644 --- a/plugins/performance-lab/includes/admin/load.php +++ b/plugins/performance-lab/includes/admin/load.php @@ -213,6 +213,48 @@ function perflab_dismiss_wp_pointer_wrapper(): void { } add_action( 'wp_ajax_dismiss-wp-pointer', 'perflab_dismiss_wp_pointer_wrapper', 0 ); +/** + * Gets the URL to a script or stylesheet. + * + * @since n.e.x.t + * + * @param string $src_path Source path. + * @param string|null $min_path Minified path. If not supplied, then '.min' is injected before the file extension in the source path. + * @return string URL to script or stylesheet. + */ +function perflab_get_asset_src( string $src_path, ?string $min_path = null ): string { + $dir_url = plugin_dir_url( PERFLAB_MAIN_FILE ); + + if ( null === $min_path ) { + // Note: wp_scripts_get_suffix() is not used here because we need access to both the source and minified paths. + $min_path = preg_replace( '/(?=\.\w+$)/', '.min', $src_path ); + } + + $force_src = false; + if ( + ( WP_DEBUG || wp_get_environment_type() === 'local' ) + && + ! file_exists( PERFLAB_PLUGIN_DIR_PATH . $min_path ) + ) { + $force_src = true; + wp_trigger_error( + __FUNCTION__, + sprintf( + /* translators: %s is the minified asset path */ + __( 'Minified asset has not been built: %s', 'performance-lab' ), + $min_path + ), + E_USER_WARNING + ); + } + + if ( SCRIPT_DEBUG || $force_src ) { + return $dir_url . $src_path; + } + + return $dir_url . $min_path; +} + /** * Callback function to handle admin scripts. * @@ -228,7 +270,7 @@ function perflab_enqueue_features_page_scripts(): void { // Enqueue plugin activate AJAX script and localize script data. wp_enqueue_script( 'perflab-plugin-activate-ajax', - plugin_dir_url( PERFLAB_MAIN_FILE ) . 'includes/admin/plugin-activate-ajax' . wp_scripts_get_suffix() . '.js', + perflab_get_asset_src( 'includes/admin/plugin-activate-ajax.js' ), array( 'wp-i18n', 'wp-a11y', 'wp-api-fetch' ), PERFLAB_VERSION, true From f54c4884215f6691d934462ca5aedd9820f20d9e Mon Sep 17 00:00:00 2001 From: Weston Ruter Date: Tue, 19 Nov 2024 14:04:05 -0800 Subject: [PATCH 3/5] Change perflab_get_asset_url() to return path instead of URL --- plugins/performance-lab/includes/admin/load.php | 14 ++++++-------- 1 file changed, 6 insertions(+), 8 deletions(-) diff --git a/plugins/performance-lab/includes/admin/load.php b/plugins/performance-lab/includes/admin/load.php index 3ca1ab0619..942c92c1ac 100644 --- a/plugins/performance-lab/includes/admin/load.php +++ b/plugins/performance-lab/includes/admin/load.php @@ -214,7 +214,7 @@ function perflab_dismiss_wp_pointer_wrapper(): void { add_action( 'wp_ajax_dismiss-wp-pointer', 'perflab_dismiss_wp_pointer_wrapper', 0 ); /** - * Gets the URL to a script or stylesheet. + * Gets the path to a script or stylesheet. * * @since n.e.x.t * @@ -222,12 +222,10 @@ function perflab_dismiss_wp_pointer_wrapper(): void { * @param string|null $min_path Minified path. If not supplied, then '.min' is injected before the file extension in the source path. * @return string URL to script or stylesheet. */ -function perflab_get_asset_src( string $src_path, ?string $min_path = null ): string { - $dir_url = plugin_dir_url( PERFLAB_MAIN_FILE ); - +function perflab_get_asset_path( string $src_path, ?string $min_path = null ): string { if ( null === $min_path ) { // Note: wp_scripts_get_suffix() is not used here because we need access to both the source and minified paths. - $min_path = preg_replace( '/(?=\.\w+$)/', '.min', $src_path ); + $min_path = (string) preg_replace( '/(?=\.\w+$)/', '.min', $src_path ); } $force_src = false; @@ -249,10 +247,10 @@ function perflab_get_asset_src( string $src_path, ?string $min_path = null ): st } if ( SCRIPT_DEBUG || $force_src ) { - return $dir_url . $src_path; + return $src_path; } - return $dir_url . $min_path; + return $min_path; } /** @@ -270,7 +268,7 @@ function perflab_enqueue_features_page_scripts(): void { // Enqueue plugin activate AJAX script and localize script data. wp_enqueue_script( 'perflab-plugin-activate-ajax', - perflab_get_asset_src( 'includes/admin/plugin-activate-ajax.js' ), + plugin_dir_url( PERFLAB_MAIN_FILE ) . perflab_get_asset_path( 'includes/admin/plugin-activate-ajax.js' ), array( 'wp-i18n', 'wp-a11y', 'wp-api-fetch' ), PERFLAB_VERSION, true From be0401c22613beb9f719770fc71372467dfd80b2 Mon Sep 17 00:00:00 2001 From: Weston Ruter Date: Tue, 19 Nov 2024 16:48:22 -0800 Subject: [PATCH 4/5] Eliminate local env check --- plugins/performance-lab/includes/admin/load.php | 6 +----- 1 file changed, 1 insertion(+), 5 deletions(-) diff --git a/plugins/performance-lab/includes/admin/load.php b/plugins/performance-lab/includes/admin/load.php index 942c92c1ac..0f0fb3992b 100644 --- a/plugins/performance-lab/includes/admin/load.php +++ b/plugins/performance-lab/includes/admin/load.php @@ -229,11 +229,7 @@ function perflab_get_asset_path( string $src_path, ?string $min_path = null ): s } $force_src = false; - if ( - ( WP_DEBUG || wp_get_environment_type() === 'local' ) - && - ! file_exists( PERFLAB_PLUGIN_DIR_PATH . $min_path ) - ) { + if ( WP_DEBUG && ! file_exists( PERFLAB_PLUGIN_DIR_PATH . $min_path ) ) { $force_src = true; wp_trigger_error( __FUNCTION__, From 5509f2cf1735917db8bcfec7bd2a93b6c879d996 Mon Sep 17 00:00:00 2001 From: Weston Ruter Date: Tue, 19 Nov 2024 17:22:49 -0800 Subject: [PATCH 5/5] Update other plugins to warn when minified asset is missing --- plugins/embed-optimizer/hooks.php | 40 ++++++++++++++++++- plugins/image-prioritizer/helper.php | 36 +++++++++++++++++ plugins/image-prioritizer/hooks.php | 3 +- plugins/optimization-detective/detection.php | 2 +- plugins/optimization-detective/helper.php | 36 +++++++++++++++++ .../performance-lab/includes/admin/load.php | 2 +- tools/phpstan/constants.php | 2 + 7 files changed, 116 insertions(+), 5 deletions(-) diff --git a/plugins/embed-optimizer/hooks.php b/plugins/embed-optimizer/hooks.php index 452794d030..99c8225961 100644 --- a/plugins/embed-optimizer/hooks.php +++ b/plugins/embed-optimizer/hooks.php @@ -121,7 +121,7 @@ function embed_optimizer_filter_extension_module_urls( $extension_module_urls ): if ( ! is_array( $extension_module_urls ) ) { $extension_module_urls = array(); } - $extension_module_urls[] = add_query_arg( 'ver', EMBED_OPTIMIZER_VERSION, plugin_dir_url( __FILE__ ) . sprintf( 'detect%s.js', wp_scripts_get_suffix() ) ); + $extension_module_urls[] = add_query_arg( 'ver', EMBED_OPTIMIZER_VERSION, plugin_dir_url( __FILE__ ) . embed_optimizer_get_asset_path( 'detect.js' ) ); return $extension_module_urls; } @@ -326,7 +326,7 @@ function embed_optimizer_lazy_load_scripts(): void { * @since 0.2.0 */ function embed_optimizer_get_lazy_load_script(): string { - $script = file_get_contents( __DIR__ . sprintf( '/lazy-load%s.js', wp_scripts_get_suffix() ) ); // phpcs:ignore WordPress.WP.AlternativeFunctions.file_get_contents_file_get_contents -- It's a local filesystem path not a remote request. + $script = file_get_contents( __DIR__ . '/' . embed_optimizer_get_asset_path( 'lazy-load.js' ) ); // phpcs:ignore WordPress.WP.AlternativeFunctions.file_get_contents_file_get_contents -- It's a local filesystem path not a remote request. if ( false === $script ) { return ''; @@ -424,3 +424,39 @@ function embed_optimizer_render_generator(): void { // Use the plugin slug as it is immutable. echo '' . "\n"; } + +/** + * Gets the path to a script or stylesheet. + * + * @since n.e.x.t + * + * @param string $src_path Source path, relative to plugin root. + * @param string|null $min_path Minified path. If not supplied, then '.min' is injected before the file extension in the source path. + * @return string URL to script or stylesheet. + */ +function embed_optimizer_get_asset_path( string $src_path, ?string $min_path = null ): string { + if ( null === $min_path ) { + // Note: wp_scripts_get_suffix() is not used here because we need access to both the source and minified paths. + $min_path = (string) preg_replace( '/(?=\.\w+$)/', '.min', $src_path ); + } + + $force_src = false; + if ( WP_DEBUG && ! file_exists( trailingslashit( __DIR__ ) . $min_path ) ) { + $force_src = true; + wp_trigger_error( + __FUNCTION__, + sprintf( + /* translators: %s is the minified asset path */ + __( 'Minified asset has not been built: %s', 'embed-optimizer' ), + $min_path + ), + E_USER_WARNING + ); + } + + if ( SCRIPT_DEBUG || $force_src ) { + return $src_path; + } + + return $min_path; +} diff --git a/plugins/image-prioritizer/helper.php b/plugins/image-prioritizer/helper.php index 9096fac63b..025e07d4c6 100644 --- a/plugins/image-prioritizer/helper.php +++ b/plugins/image-prioritizer/helper.php @@ -76,3 +76,39 @@ function image_prioritizer_register_tag_visitors( OD_Tag_Visitor_Registry $regis $video_visitor = new Image_Prioritizer_Video_Tag_Visitor(); $registry->register( 'image-prioritizer/video', $video_visitor ); } + +/** + * Gets the path to a script or stylesheet. + * + * @since n.e.x.t + * + * @param string $src_path Source path, relative to plugin root. + * @param string|null $min_path Minified path. If not supplied, then '.min' is injected before the file extension in the source path. + * @return string URL to script or stylesheet. + */ +function image_prioritizer_get_asset_path( string $src_path, ?string $min_path = null ): string { + if ( null === $min_path ) { + // Note: wp_scripts_get_suffix() is not used here because we need access to both the source and minified paths. + $min_path = (string) preg_replace( '/(?=\.\w+$)/', '.min', $src_path ); + } + + $force_src = false; + if ( WP_DEBUG && ! file_exists( trailingslashit( __DIR__ ) . $min_path ) ) { + $force_src = true; + wp_trigger_error( + __FUNCTION__, + sprintf( + /* translators: %s is the minified asset path */ + __( 'Minified asset has not been built: %s', 'image-prioritizer' ), + $min_path + ), + E_USER_WARNING + ); + } + + if ( SCRIPT_DEBUG || $force_src ) { + return $src_path; + } + + return $min_path; +} diff --git a/plugins/image-prioritizer/hooks.php b/plugins/image-prioritizer/hooks.php index 9e962ce8f1..3e16f3e9ae 100644 --- a/plugins/image-prioritizer/hooks.php +++ b/plugins/image-prioritizer/hooks.php @@ -22,7 +22,8 @@ * @since 0.2.0 */ function image_prioritizer_get_lazy_load_script(): string { - $script = file_get_contents( __DIR__ . sprintf( '/lazy-load%s.js', wp_scripts_get_suffix() ) ); // phpcs:ignore WordPress.WP.AlternativeFunctions.file_get_contents_file_get_contents -- It's a local filesystem path not a remote request. + $path = image_prioritizer_get_asset_path( 'lazy-load.js' ); + $script = file_get_contents( __DIR__ . '/' . $path ); // phpcs:ignore WordPress.WP.AlternativeFunctions.file_get_contents_file_get_contents -- It's a local filesystem path not a remote request. if ( false === $script ) { return ''; diff --git a/plugins/optimization-detective/detection.php b/plugins/optimization-detective/detection.php index b52f407deb..63d06d2be3 100644 --- a/plugins/optimization-detective/detection.php +++ b/plugins/optimization-detective/detection.php @@ -112,7 +112,7 @@ static function ( OD_URL_Metric_Group $group ): array { return wp_get_inline_script_tag( sprintf( 'import detect from %s; detect( %s );', - wp_json_encode( add_query_arg( 'ver', OPTIMIZATION_DETECTIVE_VERSION, plugin_dir_url( __FILE__ ) . sprintf( 'detect%s.js', wp_scripts_get_suffix() ) ) ), + wp_json_encode( add_query_arg( 'ver', OPTIMIZATION_DETECTIVE_VERSION, plugin_dir_url( __FILE__ ) . od_get_asset_path( 'detect.js' ) ) ), wp_json_encode( $detect_args ) ), array( 'type' => 'module' ) diff --git a/plugins/optimization-detective/helper.php b/plugins/optimization-detective/helper.php index 3573d150fe..2920ad50cb 100644 --- a/plugins/optimization-detective/helper.php +++ b/plugins/optimization-detective/helper.php @@ -64,3 +64,39 @@ function od_render_generator_meta_tag(): void { // Use the plugin slug as it is immutable. echo '' . "\n"; } + +/** + * Gets the path to a script or stylesheet. + * + * @since n.e.x.t + * + * @param string $src_path Source path, relative to plugin root. + * @param string|null $min_path Minified path. If not supplied, then '.min' is injected before the file extension in the source path. + * @return string URL to script or stylesheet. + */ +function od_get_asset_path( string $src_path, ?string $min_path = null ): string { + if ( null === $min_path ) { + // Note: wp_scripts_get_suffix() is not used here because we need access to both the source and minified paths. + $min_path = (string) preg_replace( '/(?=\.\w+$)/', '.min', $src_path ); + } + + $force_src = false; + if ( WP_DEBUG && ! file_exists( trailingslashit( __DIR__ ) . $min_path ) ) { + $force_src = true; + wp_trigger_error( + __FUNCTION__, + sprintf( + /* translators: %s is the minified asset path */ + __( 'Minified asset has not been built: %s', 'optimization-detective' ), + $min_path + ), + E_USER_WARNING + ); + } + + if ( SCRIPT_DEBUG || $force_src ) { + return $src_path; + } + + return $min_path; +} diff --git a/plugins/performance-lab/includes/admin/load.php b/plugins/performance-lab/includes/admin/load.php index 0f0fb3992b..8416f3e74a 100644 --- a/plugins/performance-lab/includes/admin/load.php +++ b/plugins/performance-lab/includes/admin/load.php @@ -229,7 +229,7 @@ function perflab_get_asset_path( string $src_path, ?string $min_path = null ): s } $force_src = false; - if ( WP_DEBUG && ! file_exists( PERFLAB_PLUGIN_DIR_PATH . $min_path ) ) { + if ( WP_DEBUG && ! file_exists( trailingslashit( PERFLAB_PLUGIN_DIR_PATH ) . $min_path ) ) { $force_src = true; wp_trigger_error( __FUNCTION__, diff --git a/tools/phpstan/constants.php b/tools/phpstan/constants.php index d740676c88..ae469bf4dc 100644 --- a/tools/phpstan/constants.php +++ b/tools/phpstan/constants.php @@ -17,3 +17,5 @@ define( 'IMAGE_PRIORITIZER_VERSION', '0.0.0' ); define( 'EMBED_OPTIMIZER_VERSION', '0.0.0' ); + +define( 'PERFLAB_PLUGIN_DIR_PATH', __DIR__ . '/../../plugins/performance-lab/' );