Skip to content

Commit 638c1ed

Browse files
authored
Merge pull request #1658 from WordPress/add/pl-asset-mininification
Minify script used for ajax activation of features; warn if absent and serve original file when SCRIPT_DEBUG is enabled
2 parents d2d5eae + 5509f2c commit 638c1ed

File tree

8 files changed

+187
-5
lines changed

8 files changed

+187
-5
lines changed

plugins/embed-optimizer/hooks.php

Lines changed: 38 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -121,7 +121,7 @@ function embed_optimizer_filter_extension_module_urls( $extension_module_urls ):
121121
if ( ! is_array( $extension_module_urls ) ) {
122122
$extension_module_urls = array();
123123
}
124-
$extension_module_urls[] = add_query_arg( 'ver', EMBED_OPTIMIZER_VERSION, plugin_dir_url( __FILE__ ) . sprintf( 'detect%s.js', wp_scripts_get_suffix() ) );
124+
$extension_module_urls[] = add_query_arg( 'ver', EMBED_OPTIMIZER_VERSION, plugin_dir_url( __FILE__ ) . embed_optimizer_get_asset_path( 'detect.js' ) );
125125
return $extension_module_urls;
126126
}
127127

@@ -326,7 +326,7 @@ function embed_optimizer_lazy_load_scripts(): void {
326326
* @since 0.2.0
327327
*/
328328
function embed_optimizer_get_lazy_load_script(): string {
329-
$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.
329+
$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.
330330

331331
if ( false === $script ) {
332332
return '';
@@ -424,3 +424,39 @@ function embed_optimizer_render_generator(): void {
424424
// Use the plugin slug as it is immutable.
425425
echo '<meta name="generator" content="embed-optimizer ' . esc_attr( EMBED_OPTIMIZER_VERSION ) . '">' . "\n";
426426
}
427+
428+
/**
429+
* Gets the path to a script or stylesheet.
430+
*
431+
* @since n.e.x.t
432+
*
433+
* @param string $src_path Source path, relative to plugin root.
434+
* @param string|null $min_path Minified path. If not supplied, then '.min' is injected before the file extension in the source path.
435+
* @return string URL to script or stylesheet.
436+
*/
437+
function embed_optimizer_get_asset_path( string $src_path, ?string $min_path = null ): string {
438+
if ( null === $min_path ) {
439+
// Note: wp_scripts_get_suffix() is not used here because we need access to both the source and minified paths.
440+
$min_path = (string) preg_replace( '/(?=\.\w+$)/', '.min', $src_path );
441+
}
442+
443+
$force_src = false;
444+
if ( WP_DEBUG && ! file_exists( trailingslashit( __DIR__ ) . $min_path ) ) {
445+
$force_src = true;
446+
wp_trigger_error(
447+
__FUNCTION__,
448+
sprintf(
449+
/* translators: %s is the minified asset path */
450+
__( 'Minified asset has not been built: %s', 'embed-optimizer' ),
451+
$min_path
452+
),
453+
E_USER_WARNING
454+
);
455+
}
456+
457+
if ( SCRIPT_DEBUG || $force_src ) {
458+
return $src_path;
459+
}
460+
461+
return $min_path;
462+
}

plugins/image-prioritizer/helper.php

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -76,3 +76,39 @@ function image_prioritizer_register_tag_visitors( OD_Tag_Visitor_Registry $regis
7676
$video_visitor = new Image_Prioritizer_Video_Tag_Visitor();
7777
$registry->register( 'image-prioritizer/video', $video_visitor );
7878
}
79+
80+
/**
81+
* Gets the path to a script or stylesheet.
82+
*
83+
* @since n.e.x.t
84+
*
85+
* @param string $src_path Source path, relative to plugin root.
86+
* @param string|null $min_path Minified path. If not supplied, then '.min' is injected before the file extension in the source path.
87+
* @return string URL to script or stylesheet.
88+
*/
89+
function image_prioritizer_get_asset_path( string $src_path, ?string $min_path = null ): string {
90+
if ( null === $min_path ) {
91+
// Note: wp_scripts_get_suffix() is not used here because we need access to both the source and minified paths.
92+
$min_path = (string) preg_replace( '/(?=\.\w+$)/', '.min', $src_path );
93+
}
94+
95+
$force_src = false;
96+
if ( WP_DEBUG && ! file_exists( trailingslashit( __DIR__ ) . $min_path ) ) {
97+
$force_src = true;
98+
wp_trigger_error(
99+
__FUNCTION__,
100+
sprintf(
101+
/* translators: %s is the minified asset path */
102+
__( 'Minified asset has not been built: %s', 'image-prioritizer' ),
103+
$min_path
104+
),
105+
E_USER_WARNING
106+
);
107+
}
108+
109+
if ( SCRIPT_DEBUG || $force_src ) {
110+
return $src_path;
111+
}
112+
113+
return $min_path;
114+
}

plugins/image-prioritizer/hooks.php

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,8 @@
2222
* @since 0.2.0
2323
*/
2424
function image_prioritizer_get_lazy_load_script(): string {
25-
$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.
25+
$path = image_prioritizer_get_asset_path( 'lazy-load.js' );
26+
$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.
2627

2728
if ( false === $script ) {
2829
return '';

plugins/optimization-detective/detection.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -114,7 +114,7 @@ static function ( OD_URL_Metric_Group $group ): array {
114114
return wp_get_inline_script_tag(
115115
sprintf(
116116
'import detect from %s; detect( %s );',
117-
wp_json_encode( add_query_arg( 'ver', OPTIMIZATION_DETECTIVE_VERSION, plugin_dir_url( __FILE__ ) . sprintf( 'detect%s.js', wp_scripts_get_suffix() ) ) ),
117+
wp_json_encode( add_query_arg( 'ver', OPTIMIZATION_DETECTIVE_VERSION, plugin_dir_url( __FILE__ ) . od_get_asset_path( 'detect.js' ) ) ),
118118
wp_json_encode( $detect_args )
119119
),
120120
array( 'type' => 'module' )

plugins/optimization-detective/helper.php

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -64,3 +64,39 @@ function od_render_generator_meta_tag(): void {
6464
// Use the plugin slug as it is immutable.
6565
echo '<meta name="generator" content="optimization-detective ' . esc_attr( OPTIMIZATION_DETECTIVE_VERSION ) . '">' . "\n";
6666
}
67+
68+
/**
69+
* Gets the path to a script or stylesheet.
70+
*
71+
* @since n.e.x.t
72+
*
73+
* @param string $src_path Source path, relative to plugin root.
74+
* @param string|null $min_path Minified path. If not supplied, then '.min' is injected before the file extension in the source path.
75+
* @return string URL to script or stylesheet.
76+
*/
77+
function od_get_asset_path( string $src_path, ?string $min_path = null ): string {
78+
if ( null === $min_path ) {
79+
// Note: wp_scripts_get_suffix() is not used here because we need access to both the source and minified paths.
80+
$min_path = (string) preg_replace( '/(?=\.\w+$)/', '.min', $src_path );
81+
}
82+
83+
$force_src = false;
84+
if ( WP_DEBUG && ! file_exists( trailingslashit( __DIR__ ) . $min_path ) ) {
85+
$force_src = true;
86+
wp_trigger_error(
87+
__FUNCTION__,
88+
sprintf(
89+
/* translators: %s is the minified asset path */
90+
__( 'Minified asset has not been built: %s', 'optimization-detective' ),
91+
$min_path
92+
),
93+
E_USER_WARNING
94+
);
95+
}
96+
97+
if ( SCRIPT_DEBUG || $force_src ) {
98+
return $src_path;
99+
}
100+
101+
return $min_path;
102+
}

plugins/performance-lab/includes/admin/load.php

Lines changed: 37 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -213,6 +213,42 @@ function perflab_dismiss_wp_pointer_wrapper(): void {
213213
}
214214
add_action( 'wp_ajax_dismiss-wp-pointer', 'perflab_dismiss_wp_pointer_wrapper', 0 );
215215

216+
/**
217+
* Gets the path to a script or stylesheet.
218+
*
219+
* @since n.e.x.t
220+
*
221+
* @param string $src_path Source path.
222+
* @param string|null $min_path Minified path. If not supplied, then '.min' is injected before the file extension in the source path.
223+
* @return string URL to script or stylesheet.
224+
*/
225+
function perflab_get_asset_path( string $src_path, ?string $min_path = null ): string {
226+
if ( null === $min_path ) {
227+
// Note: wp_scripts_get_suffix() is not used here because we need access to both the source and minified paths.
228+
$min_path = (string) preg_replace( '/(?=\.\w+$)/', '.min', $src_path );
229+
}
230+
231+
$force_src = false;
232+
if ( WP_DEBUG && ! file_exists( trailingslashit( PERFLAB_PLUGIN_DIR_PATH ) . $min_path ) ) {
233+
$force_src = true;
234+
wp_trigger_error(
235+
__FUNCTION__,
236+
sprintf(
237+
/* translators: %s is the minified asset path */
238+
__( 'Minified asset has not been built: %s', 'performance-lab' ),
239+
$min_path
240+
),
241+
E_USER_WARNING
242+
);
243+
}
244+
245+
if ( SCRIPT_DEBUG || $force_src ) {
246+
return $src_path;
247+
}
248+
249+
return $min_path;
250+
}
251+
216252
/**
217253
* Callback function to handle admin scripts.
218254
*
@@ -228,7 +264,7 @@ function perflab_enqueue_features_page_scripts(): void {
228264
// Enqueue plugin activate AJAX script and localize script data.
229265
wp_enqueue_script(
230266
'perflab-plugin-activate-ajax',
231-
plugin_dir_url( PERFLAB_MAIN_FILE ) . 'includes/admin/plugin-activate-ajax.js',
267+
plugin_dir_url( PERFLAB_MAIN_FILE ) . perflab_get_asset_path( 'includes/admin/plugin-activate-ajax.js' ),
232268
array( 'wp-i18n', 'wp-a11y', 'wp-api-fetch' ),
233269
PERFLAB_VERSION,
234270
true

tools/phpstan/constants.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,3 +17,5 @@
1717
define( 'IMAGE_PRIORITIZER_VERSION', '0.0.0' );
1818

1919
define( 'EMBED_OPTIMIZER_VERSION', '0.0.0' );
20+
21+
define( 'PERFLAB_PLUGIN_DIR_PATH', __DIR__ . '/../../plugins/performance-lab/' );

webpack.config.js

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,12 +35,46 @@ const sharedConfig = {
3535

3636
// Store plugins that require build process.
3737
const pluginsWithBuild = [
38+
'performance-lab',
3839
'embed-optimizer',
3940
'image-prioritizer',
4041
'optimization-detective',
4142
'web-worker-offloading',
4243
];
4344

45+
/**
46+
* Webpack Config: Performance Lab
47+
*
48+
* @param {*} env Webpack environment
49+
* @return {Object} Webpack configuration
50+
*/
51+
const performanceLab = ( env ) => {
52+
if ( env.plugin && env.plugin !== 'performance-lab' ) {
53+
return defaultBuildConfig;
54+
}
55+
56+
const pluginDir = path.resolve( __dirname, 'plugins/performance-lab' );
57+
58+
return {
59+
...sharedConfig,
60+
name: 'performance-lab',
61+
plugins: [
62+
new CopyWebpackPlugin( {
63+
patterns: [
64+
{
65+
from: `${ pluginDir }/includes/admin/plugin-activate-ajax.js`,
66+
to: `${ pluginDir }/includes/admin/plugin-activate-ajax.min.js`,
67+
},
68+
],
69+
} ),
70+
new WebpackBar( {
71+
name: 'Building Performance Lab Assets',
72+
color: '#2196f3',
73+
} ),
74+
],
75+
};
76+
};
77+
4478
/**
4579
* Webpack Config: Embed Optimizer
4680
*
@@ -286,6 +320,7 @@ const buildPlugin = ( env ) => {
286320
};
287321

288322
module.exports = [
323+
performanceLab,
289324
embedOptimizer,
290325
imagePrioritizer,
291326
optimizationDetective,

0 commit comments

Comments
 (0)