Repository navigation
fix(minify): strip line comments after the closing line of a multi-line value - #427
Conversation
…ne value stripLineComment required balanced parentheses before `//` on the same line, so `); // comment` closing a value opened on an earlier line kept the comment. After lines were joined, styled-components stripped from `//` to the end of the template, silently dropping the declarations and nested blocks that followed. Ignore closing parentheses without a match on the line when deciding whether `//` is inside a value.
🦋 Changeset detectedLatest commit: 1bcfdd5 The changes in this PR will be included in the next version bump. This PR includes changesets to release 1 package
Not sure what this means? Click here to learn what changesets are. Click here if you're a maintainer who wants to add another changeset to this PR |
|
@pullfrog review |
There was a problem hiding this comment.
✅ No new issues found.
Reviewed changes
countOpenParenshelper (src/minify/index.js:31): walks a line counting parens left open, ignoring a)with no match inside the line so an unmatched close from a value opened on an earlier line no longer looks unbalanced.stripLineCommentpredicate (src/minify/index.js:75): replacedcountOccurences(str, '(') === countOccurences(str, ')')withcountOpenParens(str) === 0, so); // commentafter a multi-line value is treated as a comment and stripped before lines are joined.- Tests (
test/minify/index.test.js): seven new cases covering the closing-paren fix, interpolations in the multi-line value, comments inside multi-line values,url(//...)after an unmatched),url(http://...), and//inside strings. - Changeset: patch entry describing the fix and the truncation of
url(//...)it also resolves.
The predicate change is sound: a stripped // is now only kept when an open paren actually precedes it on the line, and a url(/function ( opened before the marker still leaves countOpenParens > 0, so genuine // value content is preserved. I confirmed all 70 tests pass locally on this branch.
ℹ️ The documented behavior change is safe on released styled-components
The PR notes one intentional flip: ), repeat( // c (which closes a paren and opens a new function before the marker) now keeps the comment instead of stripping it, matching the pre-existing repeat( // c handling. I verified the load-bearing assumption behind this — that the runtime leaves // inside parentheses untouched — against first-party sources for the released v6 line, so a kept comment cannot run to the end of the joined template.
Technical details
# `//` inside parens is preserved by the runtime
The minifier joins lines with a single space, so a comment that survives minification would run to the end of the template if the runtime stripped `//` unconditionally. It does not, for the pattern this PR now keeps:
## Affected site
- `src/minify/index.js:75` — `countOpenParens(str) === 0`; a kept `//` implies an unmatched `(` precedes it on the same line.
## Evidence
- styled-components v6 pins `[email protected]`, whose parser consumes a balanced `(...)` group opaquely via `delimit(...)`; the `//` comment handler (`case 47`) is only reached at top level.
- The pre-stylis `normalize` step in the released 6.6.0 bundle skips comment/brace detection while `parenDepth > 0`, so `url(https://...)` and friends are preserved verbatim.
- Net effect: `url(//...)` is preserved (the case this PR fixes), and the contrived `), repeat( // c` yields an invalid single declaration the browser drops rather than swallowing following rules.
## Open questions for the human
- None blocking. The `), func( //` flip only changes that one declaration and is already documented in the PR body.deepseek-v4.1-flash (free via Pullfrog for OSS) | 𝕏
|
@seongwon030 thank you! |

Summary
Refs styled-components/styled-components#5613.
With the default
minify: true, a//comment on the closing line of a multi-line value survives minification, and styled-components then drops everything after it in the template:.x{grid-template-rows:...;}.x .sc-y{color:red;}.x{grid-template-rows:auto auto minmax( 0,1fr );}(nested block gone).x{grid-template-rows:auto auto minmax( 0,1fr );}.x .sc-y{color:red;}Cause
stripLineCommentonly treats//as a comment when the text before it on that line has as many(as). On); // long commentthe)closes a paren opened on an earlier line, so the line looks unbalanced and the comment is kept.minifythen joins lines with' ', and the runtime strips from//to the next newline, which is now the end of the template.Fix
Count only parens left open within the line, ignoring a
)that has no match on that line (countOpenParens(str) === 0). The check stays per line on purpose: tracking depth across lines would keep comments inside a multi-line value (calc(\n 10px // c\n + 20px\n)), and after the join they would swallow the rest of the template. That case has a test now.The same change stops truncating
url(//...)after an unmatched). For example,linear-gradient(\n red, blue\n), url(//cdn.example.com/x.png); // cpreviously minified to...),url(.One behavior change:
), repeat( // c(closing one paren and opening another on the same line) used to be stripped and is now kept, the same as the existingrepeat( // ccase. Only that declaration is affected, since the runtime leaves//inside parens alone; following rules survive.Tests
test/minify/index.test.js, each checked through bothminifyRawandminifyCooked. 4 fail onmainand pass with this change; 3 are regression guards (comments inside multi-line values,url(//...),url(http://...),//in strings).pnpm install --frozen-lockfile && pnpm test: 70/70 on Node 22 and 24.