Skip to content

Conversation

@overlookmotel
Copy link
Member

@overlookmotel overlookmotel commented Aug 29, 2025

Previously we included whitespace around block comments when the following character means it could have been removed. e.g.:

Input:

styled.div`.a /* blah */ { color: blue }`

Previous output post-minification:

styled.div`.a {color:blue}`

After this PR:

styled.div`.a{color:blue}`

This is achieved by a significant change to how spaces are inserted.

Previous logic: When encountering whitespace, determine whether to print a space or not based on what came before it, and what's after it. Problem is that this doesn't work correctly when the whitespace is followed by a block comment, because next char is / (start of the comment) - whereas what you really want to know is what's after the comment.

New logic: When encountering whitespace, print a space if the preceding character isn't :, ;, etc. Don't take into account what the next char is. But later on when encounter e.g. {, check if previous character is a space, and if so, remove it. So in the case of .a /* blah */ {}, a space gets printed after .a, but then it gets deleted again when { is found after the comment.

This also has the benefit of making the code simpler.

@github-actions github-actions bot added A-transformer Area - Transformer / Transpiler C-bug Category - Bug labels Aug 29, 2025
Copy link
Member Author

overlookmotel commented Aug 29, 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.

@codspeed-hq
Copy link

codspeed-hq bot commented Aug 29, 2025

CodSpeed Instrumentation Performance Report

Merging #13379 will improve performances by 3.36%

Comparing 08-29-fix_transformer_styled-components_remove_unnecessary_whitespace_around_block_comments_in_css_minification (6dc5b70) with main (5a25c06)1

Summary

⚡ 1 improvements
✅ 37 untouched benchmarks

Benchmarks breakdown

Benchmark BASE HEAD Change
mangler[RadixUIAdoptionSection.jsx] 14.7 µs 14.2 µs +3.36%

Footnotes

  1. No successful run was found on main (6dc5b70) during the generation of this report, so 5a25c06 was used instead as the comparison base. There might be some changes unrelated to this pull request in this report.

@overlookmotel overlookmotel marked this pull request as ready for review August 29, 2025 03:42
Copilot AI review requested due to automatic review settings August 29, 2025 03:42
@overlookmotel overlookmotel requested a review from Dunqing as a code owner August 29, 2025 03:42
Copy link
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull Request Overview

This PR fixes CSS minification in the styled-components transformer by improving whitespace handling around block comments. The change ensures unnecessary whitespace is properly removed when block comments appear before CSS delimiters like {, }, ,, and ;.

Key changes:

  • Refactored whitespace insertion logic to be more predictable and comment-aware
  • Simplified the decision-making process for when to insert/remove spaces
  • Added comprehensive test cases covering various comment scenarios

Reviewed Changes

Copilot reviewed 3 out of 3 changed files in this pull request and generated 2 comments.

File Description
crates/oxc_transformer/src/plugins/styled_components.rs Refactored CSS minification logic with new whitespace handling approach and helper function
tasks/transform_conformance/tests/plugin-styled-components/test/fixtures/minify-comments/input.js Added test cases with block comments in various CSS contexts
tasks/transform_conformance/tests/plugin-styled-components/test/fixtures/minify-comments/output.js Expected minified output showing proper whitespace removal around comments

Tip: Customize your code reviews with copilot-instructions.md. Create the file or learn how to get started.

@overlookmotel overlookmotel force-pushed the 08-29-fix_transformer_styled-components_remove_unnecessary_whitespace_around_block_comments_in_css_minification branch from fe2e345 to dd95896 Compare August 29, 2025 04:15
@graphite-app graphite-app bot added the 0-merge Merge with Graphite Merge Queue label Aug 29, 2025
@graphite-app
Copy link
Contributor

graphite-app bot commented Aug 29, 2025

Merge activity

graphite-app bot pushed a commit that referenced this pull request Aug 29, 2025
…und block comments in CSS minification (#13379)

Previously we included whitespace around block comments when the following character means it could have been removed. e.g.:

Input:

```js
styled.div`.a /* blah */ { color: blue }`
```

Previous output post-minification:

```js
styled.div`.a {color:blue}`
```

After this PR:

```js
styled.div`.a{color:blue}`
```

This is achieved by a significant change to how spaces are inserted.

Previous logic: When encountering whitespace, determine whether to print a space or not based on what came before it, and what's after it. Problem is that this doesn't work correctly when the whitespace is followed by a block comment, because next char is `/` (start of the comment) - whereas what you really want to know is what's *after* the comment.

New logic: When encountering whitespace, print a space if the preceding character isn't `:`, `;`, etc. Don't take into account what the next char is. But later on when encounter e.g. `{`, check if previous character is a space, and if so, remove it. So in the case of `.a /* blah */ {}`, a space gets printed after `.a`, but then it gets deleted again when `{` is found after the comment.

This also has the benefit of making the code simpler.
@graphite-app graphite-app bot force-pushed the 08-29-fix_transformer_styled-components_remove_unnecessary_whitespace_around_block_comments_in_css_minification branch from dd95896 to 1cf9b20 Compare August 29, 2025 05:25
graphite-app bot pushed a commit that referenced this pull request Aug 29, 2025
…n CSS minification (#13380)

The change in approach to whitespace in #13379 means we can remove more escaped line breaks (`\r` and `\n`).

Previously when processing `\r\n`, the next character after `\r` was seen as `\`, and therefore a space was unnecessarily inserted.

Now we only look backwards, not forwards, so this problem goes away. We just treat `\r` and `\n` like any other whitespace.
…und block comments in CSS minification (#13379)

Previously we included whitespace around block comments when the following character means it could have been removed. e.g.:

Input:

```js
styled.div`.a /* blah */ { color: blue }`
```

Previous output post-minification:

```js
styled.div`.a {color:blue}`
```

After this PR:

```js
styled.div`.a{color:blue}`
```

This is achieved by a significant change to how spaces are inserted.

Previous logic: When encountering whitespace, determine whether to print a space or not based on what came before it, and what's after it. Problem is that this doesn't work correctly when the whitespace is followed by a block comment, because next char is `/` (start of the comment) - whereas what you really want to know is what's *after* the comment.

New logic: When encountering whitespace, print a space if the preceding character isn't `:`, `;`, etc. Don't take into account what the next char is. But later on when encounter e.g. `{`, check if previous character is a space, and if so, remove it. So in the case of `.a /* blah */ {}`, a space gets printed after `.a`, but then it gets deleted again when `{` is found after the comment.

This also has the benefit of making the code simpler.
@graphite-app graphite-app bot force-pushed the 08-29-fix_transformer_styled-components_remove_unnecessary_whitespace_around_block_comments_in_css_minification branch from 1cf9b20 to 6dc5b70 Compare August 29, 2025 05:26
graphite-app bot pushed a commit that referenced this pull request Aug 29, 2025
…n CSS minification (#13380)

The change in approach to whitespace in #13379 means we can remove more escaped line breaks (`\r` and `\n`).

Previously when processing `\r\n`, the next character after `\r` was seen as `\`, and therefore a space was unnecessarily inserted.

Now we only look backwards, not forwards, so this problem goes away. We just treat `\r` and `\n` like any other whitespace.
@graphite-app graphite-app bot merged commit 6dc5b70 into main Aug 29, 2025
24 checks passed
@graphite-app graphite-app bot deleted the 08-29-fix_transformer_styled-components_remove_unnecessary_whitespace_around_block_comments_in_css_minification branch August 29, 2025 05:31
@graphite-app graphite-app bot removed the 0-merge Merge with Graphite Merge Queue label Aug 29, 2025
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-bug Category - Bug

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants