Make WordPress Core


Ignore:
Timestamp:
09/08/2025 11:25:40 AM (2 months ago)
Author:
jonsurrell
Message:

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 https://github.com/WordPress/wordpress-develop/pull/9672.

Follow-up to [60685], [60690].

Props jonsurrell, westonruter, wildworks, peterwilsoncc, johnbillion, tobiasbg.
Fixes #63887.

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/src/wp-includes/class-wp-scripts.php

    r60704 r60719  
    221221        if ( ! $output ) {
    222222            return;
     223        }
     224
     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            );
    223237        }
    224238
     
    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    }
     
    697722        }
    698723
     724        $source_url = rawurlencode( "{$handle}-js-translations" );
     725
    699726        $output = <<<JS
    700727( function( domain, translations ) {
     
    703730    wp.i18n.setLocaleData( localeData, domain );
    704731} )( "{$domain}", {$json_translations} );
     732//# sourceURL={$source_url}
    705733JS;
    706734
Note: See TracChangeset for help on using the changeset viewer.