Skip to content

Commit ca23e6f

Browse files
committed
Script Loader: Add sourceURL to inline scripts and styles.
Improve the source locations referenced by developer tooling in supporting browsers. Inline source locations are named like inline:handle-js-after and appear in the developer tools "sources" panel. This is the second attempt to add sourceURL comments. The first attempt in [60685] was reverted due to an issue with script concatenation that has been addressed. Developed in #9672. Follow-up to [60685], [60690]. Props jonsurrell, westonruter, wildworks, peterwilsoncc, johnbillion, tobiasbg. Fixes #63887. git-svn-id: https://develop.svn.wordpress.org/trunk@60719 602fd350-edb4-49c9-b593-d223f7449a82
1 parent 9d802e0 commit ca23e6f

File tree

7 files changed

+286
-70
lines changed

7 files changed

+286
-70
lines changed

src/wp-includes/class-wp-scripts.php

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -222,6 +222,20 @@ public function print_extra_script( $handle, $display = true ) {
222222
return;
223223
}
224224

225+
/*
226+
* Do not print a sourceURL comment if concatenation is enabled.
227+
*
228+
* Extra scripts may be concatenated into a single script.
229+
* The line-based sourceURL comments may a concatenated script and
230+
* do not make sense when multiple are joined together.
231+
*/
232+
if ( ! $this->do_concat ) {
233+
$output .= sprintf(
234+
"\n//# sourceURL=%s",
235+
rawurlencode( "{$handle}-js-extra" )
236+
);
237+
}
238+
225239
if ( ! $display ) {
226240
return $output;
227241
}
@@ -524,6 +538,17 @@ public function get_inline_script_data( $handle, $position = 'after' ) {
524538
return '';
525539
}
526540

541+
/*
542+
* Print sourceURL comment regardless of concatenation.
543+
*
544+
* Inline scripts prevent scripts from being concatenated, so
545+
* sourceURL comments are safe to print for inline scripts.
546+
*/
547+
$data[] = sprintf(
548+
'//# sourceURL=%s',
549+
rawurlencode( "{$handle}-js-{$position}" )
550+
);
551+
527552
return trim( implode( "\n", $data ), "\n" );
528553
}
529554

@@ -696,12 +721,15 @@ public function print_translations( $handle, $display = true ) {
696721
return false;
697722
}
698723

724+
$source_url = rawurlencode( "{$handle}-js-translations" );
725+
699726
$output = <<<JS
700727
( function( domain, translations ) {
701728
var localeData = translations.locale_data[ domain ] || translations.locale_data.messages;
702729
localeData[""].domain = domain;
703730
wp.i18n.setLocaleData( localeData, domain );
704731
} )( "{$domain}", {$json_translations} );
732+
//# sourceURL={$source_url}
705733
JS;
706734

707735
if ( $display ) {

src/wp-includes/class-wp-styles.php

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -337,6 +337,13 @@ public function print_inline_style( $handle, $display = true ) {
337337
return false;
338338
}
339339

340+
if ( ! $this->do_concat ) {
341+
$output[] = sprintf(
342+
'/*# sourceURL=%s */',
343+
rawurlencode( "{$handle}-inline-css" )
344+
);
345+
}
346+
340347
$output = implode( "\n", $output );
341348

342349
if ( ! $display ) {

src/wp-includes/script-loader.php

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2212,6 +2212,7 @@ function _print_scripts() {
22122212
echo "\n<script{$type_attr}>\n";
22132213
echo "/* <![CDATA[ */\n"; // Not needed in HTML 5.
22142214
echo $wp_scripts->print_code;
2215+
echo sprintf( "\n//# sourceURL=%s\n", rawurlencode( 'js-inline-concat-' . $concat ) );
22152216
echo "/* ]]> */\n";
22162217
echo "</script>\n";
22172218
}
@@ -2394,8 +2395,9 @@ function _print_styles() {
23942395
$dir = $wp_styles->text_direction;
23952396
$ver = $wp_styles->default_version;
23962397

2397-
$concat = str_split( $concat, 128 );
2398-
$concatenated = '';
2398+
$concat_source_url = 'css-inline-concat-' . $concat;
2399+
$concat = str_split( $concat, 128 );
2400+
$concatenated = '';
23992401

24002402
foreach ( $concat as $key => $chunk ) {
24012403
$concatenated .= "&load%5Bchunk_{$key}%5D={$chunk}";
@@ -2407,6 +2409,7 @@ function _print_styles() {
24072409
if ( ! empty( $wp_styles->print_code ) ) {
24082410
echo "<style{$type_attr}>\n";
24092411
echo $wp_styles->print_code;
2412+
echo sprintf( "\n/*# sourceURL=%s */", rawurlencode( $concat_source_url ) );
24102413
echo "\n</style>\n";
24112414
}
24122415
}

tests/phpunit/tests/blocks/editor.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -762,6 +762,7 @@ public function test_ensure_preload_data_script_tag_closes() {
762762
<script src="{$baseurl}/wp-includes/js/dist/api-fetch.min.js?ver=test" id="wp-api-fetch-js"></script>
763763
<script id="wp-api-fetch-js-after">
764764
wp.apiFetch.use( wp.apiFetch.createPreloadingMiddleware( {"/test/v0/test-62797":{"body":["Unclosed comment and a script open tag \\u003C!--\\u003Cscript\\u003E"],"headers":{"Allow":"GET"}}} ) );
765+
//# sourceURL=wp-api-fetch-js-after
765766
</script>
766767
767768
HTML;

0 commit comments

Comments
 (0)