Skip to content

Conversation

@overlookmotel
Copy link
Member

@overlookmotel overlookmotel commented Jul 11, 2025

Simplify CSS minification.

  • Remove the step of adding placeholders, which means we also don't need to remove them again, and don't need a hashmap to track which quasis are retained. Instead, just work through the quasis one by one.
  • Remove the intermediate new_raws Vec.
  • Skip \\, \', \" in byte search loop, to avoid backtracking to check for \" when exiting a string.

The whole thing squashes down to a single function.

I believe I've kept the behavior exactly as it was before. Is there anything I'm missing?

There's one bit of the logic I don't understand. /* ... */ is replaced with a space, unless there's a space preceding / following it. But I don't think we need to add a space in cases like height:/* big */${10000}px. Do we?

@github-actions github-actions bot added A-transformer Area - Transformer / Transpiler C-performance Category - Solution not expected to change functional behavior, only performance labels Jul 11, 2025
Copy link
Member Author

overlookmotel commented Jul 11, 2025


How to use the Graphite Merge Queue

Add either label to this PR to merge it via the merge queue:

  • 0-merge - adds this PR to the back of the merge queue
  • hotfix - for urgent hot fixes, skip the queue and merge this PR next

You must have a Graphite account in order to use the merge queue. Sign up using this link.

An organization admin has enabled the Graphite Merge Queue in this repository.

Please do not merge from GitHub as this will restart CI on PRs being processed by the merge queue.

This stack of pull requests is managed by Graphite. Learn more about stacking.

@overlookmotel overlookmotel marked this pull request as ready for review July 11, 2025 15:55
@overlookmotel overlookmotel requested a review from Dunqing as a code owner July 11, 2025 15:55
@codspeed-hq
Copy link

codspeed-hq bot commented Jul 11, 2025

CodSpeed Instrumentation Performance Report

Merging #12224 will not alter performance

Comparing 07-11-perf_transformer_styled-components_simplify_css_minification (8e13e2a) with main (9b14fbc)

Summary

✅ 34 untouched benchmarks

@overlookmotel overlookmotel force-pushed the 07-11-perf_transformer_styled-components_simplify_css_minification branch from 5fbf952 to 2adcdea Compare July 12, 2025 12:31
@overlookmotel overlookmotel force-pushed the 07-11-perf_transformer_styled-components_simplify_css_minification branch from 2adcdea to f14740e Compare July 12, 2025 12:36
@Dunqing
Copy link
Member

Dunqing commented Jul 13, 2025

There's one bit of the logic I don't understand. /* ... */ is replaced with a space, unless there's a space preceding / following it. But I don't think we need to add a space in cases like height:/* big */${10000}px. Do we?

This is what the Babel plugin does, I don't know why.

https://github.com/styled-components/babel-plugin-styled-components/blob/4e2eb388d9c90f2921c306c760657d059d01a518/src/minify/index.js#L91-L92

@overlookmotel
Copy link
Member Author

overlookmotel commented Jul 13, 2025

As far as I can see, Babel plugin will not insert a space in place of the comment in `height:/* big */100px`, because it minifies in 2 passes - (1) remove comments and then (2) remove whitespace. So it does insert a space in 1st pass, but then removes it again in 2nd pass.

We're doing it all in 1 pass, so I think we need to check for "symbols" (:{},;) before/after the comment as well when deciding whether to insert a space in place of a comment or not.

But that's a separate issue from this PR. I don't think this PR changes behavior from what it was before.

@Dunqing
Copy link
Member

Dunqing commented Jul 13, 2025

As far as I can see, Babel plugin will not insert a space in place of the comment in `height:/* big */100px`, because it minifies in 2 passes - (1) remove comments and then (2) remove whitespace. So it does insert a space in 1st pass, but then removes it again in 2nd pass.

We're doing it all in 1 pass, so I think we need to check for "symbols" (:{},;) before/after the comment as well when
deciding whether to insert a space in place of a comment or not.

I see, I can't see any reason it would block us from removing this space.

@graphite-app graphite-app bot added the 0-merge Merge with Graphite Merge Queue label Jul 13, 2025
@graphite-app
Copy link
Contributor

graphite-app bot commented Jul 13, 2025

Merge activity

Simplify CSS minification.

* Remove the step of adding placeholders, which means we also don't need to remove them again, and don't need a hashmap to track which quasis are retained. Instead, just work through the quasis one by one.
* Remove the intermediate `new_raws` `Vec`.
* Skip `\\`, `\'`, `\"` in byte search loop, to avoid backtracking to check for `\"` when exiting a string.

The whole thing squashes down to a single function.

I *believe* I've kept the behavior exactly as it was before. Is there anything I'm missing?

There's one bit of the logic I don't understand. `/* ... */` is replaced with a space, unless there's a space preceding / following it. But I don't think we need to add a space in cases like `height:/* big */${10000}px`. Do we?
@graphite-app graphite-app bot force-pushed the 07-11-perf_transformer_styled-components_simplify_css_minification branch from f933d20 to 8e13e2a Compare July 13, 2025 18:20
@graphite-app graphite-app bot merged commit 8e13e2a into main Jul 13, 2025
23 checks passed
@graphite-app graphite-app bot removed the 0-merge Merge with Graphite Merge Queue label Jul 13, 2025
@graphite-app graphite-app bot deleted the 07-11-perf_transformer_styled-components_simplify_css_minification branch July 13, 2025 18:28
graphite-app bot pushed a commit that referenced this pull request Aug 29, 2025
…ession in CSS minification (#13370)

Fix handling block comments which contain an expression. Previously if that block comment ended exactly before another expression, the comment wouldn't be considered closed, and all remaining parts of the template literal would be removed.

Input:

```js
styled.div`${x}/* ${y} */${z}`
```

Previous output post-minification:

```js
styled.div`${x}`
```

After this PR:

```js
styled.div`${x}${z}`
```

This bug was introduced way back in #12224 (my fault!), but had gone unnoticed.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

A-transformer Area - Transformer / Transpiler C-performance Category - Solution not expected to change functional behavior, only performance

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants