Repository navigation
Count script dependencies toward the enqueued size total (#74) - #1488
gunjanjaswal wants to merge 3 commits into
Conversation
Enqueued_Scripts_Size_Check only measured scripts whose src started with the plugin's own URL, so any dependency served from elsewhere (jQuery, a shared vendor library) was ignored even though it loads because of the plugin and adds to the page weight. Walk each plugin script's dependency graph, de-duplicated, and resolve every handle's src to a local file with a load_script_textdomain-style mapper (plugin dir, wp-content, wp-includes, site root). Dependencies count toward the page total; scripts served from another host, such as a CDN, are skipped since their size can't be read locally. Warnings still point only at the plugin's own files. Fixes WordPress#74.
|
The following accounts have interacted with this PR and/or linked issues. I will continue to update these lists as activity occurs. You can also manually ask me to refresh this list by adding the If you're merging code through a pull request on GitHub, copy and paste the following into the bottom of the merge commit message. To understand the WordPress project's expectations around crediting contributors, please review the Contributor Attribution page in the Core Handbook. |
There was a problem hiding this comment.
Copilot review overview
🟡 Changes recommended
Inline dependency payloads remain undercounted, and the required dependency-size reporting is missing.
Review effort: Balanced
Findings: 3
Open (4)
What changed in this PR
Updates the runtime script-size check to include measurable dependencies in cumulative page weight.
Changes:
- Traverses and de-duplicates script dependencies.
- Maps local script URLs to filesystem paths.
- Adds an uploads-based dependency fixture and PHPUnit coverage.
| File | Description |
|---|---|
includes/Checker/Checks/Performance/Enqueued_Scripts_Size_Check.php |
Adds dependency traversal, path resolution, and size accounting. |
tests/phpunit/tests/Checker/Checks/Enqueued_Scripts_Size_Check_Tests.php |
Tests direct external dependency accounting. |
tests/phpunit/testdata/plugins/test-plugin-enqueued-script-size-check/load.php |
Registers the external dependency fixture. |
💡 Add a code-review agent skill or configure MCP servers for context-aware, tailored reviews. Learn more in the docs.
| /* translators: 1: script file size. 2: tested URL. 3: threshold file size. */ | ||
| __( 'This script has a size of %1$s which in combination with the other scripts enqueued on %2$s exceeds the script size threshold of %3$s.', 'plugin-check' ), | ||
| size_format( $plugin_script['size'] ), | ||
| size_format( $measured[ $handle ]['size'] ), | ||
| $url, | ||
| size_format( $this->threshold_size ) |
| $uploads = wp_upload_dir(); | ||
| $dep_file = trailingslashit( $uploads['basedir'] ) . 'plugin-check-external-dependency.js'; | ||
|
|
||
| if ( ! file_exists( $dep_file ) ) { | ||
| file_put_contents( $dep_file, str_repeat( 'a', 1000 ) ); |
| public function test_run_counts_external_dependencies() { | ||
| // Load the test plugin, whose script depends on a 1000 byte script served from outside | ||
| // the plugin directory. | ||
| require UNIT_TESTS_PLUGIN_DIR . 'test-plugin-enqueued-script-size-check/load.php'; | ||
|
|
- Use ABSPATH . 'wp-includes/' instead of the WPINC constant, which the PHPStan config doesn't have stubbed. - Count a dependency's inline before/after code even when its file can't be resolved locally (e.g. a CDN script); WordPress still prints it. - Exercise a transitive dependency and a remote (skipped) dependency in the fixture, and clean up the fixture's uploads files on tear down.
|
Thanks for the review. Pushed b9cf12b to address it:
On the suggestion to track plugin vs dependency sizes separately and report both in the warning: I read #74 as asking for dependencies to be counted "as part of the total script size for a page load," which is what this does, rather than a change to what the warning reports. Happy to add a separate dependency breakdown if a maintainer wants it, but I left the warning format as-is to keep the change focused. |
There was a problem hiding this comment.
Copilot review overview
🟡 Changes recommended
The warning omits the separate dependency and total-size reporting required by issue #74.
Review effort: Balanced
Findings: 2
Open (4)
Resolved since last review (1)
| public function test_run_counts_external_dependencies() { | ||
| // The fixture's script depends on a 1000 byte script served from the uploads directory, | ||
| // which in turn depends on a 500 byte transitive one, plus a remote (CDN) script that | ||
| // can't be measured locally and must be skipped rather than fataling. |
|
Thanks for running the review again, @ernilambar. Quick status on where things stand after the latest push ( The one remaining item is the suggestion to report the dependency contribution separately in the warning. I read #74 as asking for dependencies to count toward the total (which they now do) rather than a change to the message, so I left the wording alone. That said, I'm happy to add a breakdown if you'd find it useful for triage, something like:
Want me to add that line (shown only when dependencies actually contribute), or keep the message as-is? Happy to go whichever way you prefer. |


What?
Enqueued_Scripts_Size_Checknow counts a plugin script's dependencies toward the cumulative page size, not just the scripts served from the plugin's own directory.Fixes #74
Why?
The check only measured scripts whose
srcstarted with the plugin's own URL. Anything a plugin script depends on but that's served from elsewhere, jQuery being the obvious one, was skipped entirely, even though it loads on the page because the plugin enqueued something that needs it. That undercounts the real weight the plugin adds to a page.How?
->deps, transitively, de-duplicated) and add every dependency's size to the page total.srcto a local file with aload_script_textdomain-style mapper that understands the plugin directory,wp-content,wp-includes, and the site root (including root-relative and protocol-relative srcs). Scripts served from another host, such as a CDN, can't be measured locally, so they're skipped rather than guessed at.I kept this scoped to the scripts check to keep the diff reviewable.
Enqueued_Styles_Size_Checkhas the same shape and the same gap, so I'm happy to follow up by converging the two into a shared base (as you suggested in the issue) and applying the dependency accounting there too, either in this PR or a separate one, whichever you prefer.AI Usage Disclosure
AI assistance (Claude Code) was used to explore the check and draft the dependency-walking and URL-to-path logic and the test. I reviewed the change, ran the check's PHPUnit tests and PHPCS locally, and take responsibility for what's here.
Testing Instructions
wp_enqueue_script( 'my-script', plugins_url( 'my-script.js', __FILE__ ), array( 'jquery' ) ).Automated: the new
test_run_counts_external_dependenciescase inEnqueued_Scripts_Size_Check_Tests.phpsets a threshold between the plugin script's own size and its size once a dependency is included, and asserts the warning only appears because the dependency is counted.