Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
28 changes: 28 additions & 0 deletions src/wp-includes/class-wp-scripts.php
Original file line number Diff line number Diff line change
Expand Up @@ -222,6 +222,20 @@ public function print_extra_script( $handle, $display = true ) {
return;
}

/*
* Do not print a sourceURL comment if concatenation is enabled.
*
* Extra scripts may be concatenated into a single script.
* The line-based sourceURL comments may a concatenated script and
* do not make sense when multiple are joined together.
*/
if ( ! $this->do_concat ) {
$output .= sprintf(
"\n//# sourceURL=%s",
rawurlencode( "{$handle}-js-extra" )
);
}

if ( ! $display ) {
return $output;
}
Expand Down Expand Up @@ -524,6 +538,17 @@ public function get_inline_script_data( $handle, $position = 'after' ) {
return '';
}

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

To contrast this with extra above, it might be worth adding add a comment to note that inline before/after scripts do not get concatenated together, so it is fine to source URL comment.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

👍 1bbecc8

/*
* Print sourceURL comment regardless of concatenation.
*
* Inline scripts prevent scripts from being concatenated, so
* sourceURL comments are safe to print for inline scripts.
*/
$data[] = sprintf(
'//# sourceURL=%s',
rawurlencode( "{$handle}-js-{$position}" )
);

return trim( implode( "\n", $data ), "\n" );
}

Expand Down Expand Up @@ -696,12 +721,15 @@ public function print_translations( $handle, $display = true ) {
return false;
}

$source_url = rawurlencode( "{$handle}-js-translations" );

$output = <<<JS
( function( domain, translations ) {
var localeData = translations.locale_data[ domain ] || translations.locale_data.messages;
localeData[""].domain = domain;
wp.i18n.setLocaleData( localeData, domain );
} )( "{$domain}", {$json_translations} );
//# sourceURL={$source_url}
JS;

if ( $display ) {
Expand Down
7 changes: 7 additions & 0 deletions src/wp-includes/class-wp-styles.php
Original file line number Diff line number Diff line change
Expand Up @@ -337,6 +337,13 @@ public function print_inline_style( $handle, $display = true ) {
return false;
}

if ( ! $this->do_concat ) {
$output[] = sprintf(
'/*# sourceURL=%s */',
rawurlencode( "{$handle}-inline-css" )
);
}

$output = implode( "\n", $output );

if ( ! $display ) {
Expand Down
7 changes: 5 additions & 2 deletions src/wp-includes/script-loader.php
Original file line number Diff line number Diff line change
Expand Up @@ -2212,6 +2212,7 @@ function _print_scripts() {
echo "\n<script{$type_attr}>\n";
echo "/* <![CDATA[ */\n"; // Not needed in HTML 5.
echo $wp_scripts->print_code;
echo sprintf( "\n//# sourceURL=%s\n", rawurlencode( 'js-inline-concat-' . $concat ) );
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

There's example output in tests, but this outputs a sourceURL like this:

//# sourceURL=js-inline-concat-one%2Ctwo

$concat is a list of the concatenated script names, one,two in this case. It should work well as a descriptive identifier. For example, on my test site on the wp-admin/customizer.php page I get an inline script that's identified in the Chrome devtools source panel as js-inline-concat-heartbeat,customize-widgets,react,react-dom,react-jsx-runtime,wp-polyfill,wp-url.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I like this, even if it is short-lived. 👍🏻

echo "/* ]]> */\n";
echo "</script>\n";
}
Expand Down Expand Up @@ -2394,8 +2395,9 @@ function _print_styles() {
$dir = $wp_styles->text_direction;
$ver = $wp_styles->default_version;

$concat = str_split( $concat, 128 );
$concatenated = '';
$concat_source_url = 'css-inline-concat-' . $concat;
$concat = str_split( $concat, 128 );
$concatenated = '';

foreach ( $concat as $key => $chunk ) {
$concatenated .= "&load%5Bchunk_{$key}%5D={$chunk}";
Expand All @@ -2407,6 +2409,7 @@ function _print_styles() {
if ( ! empty( $wp_styles->print_code ) ) {
echo "<style{$type_attr}>\n";
echo $wp_styles->print_code;
echo sprintf( "\n/*# sourceURL=%s */", rawurlencode( $concat_source_url ) );
echo "\n</style>\n";
}
}
Expand Down
1 change: 1 addition & 0 deletions tests/phpunit/tests/blocks/editor.php
Original file line number Diff line number Diff line change
Expand Up @@ -762,6 +762,7 @@ public function test_ensure_preload_data_script_tag_closes() {
<script src="{$baseurl}/wp-includes/js/dist/api-fetch.min.js?ver=test" id="wp-api-fetch-js"></script>
<script id="wp-api-fetch-js-after">
wp.apiFetch.use( wp.apiFetch.createPreloadingMiddleware( {"/test/v0/test-62797":{"body":["Unclosed comment and a script open tag \\u003C!--\\u003Cscript\\u003E"],"headers":{"Allow":"GET"}}} ) );
//# sourceURL=wp-api-fetch-js-after
</script>

HTML;
Expand Down
Loading
Loading