Skip to content

Commit

Permalink
fix minifier bug styled-components#366
Browse files Browse the repository at this point in the history
  • Loading branch information
agriffis committed Apr 5, 2022
1 parent 4416874 commit d25dec3
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 4 deletions.
13 changes: 9 additions & 4 deletions src/minify/index.js
Expand Up @@ -64,13 +64,18 @@ export const compressSymbols = code =>

// Only manipulate symbols outside of strings
if (
countOccurences(str, "'") % 2 === 0 &&
countOccurences(str, '"') % 2 === 0
countOccurences(str, "'") % 2 !== 0 ||
countOccurences(str, '"') % 2 !== 0
) {
return str + fragment.trim()
return str + fragment
}

// Preserve whitespace preceding colon, to avoid joining selectors.
if (/^\s+:/.test(fragment)) {
return str + ' ' + fragment.trim()
}

return str + fragment
return str + fragment.trim()
}, '')

// Detects lines that are exclusively line comments
Expand Down
10 changes: 10 additions & 0 deletions test/minify/index.test.js
Expand Up @@ -113,6 +113,9 @@ describe('minify utils', () => {

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

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

expect(compressSymbols(input)).toBe(expected)
})

it('preserves whitespace preceding colons', () => {
const input = '& :last-child { color: blue; }'
const expected = '& :last-child{color:blue;}'

expect(compressSymbols(input)).toBe(expected)
})
})
})

0 comments on commit d25dec3

Please sign in to comment.