Repository navigation
fix(only-include-used-components): guard both maps against silent drift - #515
Conversation
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.
There was a problem hiding this comment.
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
publicModuleCoveragetests 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
dsfrComponentDetectionClassPrefixestests that validate each detection prefix actually opens a selector in the component’s own DSFR stylesheet. - Fix
contentdetection by removingfr-responsive-img/fr-responsive-vidprefixes 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.
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.
|
Hello @kevbarns, Thanks a lot for all the work you've done on this. I'll merge and deploy a new version. |
Closes #514.
Two hand-written maps in
only-include-used-componentshad no guard against drift. This adds one test per map and fixes the drift the second test found.Fix:
contentwas detected on core utility classesfr-responsive-imgandfr-responsive-vidwere listed as detection prefixes forcontent, but their base rules (and the 7 ratio modifiers) live incore.main.css, always included. All 8 occurrences incontent.main.cssare scoped under.fr-content-media, none unscoped, so dropping them loses nothing.Measured end to end, consumer using
fr-responsive-imgwithoutfr-content-media: 3 components included instead of 2,dsfr.min.css234 877 B instead of 231 924 B. A consumer that does usefr-content-mediastill gets the component, descendant rules included.Tests
publicModuleCoverageenumerates the public subpaths from the realsrc/layout, minus whatsrc/tsconfig.jsonexcludes, runs them through the production module id extraction, and asserts each resolves.dsfrComponentDetectionClassPrefixesasserts 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:
Highlightremoved from the map; a new component as a flat file, as a directory with an index, and as a directory without one; a newblocks/*entry;fr-responsive-imgreintroduced; 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. Notesrc/tsconfig.jsonexcludes./bin, sotsc -p srcdoes not cover the changed file;tsc -p src/bindoes.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
contentbug 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.