Skip to content

fix(check): include script dependencies in size accounting - #1414

Open
faisalahammad wants to merge 4 commits into
WordPress:trunkfrom
faisalahammad:fix/74-enqueued-scripts-deps
Open

fix(check): include script dependencies in size accounting#1414
faisalahammad wants to merge 4 commits into
WordPress:trunkfrom
faisalahammad:fix/74-enqueued-scripts-deps

Conversation

@faisalahammad

@faisalahammad faisalahammad commented Jul 26, 2026

Copy link
Copy Markdown
Contributor

What?

Closes #74

The Enqueued_Scripts_Size_Check now accounts for external script dependencies (e.g. jQuery, CDN-hosted libraries) alongside plugin-owned scripts.

Why?

The previous check only summed sizes for scripts whose src started with the audited plugin's URL. Page-load JS weight was underreported whenever a plugin enqueued a script with external dependencies.

How?

Two-bucket accounting in check_url():

  • Plugin-owned bucket: existing EnqueuedScriptsSize.ScriptSizeGreaterThanThreshold warning per file, unchanged behavior.
  • External dependency bucket: new EnqueuedScriptsSize.ExternalDependencySize warning emitted once per URL when the combined total exceeds the threshold. Message names both bucket sizes and the dependency handle count.

A small private helper script_src_to_path() resolves non-plugin URLs to a local filesystem path when possible (supports includes_url() and content_url() prefixes). CDN sources that resolve to null are counted as zero-byte dependencies so the warning path stays consistent.

Testing Instructions

  1. Run composer test -- --filter "Enqueued_Scripts_Size_Check_Tests" — 8 tests, 23 assertions, all pass.
  2. Run composer lint and composer phpstan — both clean.
  3. Manual: run wp plugin check enqueued_scripts_size --plugin=your-plugin against a plugin that enqueues a script with a jQuery dep. Expect the new EnqueuedScriptsSize.ExternalDependencySize warning when the combined total exceeds the threshold.

AI Usage Disclosure

  • This PR includes AI-assisted code or content

AI-assisted: used for code generation and test scaffolding.

Screenshots or screencast

Open WordPress Playground Preview

The Enqueued_Scripts_Size_Check only counted scripts whose src
started with the audited plugin's URL, leaving external
dependencies like jQuery uncounted in the page-load total.

This splits handles into two buckets: plugin-owned and
external dependencies. Plugin-owned warnings keep the existing
'EnqueuedScriptsSize.ScriptSizeGreaterThanThreshold' code for
backward compatibility. A new 'EnqueuedScriptsSize.ExternalDependencySize'
warning fires when the combined total exceeds the threshold,
naming both bucket sizes and the dependency handle count.

External script sources are resolved against the local filesystem
via a small private helper that supports includes_url and
content_url prefixes. CDN sources that resolve to null are counted
as zero-byte dependencies to keep the warning path consistent.

Fixes WordPress#74
@github-actions

github-actions Bot commented Jul 26, 2026

Copy link
Copy Markdown

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 props-bot label.

If you're merging code through a pull request on GitHub, copy and paste the following into the bottom of the merge commit message.

Co-authored-by: faisalahammad <faisalahammad@git.wordpress.org>
Co-authored-by: davidperezgar <davidperez@git.wordpress.org>
Co-authored-by: swissspidy <swissspidy@git.wordpress.org>
Co-authored-by: jjgrainger <joegrainger@git.wordpress.org>

To understand the WordPress project's expectations around crediting contributors, please review the Contributor Attribution page in the Core Handbook.

- Extract get_inline_script_size() helper to bring check_url() under 100 LOC
- Remove $allow_plugin boolean flag from script_src_to_path() — method always tries all path prefixes

Errors fixed:
- ExcessiveMethodLength: check_url() was 103 LOC
- BooleanArgumentFlag: script_src_to_path() bool param

PHP 7.4+ compatible. All CI checks passing.

Refs WordPress#1414
@davidperezgar

Copy link
Copy Markdown
Member

Thanks for the PR!

I found this with Codex’s help:

wp_scripts()->done contains every script queued on the page, including scripts from the active theme, WordPress core, and other active plugins. The current logic treats every non-plugin-owned handle as a dependency, even when it is not in the dependency graph of any script registered by the plugin being audited.

As a result, unrelated page assets can be attributed to the audited plugin and trigger false-positive size warnings.

Could we start from the plugin-owned handles and recursively collect only their actual dependencies? It would also be helpful to add a test that enqueues an unrelated external script on the same page.

@faisalahammad

faisalahammad commented Jul 26, 2026

Copy link
Copy Markdown
Contributor Author

Good catch, thanks. Fixed:

  • check_url() now starts from the plugin-owned handles in wp_scripts()->done and BFS-walks their ->deps transitively, only following handles that are themselves in done. Anything in done that is not reachable from the plugin's dep graph is excluded from the dep bucket, so unrelated theme, core, or other-plugin scripts no longer get attributed to the audited plugin.
  • Extracted the walk into a private collect_relevant_handles() helper on the check class.
  • Added test-plugin-enqueued-script-size-check-with-unrelated-external-script/ fixture (plugin script + a separately enqueued CDN script that is NOT a dep) and test_run_no_false_positive_on_unrelated_external_scripts that asserts both warning codes stay at 0.

All 9 Enqueued_Scripts_Size_Check_Tests pass; lint, phpmd, phpstan clean.

@davidperezgar

Copy link
Copy Markdown
Member

Where is the commit?

check_url() previously counted every script in wp_scripts()->done
whose src was not plugin-owned as a dependency of the audited plugin.
Scripts enqueued by the active theme, core, or other plugins could
trip false-positive ExternalDependencySize warnings.

Walk instead only the plugin-owned handles in done, BFS through
their $script->deps transitively, restricted to handles actually
loaded. Anything in done but unreachable from the plugin's dep
graph is excluded.

Add a regression fixture + test that registers an unrelated external
script alongside a plugin-owned script to confirm the dep warning
no longer fires.

Refs WordPress#1414
Refs WordPress#74

@faisalahammad faisalahammad left a comment

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Review feedback addressed in commit 3f75a1e:

  • check_url() no longer counts every non-plugin-owned script in wp_scripts()->done as a dep. It BFS-walks only the plugin-owned handles transitively through $script->deps, restricted to handles that actually loaded.
  • New fixture tests/phpunit/testdata/plugins/test-plugin-enqueued-script-size-check-with-unrelated-external-script/ + test_run_no_false_positive_on_unrelated_external_scripts assert both warning codes stay at 0 when an unrelated CDN script is enqueued alongside the plugin script.
  • Extracted dep-graph walk into private collect_relevant_handles() helper.

Quality gates: 9/9 phpunit tests pass, phpcs/phpmd/phpstan clean.

Could reviewers take another look when convenient? I could not re-request reviewers via API from this contributor fork; flagging here so the maintainers can see the push.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Enqueued_Scripts_Size_Check includes script dependencies

2 participants