Fix token fallback plugins rewriting var() inside string literals - #82209
Fix token fallback plugins rewriting var() inside string literals#82209dilipom13 wants to merge 2 commits into
Conversation
Skip quoted CSS strings and JS comments when injecting --wpds-* fallbacks so PostCSS, esbuild, and Vite match Lightning CSS behavior. Co-authored-by: Cursor <cursoragent@cursor.com>
|
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. |
Co-authored-by: Cursor <cursoragent@cursor.com>
|
@dilipom13 Thanks for the PR! Just a quick note: Good First Issues are intended to help new WordPress contributors get started, so they’re generally reserved for new contributors. |
Okay @shail-mehta Thanks for your appreciation. |
| if ( char === '/' && next === '/' ) { | ||
| const end = source.indexOf( '\n', index ); | ||
| const sliceEnd = end === -1 ? source.length : end; | ||
| result += source.slice( index, sliceEnd ); | ||
| index = sliceEnd; | ||
| continue; | ||
| } | ||
|
|
||
| if ( char === '/' && next === '*' ) { | ||
| const end = source.indexOf( '*/', index + 2 ); | ||
| const sliceEnd = end === -1 ? source.length : end + 2; | ||
| result += source.slice( index, sliceEnd ); | ||
| index = sliceEnd; | ||
| continue; | ||
| } | ||
|
|
||
| if ( char === '"' || char === "'" ) { | ||
| const end = readQuotedSegment( source, index ); | ||
| const literal = source.slice( index, end ); | ||
| const quote = literal[ 0 ]; | ||
| const inner = literal.slice( 1, -1 ); | ||
| const transformed = transformCssValue( | ||
| inner, | ||
| tokenFallbacks, | ||
| options | ||
| ); | ||
| result += quote + transformed + quote; | ||
| index = end; | ||
| continue; | ||
| } |
There was a problem hiding this comment.
This scanner treats quote and comment markers inside regular-expression literals as JavaScript syntax.
For example, const re = /["']/; const s = 'var(--wpds-border-radius-sm)'; becomes invalid JavaScript.
Maybe we should use a real JS/TS parser or lexer to locate string and template contents and add regex-literal coverage?
| if ( source[ index ] === '$' && source[ index + 1 ] === '{' ) { | ||
| const expressionEnd = readTemplateExpression( source, index + 2 ); | ||
| content += source.slice( index, expressionEnd ); | ||
| index = expressionEnd; | ||
| continue; | ||
| } |
There was a problem hiding this comment.
Copying each template expression unchanged means CSS strings nested inside it no longer receive fallbacks. Please keep transforming string and template literals inside expressions and add a regression test for a conditional CSS value.
Example of a regression
This input was transformed at the base revision but is left unchanged at this head:
const styles = `${ enabled ? 'var(--wpds-border-radius-sm)' : '' }`;The published esbuild and Vite plugins support JS/TS source, and @wordpress/build enables the esbuild plugin automatically when @wordpress/theme is installed. Leaving this reference bare defeats the fallback plugin when the design-token stylesheet is unavailable.
|
Also, this PR feels a bit overcomplicated / overengineered. Potentially a cleaner design would be:
Although let's wait for feedback from @WordPress/gutenberg-components and @manzoorwanijk before proceeding. |
|
I added my thoughts on the original issue, as there doesn't seem to be an assessment of the cost/benefit. (Not necessarily against it, but just saying that it should be a reasoned decision.) |
|
+1 as of current state, not really worth complicating this much. If there's a simple elegant solution, then it would make sense. |
|
Right, let's close this PR for now, since there is a general agreement that the solution complexity isn't worth the benefits. @dilipom13 , thank you for working on this anyway! In the future, probably better to coordinate on the original issue before working on a PR. |
What
Fixes #82169
PostCSS, esbuild, and Vite token-fallback plugins were incorrectly rewriting
var(--wpds-*)inside quoted CSS strings and JS comments. They now skip those regions, matching Lightning CSS behavior.How to test
npm run test:unit -- --testPathPatterns=packages/theme/postcss-plugins/test/add-fallback-to-varnpm run test:unit -- --testPathPatterns=packages/theme/src/test/build-plugin-parity