Skip to content

Commit f131f99

Browse files
committed
fix single line comments + interpolation issues
this one was a doozy... I did a few things here: 1. Each injected placeholder corresponding to an expression now has a unique index that can be tracked. 2. After all the cleanup is completed, I do a diff on which indices were removed. 3. That gets passed up the chain to the main minify function, which then removes the appropriate expressions from the template literal without culling correct ones.
1 parent 9ee7fc2 commit f131f99

File tree

6 files changed

+110
-15
lines changed

6 files changed

+110
-15
lines changed

src/css/placeholderUtils.js

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,14 @@
11
// The capture group makes sure that the split contains the interpolation index
2-
const placeholderRegex = /__PLACEHOLDER_(\d+?)__/
2+
export const placeholderRegex = /(?:__PLACEHOLDER_(\d+)__)/g
33

44
// Alternative regex that splits without a capture group
5-
const placeholderNonCapturingRegex = /__PLACEHOLDER_(?:\d+?)__/
5+
const placeholderNonCapturingRegex = /__PLACEHOLDER_(?:\d+)__/g
66

77
// Generates a placeholder from an index
88
export const makePlaceholder = index => `__PLACEHOLDER_${index}__`
99

1010
// Splits CSS by placeholders
11-
export const splitByPlaceholders = (css, capture = true) =>
12-
css.split(capture ? placeholderRegex : placeholderNonCapturingRegex)
11+
export const splitByPlaceholders = ([css, ...rest], capture = true) => [
12+
css.split(capture ? placeholderRegex : placeholderNonCapturingRegex),
13+
...rest,
14+
]

src/minify/index.js

Lines changed: 17 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,18 @@
1-
import { makePlaceholder, splitByPlaceholders } from '../css/placeholderUtils'
1+
import difference from 'lodash/difference'
22

3-
let i = 0
3+
import {
4+
makePlaceholder,
5+
placeholderRegex,
6+
splitByPlaceholders,
7+
} from '../css/placeholderUtils'
48

