Skip to content

Commit 6750042

Browse files
authored
fix minifier bug #366 (#376)
1 parent 4416874 commit 6750042

File tree

2 files changed

+19
-4
lines changed

2 files changed

+19
-4
lines changed

src/minify/index.js

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -64,13 +64,18 @@ export const compressSymbols = code =>
6464

6565
// Only manipulate symbols outside of strings
6666
if (
67-
countOccurences(str, "'") % 2 === 0 &&
68-
countOccurences(str, '"') % 2 === 0
67+
countOccurences(str, "'") % 2 !== 0 ||
68+
countOccurences(str, '"') % 2 !== 0
6969
) {
70-
return str + fragment.trim()
70+
return str + fragment
71+
}
72+
73+
// Preserve whitespace preceding colon, to avoid joining selectors.
74+
if (/^\s+:/.test(fragment)) {
75+
return str + ' ' + fragment.trim()
7176
}
7277

73-
return str + fragment
78+
return str + fragment.trim()
7479
}, '')
7580

7681
// Detects lines that are exclusively line comments

test/minify/index.test.js

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -113,6 +113,9 @@ describe('minify utils', () => {
113113

114114
describe('compressSymbols', () => {
115115
it('removes spaces around symbols', () => {
116+
// The whitespace preceding the colon is removed here as part of the
117+
// trailing whitespace on the semi-colon. Contrast to the "preserves"
118+
// test below.
116119
const input = '; : { } , ; '
117120
const expected = ';:{},;'
118121

@@ -125,5 +128,12 @@ describe('minify utils', () => {
125128

126129
expect(compressSymbols(input)).toBe(expected)
127130
})
131+
132+
it('preserves whitespace preceding colons', () => {
133+
const input = '& :last-child { color: blue; }'
134+
const expected = '& :last-child{color:blue;}'
135+
136+
expect(compressSymbols(input)).toBe(expected)
137+
})
128138
})
129139
})

0 commit comments

Comments
 (0)