Repository navigation
Count script dependencies toward the enqueued size total (#74) - #1488
gunjanjaswal wants to merge 8 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. 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. Props Bot commandsYou can trigger Props Bot actions by commenting on this PR:
|
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. |
|
Circling back on this, @ernilambar. After the last re-review three of the four findings were cleared and CI's still green, so it feels close. Is there anything outstanding on the remaining point, or anything else you'd like changed before it can move forward? Happy to jump on it. |
| foreach ( $roots as $root ) { | ||
| list( $url_root, $dir_root ) = $root; | ||
|
|
||
| if ( ! $url_root || strpos( $src, $url_root ) !== 0 ) { |
A same-host dependency can be served over https while the site's URL roots (plugins_url, content_url, includes_url, site_url) are registered over http. The prefix match in get_local_path_from_roots() was scheme-sensitive, so such a dependency failed every root check and had its size silently counted as zero, which could suppress the enqueued size-threshold warning. Normalize both the source and each URL root to a single scheme before the prefix comparison, keeping the host and path checks intact. Adds a test covering an https dependency against http roots.
Issue WordPress#74 asks for the dependency total to be reported distinctly from the plugin's own enqueued scripts. The warning now states the combined size of the plugin's scripts and the additional weight their dependencies add, instead of folding everything into a single figure. Also replaces a non-standard 'fataling' in a test comment.
|
Pushed two changes. The scheme-sensitive matching is fixed. I also split the reporting to line up with the acceptance criteria on #74: the warning now gives the plugin's own enqueued-script total and the dependency contribution as separate figures, rather than folding everything into one number. Minor cleanup too, dropped the non-standard "fataling" in the transitive-dependency test comment. The older note about the fixture file lingering in uploads is already covered, the test's |
The size check resolves dependency srcs served root-relative (core's /wp-includes/... scripts) and protocol-relative (//host/path), but only fully-qualified and scheme-mismatched srcs were exercised. Add a test for both branches of get_path_from_src so a regression that counts a core dependency as zero bytes is caught.
|
Gentle nudge on this one. It's been green for a few weeks and is just waiting on a first maintainer look. It teaches the enqueued-scripts size check to count a script's dependencies toward the page total (per #74), reporting the plugin's own weight and the dependency weight separately. Happy to adjust anything if there's a preference on the message wording or the threshold handling. |
SummaryCounts each plugin script's dependencies (transitive and de-duplicated, including core ones like jQuery) toward the page's total script size, and reports the plugin's own size and the dependency size as separate numbers. This meets the main requirement of #74. CI, PHPCS and PHPStan all pass. Still to fix: the ✅ What's good
|
The warning text said it counted the scripts enqueued on a URL when it actually measures this plugin's own scripts and the dependencies they pull in, so reword it to say that and spell out the combined total. Drop the dependency clause when a script has no measurable dependencies. Correct the @SInCE tags on the helpers added for dependency counting to the upcoming release, and add a test that asserts the plugin size, the dependency size and the combined total are each reported, so a regression in the plugin/dependency split is caught instead of passing on the warning count alone.
|
Thanks for the thorough review, @ernilambar. Pushed 021d428 with the three must-fixes.
Warning text. Reworded to: "The scripts from this plugin on %s total %s, and their dependencies add a further %s, for a combined %s, which exceeds the script size threshold of %s." That labels the plugin figure as the plugin's own, states the combined total outright, and there's a shorter variant without the dependency clause when Test for the split. Optionals:
|
… limit Reporting the plugin and dependency sizes separately pushed check_url() past PHPMD's 100-line method limit. Move the message building into a small helper so the method stays within it; the message text is unchanged.
|
Thanks @ernilambar, appreciate you going through this so carefully across the rounds. #1557 is open for the scripts/styles convergence whenever that's worth picking up. |


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.