Skip to content

Commit 7307483

Browse files
committed
fix(transformer/styled-components): remove more escaped line breaks in 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.
1 parent 1cf9b20 commit 7307483

File tree

5 files changed

+44
-5
lines changed

5 files changed

+44
-5
lines changed

crates/oxc_transformer/src/plugins/styled_components.rs

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -989,9 +989,7 @@ fn minify_template_literal<'a>(lit: &mut TemplateLiteral<'a>, ast: AstBuilder<'a
989989
continue;
990990
}
991991
b'n' | b'r' if string_quote == NOT_IN_STRING => {
992-
if output.last().is_some_and(|&last| last != b' ') {
993-
output.push(b' ');
994-
}
992+
insert_space_if_required(&mut output, quasi_index);
995993
i += 2;
996994
continue;
997995
}

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: 185/308
3+
Passed: 186/309
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 (23/38)
1615+
# plugin-styled-components (24/39)
16161616
* minify-comments/input.js
16171617
Unresolved references mismatch:
16181618
after transform: ["x", "y", "z"]
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
import styled from 'styled-components';
2+
3+
// Leading or trailing line breaks
4+
const A = styled.div`\r\ncolor: blue;\r\n`;
5+
6+
// Line breaks in removal position
7+
const B = styled.div`
8+
color:\r\nblue;
9+
.a\r\n{\r\n}
10+
`;
11+
12+
// Line breaks in non-removal position
13+
const C = styled.div`thing\r\n:hover;`;
14+
15+
// Line breaks before and after interpolations
16+
const D = styled.div`
17+
foo\r\n${'blue'}\r\n:blah;
18+
`;
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: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
import styled from 'styled-components';
2+
const A = styled.div`color:blue;`;
3+
const B = styled.div`color:blue;.a{}`;
4+
const C = styled.div`thing :hover;`;
5+
const D = styled.div`foo ${'blue'} :blah;`;

0 commit comments

Comments
 (0)