Skip to content

Commit e08f24e

Browse files
authored
Merge pull request #70 from styled-components/fix/newline-mangling
Fix newlines being mangled inside values
2 parents 47b702e + 417396b commit e08f24e

File tree

16 files changed

+76
-37
lines changed

16 files changed

+76
-37
lines changed

src/minify/index.js

Lines changed: 24 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ import { makePlaceholder, splitByPlaceholders } from '../css/placeholderUtils'
22

33
const makeMultilineCommentRegex = newlinePattern => new RegExp('\\/\\*(.|' + newlinePattern + ')*?\\*\\/', 'g')
44
const lineCommentStart = /\/\//g
5+
const symbolRegex = /(\s*[;:{},]\s*)/g
56

67
// Counts occurences of substr inside str
78
const countOccurences = (str, substr) => str.split(substr).length - 1
@@ -37,6 +38,25 @@ export const stripLineComment = line => (
3738
))
3839
)
3940

41+
export const compressSymbols = code => code
42+
.split(symbolRegex)
43+
.reduce((str, fragment, index) => {
44+
// Even-indices are non-symbol fragments
45+
if (index % 2 === 0) {
46+
return str + fragment
47+
}
48+
49+
// Only manipulate symbols outside of strings
50+
if (
51+
countOccurences(str, '\'') % 2 === 0 &&
52+
countOccurences(str, '\"') % 2 === 0
53+
) {
54+
return str + fragment.trim()
55+
}
56+
57+
return str + fragment
58+
}, '')
59+
4060
// Detects lines that are exclusively line comments
4161
const isLineComment = line => line.trim().startsWith('//')
4262

@@ -46,14 +66,14 @@ const minify = linebreakPattern => {
4666
const multilineCommentRegex = makeMultilineCommentRegex(linebreakPattern)
4767

4868
return code => {
49-
const lines = code
69+
const newCode = code
5070
.replace(multilineCommentRegex, '\n') // Remove multiline comments
5171
.split(linebreakRegex) // Split at newlines
72+
.filter(line => line.length > 0 && !isLineComment(line)) // Removes lines containing only line comments
5273
.map(stripLineComment) // Remove line comments inside text
74+
.join(' ') // Rejoin all lines
5375

54-
return lines
55-
.filter(line => !isLineComment(line)) // Removes lines containing only line comments
56-
.join('')
76+
return compressSymbols(newCode)
5777
}
5878
}
5979

test/fixtures/01-add-display-names/after.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
const Test = styled.div.withConfig({
22
displayName: 'Test'
3-
})`width: 100%;`;
3+
})`width:100%;`;
44
const Test2 = styled('div').withConfig({
55
displayName: 'Test2'
66
})``;

test/fixtures/02-add-identifier/after.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
const Test = styled.div.withConfig({
22
componentId: "f15zqz-0"
3-
})`width: 100%;`;
3+
})`width:100%;`;
44
const Test2 = true ? styled.div.withConfig({
55
componentId: "f15zqz-1"
66
})`` : styled.div.withConfig({

test/fixtures/03-add-identifier-and-display-name/after.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
const Test = styled.div.withConfig({
22
displayName: "Test",
33
componentId: "s1j54k93-0"
4-
})`width: 100%;`;
4+
})`width:100%;`;
55
const Test2 = true ? styled.div.withConfig({
66
displayName: "Test2",
77
componentId: "s1j54k93-1"

test/fixtures/04-track-the-imported-variable/after.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ import s from "styled-components";
33
const Test = s.div.withConfig({
44
displayName: "Test",
55
componentId: "syqrfc-0"
6-
})`width: 100%;`;
6+
})`width:100%;`;
77
const Test2 = true ? s.div.withConfig({
88
displayName: "Test2",
99
componentId: "syqrfc-1"

test/fixtures/05-use-file-name/after.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ import styled from "styled-components";
33
const Test = styled.div.withConfig({
44
displayName: "before__Test",
55
componentId: "gl3p53-0"
6-
})`color: red;`;
6+
})`color:red;`;
77
styled.div.withConfig({
88
displayName: "before",
99
componentId: "gl3p53-1"
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
const Test = s.div.withConfig({
22
displayName: 'before__Test',
33
componentId: 's2wuo7o-0'
4-
})`width: 100%;`;
4+
})`width:100%;`;
55
import { default as s, css } from 'styled-components';
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,3 @@
11
import styled from 'styled-components';
22

3-
const Test = styled.div`width: 100%;`;
3+
const Test = styled.div`width:100%;`;

test/fixtures/08-minify-css-to-use-with-transpilation/after.js

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,10 @@
11
'use strict';
22

3-
var _templateObject = _taggedTemplateLiteral(['width: 100%;'], ['width: 100%;']),
4-
_templateObject2 = _taggedTemplateLiteral(['content: " ', ' ";'], ['content: " ', ' ";']),
5-
_templateObject3 = _taggedTemplateLiteral(['content: " ', ' ";color: red;'], ['content: " ', ' ";color: red;']),
6-
_templateObject4 = _taggedTemplateLiteral(['color: red;'], ['color: red;']),
7-
_templateObject5 = _taggedTemplateLiteral(['&:hover {color: blue;}'], ['&:hover {color: blue;}']);
3+
var _templateObject = _taggedTemplateLiteral(['width:100%;'], ['width:100%;']),
4+
_templateObject2 = _taggedTemplateLiteral(['content:" ', ' ";'], ['content:" ', ' ";']),
5+
_templateObject3 = _taggedTemplateLiteral(['content:" ', ' ";color:red;'], ['content:" ', ' ";color:red;']),
6+
_templateObject4 = _taggedTemplateLiteral(['color:red;'], ['color:red;']),
7+
_templateObject5 = _taggedTemplateLiteral(['&:hover{color:blue;}'], ['&:hover{color:blue;}']);
88

99
var _styledComponents = require('styled-components');
1010

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,13 @@
11
import styled from 'styled-components';
22

3-
const Simple = styled.div`width: 100%;`;
3+
const Simple = styled.div`width:100%;`;
44

5-
const Interpolation = styled.div`content: "https://test.com/${props => props.endpoint}";`;
5+
const Interpolation = styled.div`content:"https://test.com/${props => props.endpoint}";`;
66

7-
const SpecialCharacters = styled.div`content: " ${props => props.text} ";color: red;`;
7+
const SpecialCharacters = styled.div`content:" ${props => props.text} ";color:red;`;
88

9-
const Comment = styled.div`width: 100%;color: red;`;
9+
const Comment = styled.div`width:100%;color:red;`;
1010

11-
const Parens = styled.div`&:hover {color: blue;}color: red;`;
11+
const Parens = styled.div`&:hover{color:blue;}color:red;`;
1212

13-
const UrlComments = styled.div`color: red;background: red;border: 1px solid green;`;
13+
const UrlComments = styled.div`color:red;background:red;border:1px solid green;`;

0 commit comments

Comments
 (0)