Skip to content

Commit 0b252d8

Browse files
committed
fix(transformer/styled-components): remove repeat whitespace in CSS minification
1 parent a5c6154 commit 0b252d8

File tree

5 files changed

+47
-4
lines changed

5 files changed

+47
-4
lines changed

crates/oxc_transformer/src/plugins/styled_components.rs

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1061,7 +1061,16 @@ fn minify_template_literal<'a>(lit: &mut TemplateLiteral<'a>, ast: AstBuilder<'a
10611061
}
10621062
// Skip and compress whitespace.
10631063
_ if cur_byte.is_ascii_whitespace() => {
1064-
i += 1;
1064+
// Consume any following whitespace
1065+
let mut next_byte;
1066+
loop {
1067+
i += 1;
1068+
next_byte = bytes.get(i);
1069+
if next_byte.is_none_or(|&b| !b.is_ascii_whitespace()) {
1070+
break;
1071+
}
1072+
}
1073+
10651074
// Decide whether to preserve this whitespace character.
10661075
// CSS allows removing spaces around certain delimiters without changing meaning:
10671076
// - `color: red` -> `color:red` (spaces around colons)
@@ -1085,7 +1094,7 @@ fn minify_template_literal<'a>(lit: &mut TemplateLiteral<'a>, ast: AstBuilder<'a
10851094
// are significant in CSS. ` :hover` (descendant pseudo-selector) is different
10861095
// from `:hover` (direct pseudo-selector). Example: `.parent :hover` selects any
10871096
// hovered descendant, while `.parent:hover` selects the parent when hovered.
1088-
&& bytes.get(i).is_none_or(|&next| !matches!(next, b'{' | b'}' | b',' | b';'))
1097+
&& next_byte.is_none_or(|&next| !matches!(next, b'{' | b'}' | b',' | b';'))
10891098
{
10901099
// Preserve this space character.
10911100
// Examples:

tasks/transform_conformance/snapshots/oxc.snap.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
commit: 41d96516
22

3-
Passed: 183/306
3+
Passed: 184/307
44

55
# All Passed:
66
* babel-plugin-transform-class-static-block
@@ -1612,7 +1612,7 @@ after transform: ["Function", "babelHelpers"]
16121612
rebuilt : ["babelHelpers", "dec"]
16131613

16141614

1615-
# plugin-styled-components (21/36)
1615+
# plugin-styled-components (22/37)
16161616
* styled-components/add-identifier-with-top-level-import-paths/input.js
16171617
x Output mismatch
16181618

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
import styled from 'styled-components';
2+
3+
const A = styled.div`
4+
.a { }
5+
.b { }
6+
.c
7+
{
8+
}
9+
`;
10+
11+
const B = styled.div`
12+
str: "x y";
13+
`;
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
{
2+
"presets": [
3+
[
4+
"@babel/preset-env",
5+
{
6+
"modules": false,
7+
"targets": "defaults"
8+
}
9+
]
10+
],
11+
"plugins": [
12+
["styled-components", {
13+
"ssr": false,
14+
"displayName": false,
15+
"transpileTemplateLiterals": false
16+
}]
17+
]
18+
}
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
import styled from 'styled-components';
2+
const A = styled.div`.a{}.b{}.c{}`;
3+
const B = styled.div`str:"x y";`;

0 commit comments

Comments
 (0)