Skip to content

Commit c51de8b

Browse files
committed
Add sourceURL comments for inline scripts
1 parent 62b54c0 commit c51de8b

File tree

8 files changed

+86
-12
lines changed

8 files changed

+86
-12
lines changed

plugins/embed-optimizer/helper.php

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -351,6 +351,14 @@ function embed_optimizer_get_lazy_load_script(): string {
351351
return '';
352352
}
353353

354+
$script .= sprintf(
355+
"\n//# sourceURL=%s",
356+
add_query_arg(
357+
array( 'ver' => EMBED_OPTIMIZER_VERSION ),
358+
plugins_url( embed_optimizer_get_asset_path( 'lazy-load.js' ), __FILE__ )
359+
)
360+
);
361+
354362
return $script;
355363
}
356364

plugins/image-prioritizer/helper.php

Lines changed: 30 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -400,8 +400,16 @@ function image_prioritizer_get_asset_path( string $src_path, ?string $min_path =
400400
* @return string Lazy load script.
401401
*/
402402
function image_prioritizer_get_video_lazy_load_script(): string {
403-
$path = image_prioritizer_get_asset_path( 'lazy-load-video.js' );
404-
return (string) 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.
403+
$path = image_prioritizer_get_asset_path( 'lazy-load-video.js' );
404+
$script = (string) 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.
405+
$script .= sprintf(
406+
"\n//# sourceURL=%s",
407+
add_query_arg(
408+
array( 'ver' => IMAGE_PRIORITIZER_VERSION ),
409+
plugins_url( $path, __FILE__ )
410+
)
411+
);
412+
return $script;
405413
}
406414

407415
/**
@@ -415,8 +423,16 @@ function image_prioritizer_get_video_lazy_load_script(): string {
415423
* @return string Lazy load script.
416424
*/
417425
function image_prioritizer_get_lazy_load_bg_image_script(): string {
418-
$path = image_prioritizer_get_asset_path( 'lazy-load-bg-image.js' );
419-
return (string) 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.
426+
$path = image_prioritizer_get_asset_path( 'lazy-load-bg-image.js' );
427+
$script = (string) 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.
428+
$script .= sprintf(
429+
"\n//# sourceURL=%s",
430+
add_query_arg(
431+
array( 'ver' => IMAGE_PRIORITIZER_VERSION ),
432+
plugins_url( $path, __FILE__ )
433+
)
434+
);
435+
return $script;
420436
}
421437

422438
/**
@@ -428,6 +444,14 @@ function image_prioritizer_get_lazy_load_bg_image_script(): string {
428444
* @return string Lazy load stylesheet.
429445
*/
430446
function image_prioritizer_get_lazy_load_bg_image_stylesheet(): string {
431-
$path = image_prioritizer_get_asset_path( 'lazy-load-bg-image.css' );
432-
return (string) 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.
447+
$path = image_prioritizer_get_asset_path( 'lazy-load-bg-image.css' );
448+
$style = (string) 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.
449+
$style .= sprintf(
450+
"\n/*# sourceURL=%s */",
451+
add_query_arg(
452+
array( 'ver' => IMAGE_PRIORITIZER_VERSION ),
453+
plugins_url( $path, __FILE__ )
454+
)
455+
);
456+
return $style;
433457
}

plugins/image-prioritizer/tests/test-cases/common-lcp-background-image-and-lazy-loaded-background-image-outside-viewport-with-fully-populated-sample-data/expected.html

Lines changed: 1 addition & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

plugins/image-prioritizer/tests/test-cases/only-mobile-and-desktop-groups-are-populated/expected.html

Lines changed: 1 addition & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

plugins/optimization-detective/detection.php

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -151,10 +151,14 @@ static function ( OD_URL_Metric_Group $group ): array {
151151

152152
return wp_get_inline_script_tag(
153153
sprintf(
154-
'( %s )( %s, %s );',
154+
"( %s )( %s, %s );\n//# sourceURL=%s",
155155
file_get_contents( __DIR__ . '/' . od_get_asset_path( 'detect-loader.js' ) ), // phpcs:ignore WordPress.WP.AlternativeFunctions.file_get_contents_file_get_contents -- It's a local filesystem path not a remote request.
156156
wp_json_encode( plugins_url( add_query_arg( 'ver', OPTIMIZATION_DETECTIVE_VERSION, od_get_asset_path( 'detect.js' ) ), __FILE__ ) ),
157-
wp_json_encode( $detect_args )
157+
wp_json_encode( $detect_args ),
158+
add_query_arg(
159+
array( 'ver' => OPTIMIZATION_DETECTIVE_VERSION ),
160+
plugins_url( od_get_asset_path( 'detect-loader.js' ), __FILE__ )
161+
)
158162
),
159163
array( 'type' => 'module' )
160164
);

plugins/optimization-detective/optimization.php

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -118,11 +118,12 @@ function_exists( 'perflab_server_timing_use_output_buffer' )
118118
* @param string[] $reasons Reason messages.
119119
*/
120120
function od_print_disabled_reasons( array $reasons ): void {
121-
foreach ( $reasons as $reason ) {
121+
foreach ( $reasons as $i => $reason ) {
122122
wp_print_inline_script_tag(
123123
sprintf(
124-
'console.info( %s );',
125-
wp_json_encode( '[Optimization Detective] ' . $reason )
124+
"console.info( %s );\n//# sourceURL=od-print-disabled-reasons-%d",
125+
wp_json_encode( '[Optimization Detective] ' . $reason ),
126+
$i + 1
126127
),
127128
array( 'type' => 'module' )
128129
);

plugins/optimization-detective/tests/class-optimization-detective-test-helpers.php

Lines changed: 35 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -279,6 +279,7 @@ public function assert_snapshot_equals( string $directory ): void {
279279
$buffer = od_optimize_template_output_buffer( $buffer );
280280

281281
// Normalize script module content so changes do not impact snapshots.
282+
// TODO: Once WP 6.7 is the minimum-supported version, replace this with WP_HTML_Tag_Processor::set_modifiable_text().
282283
$buffer = preg_replace_callback(
283284
'#(<script type="module">)(.+?)(</script>)#s',
284285
static function ( $matches ) {
@@ -290,9 +291,42 @@ static function ( $matches ) {
290291
$text = trim( $text );
291292
if ( 1 === preg_match( '/^(import|const) \w+/', $text, $matches ) ) {
292293
$text = '/* ' . $matches[0] . ' ... */';
293-
} elseif ( 1 === preg_match( '/^\( async function load.+\);$/s', $text ) ) {
294+
} elseif ( 1 === preg_match( ':^\( async function load.+\);(\n//# sourceURL=.*)?$:s', $text ) ) {
294295
$text = '/* detect loader */';
295296
}
297+
298+
// Normalize versions which occur in sourceURL.
299+
$text = preg_replace( '/ver=\d+\.\d+\.\d+(-\w+)?/', 'ver=__VERSION__', $text );
300+
301+
// Ensure any home URLs are normalized to account for variations in the testing environment.
302+
$text = str_replace(
303+
array( home_url( '/', 'http' ), home_url( '/', 'https' ) ),
304+
'https://example.com/',
305+
$text
306+
);
307+
308+
return $start_tag . $text . $end_tag;
309+
},
310+
$buffer
311+
);
312+
313+
// TODO: Once WP 6.7 is the minimum-supported version, replace this with WP_HTML_Tag_Processor::set_modifiable_text().
314+
$buffer = preg_replace_callback(
315+
'#(<style[^>]*?>)(.+?)(</style>)#s',
316+
static function ( $matches ) {
317+
array_shift( $matches );
318+
list( $start_tag, $text, $end_tag ) = $matches;
319+
320+
// Normalize versions which occur in sourceURL.
321+
$text = preg_replace( '/ver=\d+\.\d+\.\d+(-\w+)?/', 'ver=__VERSION__', $text );
322+
323+
// Ensure any home URLs are normalized to account for variations in the testing environment.
324+
$text = str_replace(
325+
array( home_url( '/', 'http' ), home_url( '/', 'https' ) ),
326+
'https://example.com/',
327+
$text
328+
);
329+
296330
return $start_tag . $text . $end_tag;
297331
},
298332
$buffer

plugins/speculation-rules/settings.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -340,6 +340,7 @@ function plsr_render_settings_field( array $args ): void {
340340
}
341341
JS;
342342
// 👆 This 'JS;' line can only be indented two tabs when minimum PHP version is increased to 7.3+.
343+
$js .= "\n//# sourceURL=speculation-rules-auth-admin-notice";
343344
wp_print_inline_script_tag( $js, array( 'type' => 'module' ) );
344345
?>
345346
<?php endif; ?>

0 commit comments

Comments
 (0)