Skip to content

fix(only-include-used-components): guard both maps against silent drift - #515

Merged
garronej merged 3 commits into
mainfrom
fix/content-detection-over-inclusion
Aug 30, 2026
Merged

garronej merged 3 commits into
mainfrom
fix/content-detection-over-inclusion

Conversation

@kevbarns

Copy link
Copy Markdown
Collaborator

Closes #514.

Two hand-written maps in only-include-used-components had no guard against drift. This adds one test per map and fixes the drift the second test found.

Fix: content was detected on core utility classes

fr-responsive-img and fr-responsive-vid were listed as detection prefixes for content, but their base rules (and the 7 ratio modifiers) live in core.main.css, always included. All 8 occurrences in content.main.css are scoped under .fr-content-media, none unscoped, so dropping them loses nothing.

Measured end to end, consumer using fr-responsive-img without fr-content-media: 3 components included instead of 2, dsfr.min.css 234 877 B instead of 231 924 B. A consumer that does use fr-content-media still gets the component, descendant rules included.

Tests

publicModuleCoverage enumerates the public subpaths from the real src/ layout, minus what src/tsconfig.json excludes, runs them through the production module id extraction, and asserts each resolves. dsfrComponentDetectionClassPrefixes asserts every prefix opens a selector in its own component stylesheet, read from the installed @gouvfr/dsfr.

Both were checked against the inputs that make them answer false, not only the nominal ones. Seven mutations, each failing with the offending name: Highlight removed from the map; a new component as a flat file, as a directory with an index, and as a directory without one; a new blocks/* entry; fr-responsive-img reintroduced; a typo'd prefix; a prefix pointing at the wrong component.

Verified

yarn test (27 files, 108 tests), tsc -p src/bin, eslint, prettier, plus the end to end A/B above on a built artifact. Note src/tsconfig.json excludes ./bin, so tsc -p src does not cover the changed file; tsc -p src/bin does.

Scope

The module map is asserted to be complete, not correct. A mapping missing a transitive dependency would under-include and still pass. I probed for that by detecting DSFR classes in each module's own sources: it surfaced 4 candidates, all false positives caused by the content bug above. Once that is fixed there is no signal left on that path, and a real guard would need to follow the React import graph. Out of scope here.

REACT_DSFR_MODULE_TO_DSFR_COMPONENTS had no exhaustiveness check. A module
missing from it and from NON_COMPONENT_MODULE_IDS makes
resolveModuleIdToDsfrComponents return undefined, which trips the
include-everything fail-safe: the script warns, exits 0, and trims nothing.
Adding a component to src/ was enough to silently disable the optimization
for every consumer importing it.

Enumerate the public subpaths from the real src/ layout, minus what
src/tsconfig.json excludes, run them through the production module id
extraction, and assert each one resolves.
…sses

fr-responsive-img and fr-responsive-vid were listed as detection prefixes for
the content component, but they are core utility classes: their base rules,
and all 7 ratio modifiers, live in core.main.css, which is always included.
Every rule content.css has for them is scoped under .fr-content-media, which
is already a prefix on its own, so nothing is lost by dropping them.

Measured end to end on a consumer using fr-responsive-img without
fr-content-media: 3 components included instead of 2, dsfr.min.css 234877 B
instead of 231924 B. A consumer that does use fr-content-media still gets the
component, descendant rules included.

Add a test that re-derives the whole prefix map against the installed
@gouvfr/dsfr: a prefix must open a selector in its own component stylesheet,
not merely appear in it. Extract the css extension fallback list so the test
uses the same one as the production lookup.
Copilot AI lite review requested due to automatic review settings August 29, 2026 12:12

Copilot AI left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Pull request overview

This PR adds automated guards to prevent silent drift in only-include-used-components by introducing tests that validate (1) module-to-component map exhaustiveness and (2) DSFR class-prefix detection correctness, and it fixes an over-inclusion bug for content caused by core utility class prefixes.

Changes:

  • Add publicModuleCoverage tests to ensure every public @codegouvfr/react-dsfr/<subpath> resolves to a known module id (or non-component id) rather than triggering the include-everything fail-safe.
  • Add dsfrComponentDetectionClassPrefixes tests that validate each detection prefix actually opens a selector in the component’s own DSFR stylesheet.
  • Fix content detection by removing fr-responsive-img / fr-responsive-vid prefixes and centralize DSFR component CSS extension preference ordering.

Reviewed changes

Copilot reviewed 2 out of 3 changed files in this pull request and generated 1 comment.

File Description
test/runtime/scripts/onlyIncludeUsedComponents/publicModuleCoverage.test.ts Adds exhaustiveness guard for module-id resolution and protects the optimization from silently disabling itself.
test/runtime/scripts/onlyIncludeUsedComponents/dsfrComponentDetectionClassPrefixes.test.ts Adds validation that detection prefixes map to selectors actually owned by each DSFR component stylesheet.
src/bin/only-include-used-components.ts Fixes content detection drift and introduces a shared DSFR_COMPONENT_CSS_FILE_EXTENSIONS constant used by the runtime script and tests.

💡 Add a code-review agent skill or configure MCP servers for context-aware, tailored reviews. Learn more in the docs.

@kevbarns kevbarns self-assigned this Aug 29, 2026
getPublicSubpaths listed every child of an index-less directory, including
nested directories that are not importable: src/tools/powerhooks has no index.
The module id set was unaffected, since those children collapse onto the same
first segment, but the helper did not match its own docblock.

Keep a child directory only when it has an index of its own, which drops
tools/powerhooks and keeps tools/StatefulObservable. Child files stay whatever
their extension: src/assets holds only .svg and .css, and filtering on .ts
there would drop the "assets" module id and leave it silently unchecked.

Assert the resolved module ids cover one case of each shape so that narrowing
the enumeration fails loudly instead of shrinking what the guard looks at.
@garronej

Copy link
Copy Markdown
Collaborator

Hello @kevbarns,

Thanks a lot for all the work you've done on this.
I haven't followed up in detail the developement of this feature but I trust @lsagetlethias jugement who seems to have dug deep in the code.

I'll merge and deploy a new version.

@garronej
garronej merged commit 190ffc7 into main Aug 30, 2026
6 checks passed
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.

only-include-used-components: both hand-written maps can drift silently

3 participants