Skip to content

Commit f092518

Browse files
committed
fix minification issue with non-optional spaces in at-rule preludes
1 parent cde5e32 commit f092518

File tree

2 files changed

+9
-1
lines changed

2 files changed

+9
-1
lines changed

src/lib/index.ts

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -309,7 +309,9 @@ export function format_atrule_prelude(
309309
.replace(/\)([a-zA-Z])/g, ') $1') // force whitespace between closing parenthesis and following text (usually and|or)
310310
.replace(/\s*(=>|>=|<=)\s*/g, `${optional_space}$1${optional_space}`) // add optional spacing around =>, >= and <=
311311
.replace(/([^<>=\s])([<>])([^<>=\s])/g, `$1${optional_space}$2${optional_space}$3`) // add spacing around < or > except when it's part of <=, >=, =>
312-
.replace(/\s+/g, optional_space) // collapse multiple whitespaces into one
312+
.replace(/([^<>=\s])\s+([<>])\s+([^<>=\s])/g, `$1${optional_space}$2${optional_space}$3`) // handle spaces around < or > when they already have surrounding whitespace
313+
.replace(/\s+/g, SPACE) // collapse multiple whitespaces into one
314+
.replace(/([:,]) /g, minify ? '$1' : '$1 ') // in minify mode, remove optional spaces after : and ,
313315
.replace(
314316
/calc\(\s*([^()+\-*/]+)\s*([*/+-])\s*([^()+\-*/]+)\s*\)/g,
315317
(_, left, operator, right) => {

test/atrules.test.ts

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -367,6 +367,12 @@ test('minify: new-fangled comparators (width > 1000px)', () => {
367367
expect(actual).toEqual(expected)
368368
})
369369

370+
test('minify: keeps necessary whitespace between keywords', () => {
371+
let actual = minify(`@media screen or print {}`)
372+
let expected = `@media screen or print{}`
373+
expect(actual).toEqual(expected)
374+
})
375+
370376
test.skip('preserves comments', () => {
371377
let actual = format(`
372378
@media /* comment */ all {}

0 commit comments

Comments
 (0)