@wordpress/theme: deduplicate addFallbackToVar helper#78666
Conversation
|
Size Change: 0 B Total Size: 8.18 MB ℹ️ View Unchanged
|
|
Flaky tests detected in 09abf9f. 🔍 Workflow run URL: https://github.com/WordPress/gutenberg/actions/runs/26443059759
|
|
Where was |
|
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. |
@aduth the duplication here was introduced by design, as explained in #75586 's description:
So, in this PR, we continue to honor the approach, but apply an optimization that was initially deferred |
Extract the regex/replacement logic into a single generic `add-fallback-to-var.mjs` that takes the token fallback map as an argument. `ds-token-fallbacks.mjs` now becomes a thin wrapper that prebinds the package's generated fallback map, so the PostCSS, esbuild, and Vite plugins keep their existing import and behavior. Addresses task B4 from #77462.
8073d98 to
44f9d91
Compare
aduth
left a comment
There was a problem hiding this comment.
It looks like an effective deduplication, but I'm still confused why we're maintaining a function that is not used internally or externally, or even accessible externally. Feels like YAGNI.
|
I'll let @mirka clarify too, but my understanding is mostly for ease of testing |
What?
Addresses task B4 from #77462 — deduplicates the
addFallbackToVarhelper that lived in bothpostcss-plugins/add-fallback-to-var.tsandpostcss-plugins/ds-token-fallbacks.mjs.Why?
The regex + replacement logic was copy-pasted across two files, with comments warning future maintainers to keep them in sync. That's exactly the kind of duplication the stabilization assessment flagged as risk-prone.
How?
packages/theme/src/postcss-plugins/add-fallback-to-var.mjsholds the single generic implementation (takes the token fallback map as a parameter, with optionalescapeQuotes). JSDoc preserves the type information that the previous.tsfile expressed inline.packages/theme/src/postcss-plugins/ds-token-fallbacks.mjsis now a thin wrapper that pre-binds the package's generated fallback map and re-exportsaddFallbackToVar. The PostCSS, esbuild, and Vite plugins keep their existing imports unchanged.add-fallback-to-var.tsis removed; its companion unit test now imports the new.mjssource.Testing Instructions
npm run test:unit -- packages/theme— all 38 tests still pass (including the 14-caseadd-fallback-to-varsuite and the 5-case preboundds-token-fallbackssuite)../postcss-plugins/postcss-ds-token-fallbacks,./esbuild-plugins/esbuild-ds-token-fallbacks, and./vite-plugins/vite-ds-token-fallbacksentry points are unchanged on disk, so external consumers see no behavior change.Checklist