Fix token fallback plugins breaking JS strings with quoted font names#76254
Fix token fallback plugins breaking JS strings with quoted font names#76254
Conversation
|
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. |
59b5ba7 to
09d74bf
Compare
Card and CollapsibleCard primitives to @wordpress/uiThere was a problem hiding this comment.
Pull request overview
Fixes JS/TS build-time token fallback injection so fallback values containing quoted font names (e.g. "Segoe UI") don’t break surrounding JS string literals in @wordpress/theme Vite/esbuild pipelines.
Changes:
- Add
escapeQuotesoption toaddFallbackToVarto escape"/'in injected fallback values. - Enable
escapeQuotes: truein the Vite and esbuild token fallback plugins for JS/TS transforms. - Expand JSDoc for
addFallbackToVarto document the new option and its intended use.
Reviewed changes
Copilot reviewed 3 out of 3 changed files in this pull request and generated 2 comments.
| File | Description |
|---|---|
| packages/theme/src/vite-plugins/vite-ds-token-fallbacks.mjs | Passes escapeQuotes: true when injecting fallbacks into JS/TS during Vite transforms. |
| packages/theme/src/postcss-plugins/ds-token-fallbacks.mjs | Adds escapeQuotes option and escaping logic for fallback injection. |
| packages/theme/src/esbuild-plugins/esbuild-ds-token-fallbacks.mjs | Passes escapeQuotes: true when injecting fallbacks into JS/TS during esbuild loads. |
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
|
Size Change: 0 B Total Size: 6.89 MB ℹ️ View Unchanged
|
There was a problem hiding this comment.
Pull request overview
Copilot reviewed 5 out of 5 changed files in this pull request and generated no new comments.
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
|
Flaky tests detected in 15c33ae. 🔍 Workflow run URL: https://github.com/WordPress/gutenberg/actions/runs/22780899036
|
The Vite and esbuild design-system token fallback plugins inject fallback values into `var()` calls within JS/TS source files. When a fallback value contains quoted font names (e.g. `"Segoe UI"`), the injected double quotes can break the enclosing JS string literal, causing "Unexpected identifier" errors at runtime. Add an `escapeQuotes` option to `addFallbackToVar` that escapes `"` and `'` in fallback values. JS unescapes them at parse time, so the browser's CSS engine still receives the correct unquoted characters. Both the Vite and esbuild plugins now use this option. Made-with: Cursor
Keep the TypeScript and MJS implementations of `addFallbackToVar` in sync by adding the same `escapeQuotes` option to the TS version. Add unit tests covering the new option: escaping double quotes, single quotes, mixed quotes, and verifying no-op behavior for values without quotes. Made-with: Cursor
e2e3b0e to
15c33ae
Compare
| let fallback = tokenFallbacks[ tokenName ]; | ||
| if ( fallback === undefined ) { | ||
| return match; | ||
| } | ||
| if ( escapeQuotes ) { | ||
| fallback = fallback | ||
| .replaceAll( '"', '\\"' ) | ||
| .replaceAll( "'", "\\'" ); | ||
| } | ||
| return `var(${ tokenName }, ${ fallback })`; |
There was a problem hiding this comment.
👀 I see the surface area of the duplicated logic between the ts and mjs files have increased, but still probably fine to leave this way for now.
Summary
@wordpress/themeVite and esbuild token fallback plugins where injecting fallback values containing quoted CSS font names (e.g."Segoe UI") into JS/TS string literals would break the enclosing string, causingUnexpected identifierruntime errors.escapeQuotesoption toaddFallbackToVar(both the.mjsand.tsimplementations) that escapes"and'in fallback values before injection. JS unescapes them at parse time, so the browser's CSS engine still receives the correct characters.escapeQuotesoption.Test plan
fontFamily: 'var(--wpds-font-family-body)'and confirm it renders without errors.false).npm run test:unit -- packages/theme/src/postcss-plugins/test/add-fallback-to-var.test.ts— all 14 tests pass.