From a15db8762b100a71692c952e9ea9e6cd7ccbaed8 Mon Sep 17 00:00:00 2001 From: Shyamsundar Gadde <73636812+ShyamGadde@users.noreply.github.com> Date: Mon, 11 Nov 2024 04:52:43 +0530 Subject: [PATCH 01/16] Skip minification when creating the production build --- webpack.config.js | 1 + 1 file changed, 1 insertion(+) diff --git a/webpack.config.js b/webpack.config.js index 49f965ab45..c2c7d2ccbf 100644 --- a/webpack.config.js +++ b/webpack.config.js @@ -164,6 +164,7 @@ const buildPlugin = ( env ) => { { from, to, + info: { minimized: true }, // Terser skip minimization globOptions: { dot: true, ignore: [ From 9ba7400417082d1e2098b1f8e3ab10b6d444212a Mon Sep 17 00:00:00 2001 From: Shyamsundar Gadde <73636812+ShyamGadde@users.noreply.github.com> Date: Mon, 11 Nov 2024 04:54:44 +0530 Subject: [PATCH 02/16] Create minified script files when building Optimization Detective Assets --- .gitignore | 1 + webpack.config.js | 10 +++++++--- 2 files changed, 8 insertions(+), 3 deletions(-) diff --git a/.gitignore b/.gitignore index 39362b3482..fc2e0f7d09 100644 --- a/.gitignore +++ b/.gitignore @@ -24,6 +24,7 @@ nbproject/ build .wp-env.override.json +*.min.js *.asset.php ############ diff --git a/webpack.config.js b/webpack.config.js index c2c7d2ccbf..d43a1c6087 100644 --- a/webpack.config.js +++ b/webpack.config.js @@ -50,7 +50,7 @@ const optimizationDetective = ( env ) => { const source = path.resolve( __dirname, 'node_modules/web-vitals' ); const destination = path.resolve( __dirname, - 'plugins/optimization-detective/build' + 'plugins/optimization-detective' ); return { @@ -61,16 +61,20 @@ const optimizationDetective = ( env ) => { patterns: [ { from: `${ source }/dist/web-vitals.js`, - to: `${ destination }/web-vitals.js`, + to: `${ destination }/build/web-vitals.js`, }, { from: `${ source }/package.json`, - to: `${ destination }/web-vitals.asset.php`, + to: `${ destination }/build/web-vitals.asset.php`, transform: { transformer: assetDataTransformer, cache: false, }, }, + { + from: `${ destination }/detect.js`, + to: `${ destination }/detect.min.js`, + }, ], } ), new WebpackBar( { From 236c61ffa5003fc7a45ea4967d55c68b44a381b0 Mon Sep 17 00:00:00 2001 From: Shyamsundar Gadde <73636812+ShyamGadde@users.noreply.github.com> Date: Mon, 11 Nov 2024 04:55:37 +0530 Subject: [PATCH 03/16] Use unminified dev version of scripts when `SCRIPT_DEBUG` is enabled --- plugins/optimization-detective/detection.php | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-) diff --git a/plugins/optimization-detective/detection.php b/plugins/optimization-detective/detection.php index 7d18ae8374..83ba4fa3da 100644 --- a/plugins/optimization-detective/detection.php +++ b/plugins/optimization-detective/detection.php @@ -76,10 +76,15 @@ static function ( OD_URL_Metric_Group $group ): array { $detect_args['urlMetricGroupCollection'] = $group_collection; } + $detection_script_file = 'detect.min.js'; + if ( defined( 'SCRIPT_DEBUG' ) && SCRIPT_DEBUG ) { + $detection_script_file = 'detect.js'; + } + 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__ ) . 'detect.js' ) ), + wp_json_encode( add_query_arg( 'ver', OPTIMIZATION_DETECTIVE_VERSION, plugin_dir_url( __FILE__ ) . $detection_script_file ) ), wp_json_encode( $detect_args ) ), array( 'type' => 'module' ) From b686515bda71c3b75ca7630bd3fdce03ca5539c8 Mon Sep 17 00:00:00 2001 From: Shyamsundar Gadde <73636812+ShyamGadde@users.noreply.github.com> Date: Mon, 11 Nov 2024 11:50:09 +0530 Subject: [PATCH 04/16] Remove redundant comment --- webpack.config.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/webpack.config.js b/webpack.config.js index d43a1c6087..c0bd3793aa 100644 --- a/webpack.config.js +++ b/webpack.config.js @@ -168,7 +168,7 @@ const buildPlugin = ( env ) => { { from, to, - info: { minimized: true }, // Terser skip minimization + info: { minimized: true }, globOptions: { dot: true, ignore: [ From a3ab16ac0561558bfdaee26b66ccf5097febbaf0 Mon Sep 17 00:00:00 2001 From: Shyamsundar Gadde <73636812+ShyamGadde@users.noreply.github.com> Date: Tue, 12 Nov 2024 06:18:53 +0530 Subject: [PATCH 05/16] Refactor detection script file handling to use wp_scripts_get_suffix --- plugins/optimization-detective/detection.php | 7 +------ 1 file changed, 1 insertion(+), 6 deletions(-) diff --git a/plugins/optimization-detective/detection.php b/plugins/optimization-detective/detection.php index 83ba4fa3da..4fe232a22e 100644 --- a/plugins/optimization-detective/detection.php +++ b/plugins/optimization-detective/detection.php @@ -76,15 +76,10 @@ static function ( OD_URL_Metric_Group $group ): array { $detect_args['urlMetricGroupCollection'] = $group_collection; } - $detection_script_file = 'detect.min.js'; - if ( defined( 'SCRIPT_DEBUG' ) && SCRIPT_DEBUG ) { - $detection_script_file = 'detect.js'; - } - 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__ ) . $detection_script_file ) ), + 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( $detect_args ) ), array( 'type' => 'module' ) From 1b0731d106d77aa0acf1743c72ebd3ac31814f6a Mon Sep 17 00:00:00 2001 From: Shyamsundar Gadde <73636812+ShyamGadde@users.noreply.github.com> Date: Tue, 12 Nov 2024 13:55:11 +0530 Subject: [PATCH 06/16] Disable minification attempt of `web-vitals.js` --- webpack.config.js | 1 + 1 file changed, 1 insertion(+) diff --git a/webpack.config.js b/webpack.config.js index c0bd3793aa..81444c74b3 100644 --- a/webpack.config.js +++ b/webpack.config.js @@ -62,6 +62,7 @@ const optimizationDetective = ( env ) => { { from: `${ source }/dist/web-vitals.js`, to: `${ destination }/build/web-vitals.js`, + info: { minimized: true }, }, { from: `${ source }/package.json`, From b7e8e308636b5d632f6877cdc76c743e2ba71fdd Mon Sep 17 00:00:00 2001 From: Shyamsundar Gadde <73636812+ShyamGadde@users.noreply.github.com> Date: Tue, 12 Nov 2024 13:56:43 +0530 Subject: [PATCH 07/16] Disable minification of unminified partytown script files under `lib/debug` --- webpack.config.js | 1 + 1 file changed, 1 insertion(+) diff --git a/webpack.config.js b/webpack.config.js index 81444c74b3..2e76b21ad6 100644 --- a/webpack.config.js +++ b/webpack.config.js @@ -115,6 +115,7 @@ const webWorkerOffloading = ( env ) => { { from: `${ source }/lib/`, to: `${ destination }`, + info: { minimized: true }, }, { from: `${ source }/package.json`, From b7127d7dbec3d142d9ad6b9ce5fec4c572cdda3d Mon Sep 17 00:00:00 2001 From: Shyamsundar Gadde <73636812+ShyamGadde@users.noreply.github.com> Date: Tue, 12 Nov 2024 14:22:59 +0530 Subject: [PATCH 08/16] Add webpack config to create minified versions of script files in Embed Optimizer plugin --- webpack.config.js | 50 +++++++++++++++++++++++++++++++++++++++++++++-- 1 file changed, 48 insertions(+), 2 deletions(-) diff --git a/webpack.config.js b/webpack.config.js index 2e76b21ad6..9c87cd25d3 100644 --- a/webpack.config.js +++ b/webpack.config.js @@ -34,7 +34,48 @@ const sharedConfig = { }; // Store plugins that require build process. -const pluginsWithBuild = [ 'optimization-detective', 'web-worker-offloading' ]; +const pluginsWithBuild = [ + 'embed-optimizer', + 'optimization-detective', + 'web-worker-offloading', +]; + +/** + * Webpack Config: Embed Optimizer + * + * @param {*} env Webpack environment + * @return {Object} Webpack configuration + */ +const embedOptimizer = ( env ) => { + if ( env.plugin && env.plugin !== 'embed-optimizer' ) { + return defaultBuildConfig; + } + + const pluginDir = path.resolve( __dirname, 'plugins/embed-optimizer' ); + + return { + ...sharedConfig, + name: 'embed-optimizer', + plugins: [ + new CopyWebpackPlugin( { + patterns: [ + { + from: `${ pluginDir }/detect.js`, + to: `${ pluginDir }/detect.min.js`, + }, + { + from: `${ pluginDir }/lazy-load.js`, + to: `${ pluginDir }/lazy-load.min.js`, + }, + ], + } ), + new WebpackBar( { + name: 'Building Embed Optimizer Assets', + color: '#2196f3', + } ), + ], + }; +}; /** * Webpack Config: Optimization Detective @@ -210,4 +251,9 @@ const buildPlugin = ( env ) => { }; }; -module.exports = [ optimizationDetective, webWorkerOffloading, buildPlugin ]; +module.exports = [ + embedOptimizer, + optimizationDetective, + webWorkerOffloading, + buildPlugin, +]; From 888ecdb016fe720e59128e047cdc4f9f056a90e4 Mon Sep 17 00:00:00 2001 From: Shyamsundar Gadde <73636812+ShyamGadde@users.noreply.github.com> Date: Tue, 12 Nov 2024 14:27:55 +0530 Subject: [PATCH 09/16] Add minified file exists check to load.php in Embed Optimizer plugin --- plugins/embed-optimizer/load.php | 18 ++++++++++++++++++ 1 file changed, 18 insertions(+) diff --git a/plugins/embed-optimizer/load.php b/plugins/embed-optimizer/load.php index 2124b666e5..d5c3ae9507 100644 --- a/plugins/embed-optimizer/load.php +++ b/plugins/embed-optimizer/load.php @@ -76,6 +76,24 @@ static function ( string $version ): void { return; } + if ( + ( is_admin() || ( defined( 'WP_CLI' ) && WP_CLI ) ) && + ! file_exists( __DIR__ . '/detect.min.js' ) + ) { + // phpcs:ignore WordPress.PHP.DevelopmentFunctions.error_log_trigger_error + trigger_error( + esc_html( + sprintf( + /* translators: 1: File path. 2: CLI command. */ + '[Embed Optimizer] ' . __( 'Unable to load %1$s. Please make sure you have run %2$s.', 'embed-optimizer' ), + 'detect.min.js', + '`npm install && npm run build:plugin:embed-optimizer`' + ) + ), + E_USER_ERROR + ); + } + define( 'EMBED_OPTIMIZER_VERSION', $version ); // Load in the Embed Optimizer plugin hooks. From 693e1db66d98cc787d4a737f08e6a2c302835f2d Mon Sep 17 00:00:00 2001 From: Shyamsundar Gadde <73636812+ShyamGadde@users.noreply.github.com> Date: Tue, 12 Nov 2024 14:30:52 +0530 Subject: [PATCH 10/16] Conditionally load minified version of scripts in Embed Optimizer plugin --- plugins/embed-optimizer/hooks.php | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/plugins/embed-optimizer/hooks.php b/plugins/embed-optimizer/hooks.php index a512ac9c63..452794d030 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__ ) . 'detect.js' ); + $extension_module_urls[] = add_query_arg( 'ver', EMBED_OPTIMIZER_VERSION, plugin_dir_url( __FILE__ ) . sprintf( 'detect%s.js', wp_scripts_get_suffix() ) ); 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__ . '/lazy-load.js' ); // 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__ . 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. if ( false === $script ) { return ''; From 163082fe0c592ea185cccc06c48c2e1c3a1a035a Mon Sep 17 00:00:00 2001 From: Shyamsundar Gadde <73636812+ShyamGadde@users.noreply.github.com> Date: Tue, 12 Nov 2024 14:41:58 +0530 Subject: [PATCH 11/16] Add webpack config to create minified versions of script files in Image Prioritizer plugin --- webpack.config.js | 35 +++++++++++++++++++++++++++++++++++ 1 file changed, 35 insertions(+) diff --git a/webpack.config.js b/webpack.config.js index 9c87cd25d3..409f70c37b 100644 --- a/webpack.config.js +++ b/webpack.config.js @@ -36,6 +36,7 @@ const sharedConfig = { // Store plugins that require build process. const pluginsWithBuild = [ 'embed-optimizer', + 'image-prioritizer', 'optimization-detective', 'web-worker-offloading', ]; @@ -77,6 +78,39 @@ const embedOptimizer = ( env ) => { }; }; +/** + * Webpack Config: Image Prioritizer + * + * @param {*} env Webpack environment + * @return {Object} Webpack configuration + */ +const imagePrioritizer = ( env ) => { + if ( env.plugin && env.plugin !== 'image-prioritizer' ) { + return defaultBuildConfig; + } + + const pluginDir = path.resolve( __dirname, 'plugins/image-prioritizer' ); + + return { + ...sharedConfig, + name: 'image-prioritizer', + plugins: [ + new CopyWebpackPlugin( { + patterns: [ + { + from: `${ pluginDir }/lazy-load.js`, + to: `${ pluginDir }/lazy-load.min.js`, + }, + ], + } ), + new WebpackBar( { + name: 'Building Image Prioritizer Assets', + color: '#2196f3', + } ), + ], + }; +}; + /** * Webpack Config: Optimization Detective * @@ -253,6 +287,7 @@ const buildPlugin = ( env ) => { module.exports = [ embedOptimizer, + imagePrioritizer, optimizationDetective, webWorkerOffloading, buildPlugin, From 232e22751b57b62f2235f1e429f3a6d3755b7121 Mon Sep 17 00:00:00 2001 From: Shyamsundar Gadde <73636812+ShyamGadde@users.noreply.github.com> Date: Tue, 12 Nov 2024 14:46:34 +0530 Subject: [PATCH 12/16] Add minified file exists check to load.php in Image Prioritizer plugin --- plugins/image-prioritizer/load.php | 18 ++++++++++++++++++ 1 file changed, 18 insertions(+) diff --git a/plugins/image-prioritizer/load.php b/plugins/image-prioritizer/load.php index 9e1fa43696..a790e6555d 100644 --- a/plugins/image-prioritizer/load.php +++ b/plugins/image-prioritizer/load.php @@ -77,6 +77,24 @@ static function ( string $version ): void { return; } + if ( + ( is_admin() || ( defined( 'WP_CLI' ) && WP_CLI ) ) && + ! file_exists( __DIR__ . '/lazy-load.min.js' ) + ) { + // phpcs:ignore WordPress.PHP.DevelopmentFunctions.error_log_trigger_error + trigger_error( + esc_html( + sprintf( + /* translators: 1: File path. 2: CLI command. */ + '[Image Prioritizer] ' . __( 'Unable to load %1$s. Please make sure you have run %2$s.', 'image-prioritizer' ), + 'lazy-load.min.js', + '`npm install && npm run build:plugin:image-prioritizer`' + ) + ), + E_USER_ERROR + ); + } + define( 'IMAGE_PRIORITIZER_VERSION', $version ); require_once __DIR__ . '/helper.php'; From 9747f7b17d05ed12fe966f04b65e5be06f5d0aa4 Mon Sep 17 00:00:00 2001 From: Shyamsundar Gadde <73636812+ShyamGadde@users.noreply.github.com> Date: Tue, 12 Nov 2024 14:47:15 +0530 Subject: [PATCH 13/16] Conditionally load minified version of scripts in Image Prioritizer plugin --- plugins/image-prioritizer/hooks.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/plugins/image-prioritizer/hooks.php b/plugins/image-prioritizer/hooks.php index 18ffecabd7..9e962ce8f1 100644 --- a/plugins/image-prioritizer/hooks.php +++ b/plugins/image-prioritizer/hooks.php @@ -22,7 +22,7 @@ * @since 0.2.0 */ function image_prioritizer_get_lazy_load_script(): string { - $script = file_get_contents( __DIR__ . '/lazy-load.js' ); // 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__ . 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. if ( false === $script ) { return ''; From 0a1354bdfd36fa2d387ca86918666050e892ed31 Mon Sep 17 00:00:00 2001 From: Shyamsundar Gadde <73636812+ShyamGadde@users.noreply.github.com> Date: Wed, 13 Nov 2024 15:08:23 +0530 Subject: [PATCH 14/16] Load unminified version of partytown scripts when `SCRIPT_DEBUG` is enabled --- plugins/web-worker-offloading/hooks.php | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-) diff --git a/plugins/web-worker-offloading/hooks.php b/plugins/web-worker-offloading/hooks.php index 0b7926f355..6778c61283 100644 --- a/plugins/web-worker-offloading/hooks.php +++ b/plugins/web-worker-offloading/hooks.php @@ -21,7 +21,12 @@ function plwwo_register_default_scripts( WP_Scripts $scripts ): void { // The source code for partytown.js is built from . // See webpack config in the WordPress/performance repo: . - $partytown_js = file_get_contents( __DIR__ . '/build/partytown.js' ); // phpcs:ignore WordPress.WP.AlternativeFunctions.file_get_contents_file_get_contents -- It's a local filesystem path not a remote request. + $partytown_js_path = '/build/partytown.js'; + if ( defined( 'SCRIPT_DEBUG' ) && SCRIPT_DEBUG ) { + $partytown_js_path = '/build/debug/partytown.js'; + } + + $partytown_js = file_get_contents( __DIR__ . $partytown_js_path ); // phpcs:ignore WordPress.WP.AlternativeFunctions.file_get_contents_file_get_contents -- It's a local filesystem path not a remote request. if ( false === $partytown_js ) { return; } From 6e18901e214934ade1c973f847746c0a169b0a00 Mon Sep 17 00:00:00 2001 From: Shyamsundar Gadde Date: Wed, 13 Nov 2024 23:41:07 +0530 Subject: [PATCH 15/16] Refactor conditional logic for script path assignment Co-authored-by: Weston Ruter --- plugins/web-worker-offloading/hooks.php | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/plugins/web-worker-offloading/hooks.php b/plugins/web-worker-offloading/hooks.php index 6778c61283..7e90a5eed4 100644 --- a/plugins/web-worker-offloading/hooks.php +++ b/plugins/web-worker-offloading/hooks.php @@ -21,9 +21,10 @@ function plwwo_register_default_scripts( WP_Scripts $scripts ): void { // The source code for partytown.js is built from . // See webpack config in the WordPress/performance repo: . - $partytown_js_path = '/build/partytown.js'; if ( defined( 'SCRIPT_DEBUG' ) && SCRIPT_DEBUG ) { $partytown_js_path = '/build/debug/partytown.js'; + } else { + $partytown_js_path = '/build/partytown.js'; } $partytown_js = file_get_contents( __DIR__ . $partytown_js_path ); // phpcs:ignore WordPress.WP.AlternativeFunctions.file_get_contents_file_get_contents -- It's a local filesystem path not a remote request. From 7af4b0734a9f598aba2ff2a330b4f61b5678d0fc Mon Sep 17 00:00:00 2001 From: Weston Ruter Date: Wed, 13 Nov 2024 11:45:22 -0800 Subject: [PATCH 16/16] Add changelog entries --- plugins/embed-optimizer/readme.txt | 6 ++++++ plugins/image-prioritizer/readme.txt | 6 ++++++ plugins/optimization-detective/readme.txt | 6 ++++++ plugins/web-worker-offloading/readme.txt | 6 ++++++ 4 files changed, 24 insertions(+) diff --git a/plugins/embed-optimizer/readme.txt b/plugins/embed-optimizer/readme.txt index b02756cfbb..cc07740daa 100644 --- a/plugins/embed-optimizer/readme.txt +++ b/plugins/embed-optimizer/readme.txt @@ -67,6 +67,12 @@ The [plugin source code](https://github.com/WordPress/performance/tree/trunk/plu == Changelog == += n.e.x.t = + +**Enhancements** + +* Serve unminified scripts when `SCRIPT_DEBUG` is enabled. ([1643](https://github.com/WordPress/performance/pull/1643)) + = 0.3.0 = **Enhancements** diff --git a/plugins/image-prioritizer/readme.txt b/plugins/image-prioritizer/readme.txt index a7925be3ab..cb5a67c0eb 100644 --- a/plugins/image-prioritizer/readme.txt +++ b/plugins/image-prioritizer/readme.txt @@ -62,6 +62,12 @@ The [plugin source code](https://github.com/WordPress/performance/tree/trunk/plu == Changelog == += n.e.x.t = + +**Enhancements** + +* Serve unminified scripts when `SCRIPT_DEBUG` is enabled. ([1643](https://github.com/WordPress/performance/pull/1643)) + = 0.2.0 = **Enhancements** diff --git a/plugins/optimization-detective/readme.txt b/plugins/optimization-detective/readme.txt index a0f1e51804..1e16f2c8bf 100644 --- a/plugins/optimization-detective/readme.txt +++ b/plugins/optimization-detective/readme.txt @@ -253,6 +253,12 @@ The [plugin source code](https://github.com/WordPress/performance/tree/trunk/plu == Changelog == += n.e.x.t = + +**Enhancements** + +* Serve unminified scripts when `SCRIPT_DEBUG` is enabled. ([1643](https://github.com/WordPress/performance/pull/1643)) + = 0.7.0 = **Enhancements** diff --git a/plugins/web-worker-offloading/readme.txt b/plugins/web-worker-offloading/readme.txt index db93c984cd..4c1e62a7ed 100644 --- a/plugins/web-worker-offloading/readme.txt +++ b/plugins/web-worker-offloading/readme.txt @@ -94,6 +94,12 @@ The [plugin source code](https://github.com/WordPress/performance/tree/trunk/plu == Changelog == += n.e.x.t = + +**Enhancements** + +* Serve unminified scripts when `SCRIPT_DEBUG` is enabled. ([1643](https://github.com/WordPress/performance/pull/1643)) + = 0.1.1 = **Enhancements**