5-
const injectUniquePlaceholders = strArr =>
6-
strArr.reduce((str, val, index, arr) => {
9+
const injectUniquePlaceholders = strArr => {
10+
let i = 0
11+
12+
return strArr.reduce((str, val, index, arr) => {
713
return str + val + (index < arr.length - 1 ? makePlaceholder(i++) : '')
814
}, '')
15+
}
916

1017
const makeMultilineCommentRegex = newlinePattern =>
1118
new RegExp('\\/\\*[^!](.|' + newlinePattern + ')*?\\*\\/', 'g')
@@ -82,7 +89,12 @@ const minify = linebreakPattern => {
8289
.map(stripLineComment) // Remove line comments inside text
8390
.join(' ') // Rejoin all lines
8491

85-
return compressSymbols(newCode)
92+
const eliminatedExpressionIndices = difference(
93+
code.match(placeholderRegex),
94+
newCode.match(placeholderRegex)
95+
).map(x => parseInt(x.match(/\d+/)[0], 10))
96+
97+
return [compressSymbols(newCode), eliminatedExpressionIndices]
8698
}
8799
}
88100

src/visitors/minify.js

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -10,12 +10,18 @@ export default t => (path, state) => {
1010
const templateLiteral = path.node.quasi
1111
const quasisLength = templateLiteral.quasis.length
1212

13-
const rawValuesMinified = minifyRawValues(
13+
const [rawValuesMinified] = minifyRawValues(
1414
templateLiteral.quasis.map(x => x.value.raw)
1515
)
16-
const cookedValuesMinfified = minifyCookedValues(
17-
templateLiteral.quasis.map(x => x.value.cooked)
18-
)
16+
17+
const [
18+
cookedValuesMinfified,
19+
eliminatedExpressionIndices,
20+
] = minifyCookedValues(templateLiteral.quasis.map(x => x.value.cooked))
21+
22+
eliminatedExpressionIndices.forEach((expressionIndex, iteration) => {
23+
templateLiteral.expressions.splice(expressionIndex - iteration, 1)
24+
})
1925

2026
for (let i = 0; i < quasisLength; i++) {
2127
const element = templateLiteral.quasis[i]

src/visitors/templateLiterals/transpile.js

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,9 @@ export default t => (path, state) => {
88
} = path.node
99

1010
const values = t.arrayExpression(
11-
quasis.map(quasi => t.stringLiteral(quasi.value.cooked))
11+
quasis
12+
.filter(quasi => quasi.value.cooked !== undefined)
13+
.map(quasi => t.stringLiteral(quasi.value.cooked))
1214
)
1315

1416
path.replaceWith(t.callExpression(callee, [values, ...expressions]))

test/__snapshots__/index.test.js.snap

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -158,6 +158,45 @@ const Parens = styled(\'div\')\`&:hover{color:blue;}color:red;\`;
158158
const UrlComments = styled(\'div\')\`color:red;background:red;border:1px solid green;\`;"
159159
`;
160160
161+
exports[`fixtures should minify single line comments with interpolations 1`] = `
162+
"import styled from \'styled-components/no-tags\';
163+
164+
const Test1 = styled(\'div\').withConfig({
165+
displayName: \'minify-single-line-comments-with-interpolations__Test1\',
166+
componentId: \'sc-1p41dy5-0\'
167+
})([\'width:100%;\']);
168+
169+
const Test2 = styled(\'div\').withConfig({
170+
displayName: \'minify-single-line-comments-with-interpolations__Test2\',
171+
componentId: \'sc-1p41dy5-1\'
172+
})([\'width:100%;\']);
173+
174+
const Test3 = styled(\'div\').withConfig({
175+
displayName: \'minify-single-line-comments-with-interpolations__Test3\',
176+
componentId: \'sc-1p41dy5-2\'
177+
})([\'width:100%;\', \';\'], \'red\');
178+
179+
const Test4 = styled(\'div\').withConfig({
180+
displayName: \'minify-single-line-comments-with-interpolations__Test4\',
181+
componentId: \'sc-1p41dy5-3\'
182+
})([\'width:100%;\']);
183+
184+
const Test5 = styled(\'div\').withConfig({
185+
displayName: \'minify-single-line-comments-with-interpolations__Test5\',
186+
componentId: \'sc-1p41dy5-4\'
187+
})([\'width:100%;\']);
188+
189+
const Test6 = styled(\'div\').withConfig({
190+
displayName: \'minify-single-line-comments-with-interpolations__Test6\',
191+
componentId: \'sc-1p41dy5-5\'
192+
})([\'background:url(\"https://google.com\");width:100%;\', \' \'], \'green\');
193+
194+
const Test7 = styled(\'div\').withConfig({
195+
displayName: \'minify-single-line-comments-with-interpolations__Test7\',
196+
componentId: \'sc-1p41dy5-6\'
197+
})([\'background:url(\"https://google.com\");width:\', \';\', \' height:\', \';\'], p => p.props.width, \'green\', p => p.props.height);"
198+
`;
199+
161200
exports[`fixtures should not use private api if not required 1`] = `
162201
"import styled from \'styled-components/no-tags\';
163202
Lines changed: 35 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,40 @@
11
import styled from 'styled-components'
22

3-
const Simple = styled.div`
3+
const Test1 = styled.div`
44
width: 100%;
55
// color: ${'red'};
66
`
7+
8+
const Test2 = styled.div`
9+
width: 100%;
10+
// color: pale${'red'};
11+
`
12+
13+
const Test3 = styled.div`
14+
width: 100%;
15+
// color
16+
${'red'};
17+
`
18+
19+
const Test4 = styled.div`
20+
width: 100%;
21+
// color: ${'red'}-blue;
22+
`
23+
24+
const Test5 = styled.div`
25+
width: 100%;
26+
// color: ${'red'}${'blue'};
27+
`
28+
29+
const Test6 = styled.div`
30+
background: url("https://google.com");
31+
width: 100%;
32+
${'green'} // color: ${'red'}${'blue'};
33+
`
34+
35+
const Test7 = styled.div`
36+
background: url("https://google.com");
37+
width: ${p => p.props.width};
38+
${'green'} // color: ${'red'}${'blue'};
39+
height: ${p => p.props.height};
40+
`

0 commit comments

Comments
 (0)