Skip to content

Commit

Permalink
fixes after landing css change
Browse files Browse the repository at this point in the history
  • Loading branch information
evanw committed Feb 21, 2021
1 parent ba901e0 commit d496dcf
Show file tree
Hide file tree
Showing 3 changed files with 31 additions and 18 deletions.
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,10 @@
The parser previously allowed this for regular functions but not for async or generator functions. Now with this release, this behavior is also allowed for these special kinds of functions too.
* Remove empty CSS rules when minifying ([#851](https://github.com/evanw/esbuild/pull/851))
Empty rules with no content such as `div {}` are now removed when CSS is minified. This change was contributed by [@susiwen8](https://github.com/susiwen8).
## 0.8.49
* Work around a problem with `pnpm` and `NODE_PATH` ([#816](https://github.com/evanw/esbuild/issues/816))
Expand Down
13 changes: 9 additions & 4 deletions internal/css_printer/css_printer.go
Original file line number Diff line number Diff line change
Expand Up @@ -90,6 +90,7 @@ func (p *printer) printRule(rule css_ast.R, indent int, omitTrailingSemicolon bo
}
indent++
for _, block := range r.Blocks {
// "@keyframes { from {} to { color: red } }" => "@keyframes { to { color: red } }"
if p.MangleSyntax && len(block.Rules) == 0 {
continue
}
Expand Down Expand Up @@ -122,9 +123,16 @@ func (p *printer) printRule(rule css_ast.R, indent int, omitTrailingSemicolon bo
p.print("}")

case *css_ast.RKnownAt:
// "@font-face {}" => ""
// "@page {}" => ""
// "@document {}" => ""
// "@media {}" => ""
// "@scope {}" => ""
// "@supports {}" => ""
if p.MangleSyntax && len(r.Rules) == 0 {
return
}

p.print("@")
whitespace := mayNeedWhitespaceAfter
if len(r.Prelude) == 0 {
Expand Down Expand Up @@ -161,6 +169,7 @@ func (p *printer) printRule(rule css_ast.R, indent int, omitTrailingSemicolon bo
}

case *css_ast.RSelector:
// "a {}" => ""
if p.MangleSyntax && len(r.Rules) == 0 {
return
}
Expand All @@ -172,10 +181,6 @@ func (p *printer) printRule(rule css_ast.R, indent int, omitTrailingSemicolon bo
p.printRuleBlock(r.Rules, indent)

case *css_ast.RQualified:
if p.MangleSyntax && len(r.Rules) == 0 {
return
}

hasWhitespaceAfter := p.printTokens(r.Prelude)
if !hasWhitespaceAfter && !p.RemoveWhitespace {
p.print(" ")
Expand Down
32 changes: 18 additions & 14 deletions internal/css_printer/css_printer_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -48,9 +48,9 @@ func expectPrintedMinify(t *testing.T, contents string, expected string) {
})
}

func expectPrinteMangleSyntax(t *testing.T, contents string, expected string) {
func expectPrintedMangle(t *testing.T, contents string, expected string) {
t.Helper()
expectPrintedCommon(t, contents+" [minified]", contents, expected, Options{
expectPrintedCommon(t, contents+" [mangled]", contents, expected, Options{
MangleSyntax: true,
})
}
Expand Down Expand Up @@ -409,16 +409,20 @@ func TestASCII(t *testing.T) {

func TestEmptyRule(t *testing.T) {
expectPrinted(t, "div {}", "div {\n}\n")
expectPrinted(t, ".a {}", ".a {\n}\n")
expectPrinted(t, ".a {}.b { color: red;}", ".a {\n}\n.b {\n color: red;\n}\n")
expectPrinted(t, "@media (prefers-reduced-motion: no-preference) {\n}", "@media (prefers-reduced-motion: no-preference) {\n}\n")
expectPrinted(t, "@keyframes test {\n from {}\n to {\n color: red;\n}\n}", "@keyframes test {\n from {\n }\n to {\n color: red;\n }\n}\n")

expectPrintedMinify(t, "p:hover {}", "p:hover{}")
expectPrintedMinify(t, ".a {}\n.b {\n color: red;\n}", ".a{}.b{color:red}")
expectPrintedMinify(t, "@keyframes mini {\n from {}\n to {\n color: red;\n}\n}", "@keyframes mini{from{}to{color:red}}")

expectPrinteMangleSyntax(t, "a:hover {}", "")
expectPrinteMangleSyntax(t, ".a {}\n.b {\n color: red;\n}", ".b {\n color: red;\n}\n")
expectPrinteMangleSyntax(t, "@keyframes test {\n from {}\n to {\n color: red;\n}\n}", "@keyframes test {\n to {\n color: red;\n }\n}\n")
expectPrinted(t, "$invalid {}", "$invalid {\n}\n")
expectPrinted(t, "@media screen {}", "@media screen {\n}\n")
expectPrinted(t, "@keyframes test { from {} to { color: red } }", "@keyframes test {\n from {\n }\n to {\n color: red;\n }\n}\n")
expectPrinted(t, "@keyframes test { from { color: red } to {} }", "@keyframes test {\n from {\n color: red;\n }\n to {\n }\n}\n")

expectPrintedMinify(t, "div {}", "div{}")
expectPrintedMinify(t, "$invalid {}", "$invalid{}")
expectPrintedMinify(t, "@media screen {}", "@media screen{}")
expectPrintedMinify(t, "@keyframes test { from {} to { color: red } }", "@keyframes test{from{}to{color:red}}")
expectPrintedMinify(t, "@keyframes test { from { color: red } to {} }", "@keyframes test{from{color:red}to{}}")

expectPrintedMangle(t, "div {}", "")
expectPrintedMangle(t, "$invalid {}", "$invalid {\n}\n")
expectPrintedMangle(t, "@media screen {}", "")
expectPrintedMangle(t, "@keyframes test { from {} to { color: red } }", "@keyframes test {\n to {\n color: red;\n }\n}\n")
expectPrintedMangle(t, "@keyframes test { from { color: red } to {} }", "@keyframes test {\n from {\n color: red;\n }\n}\n")
}

0 comments on commit d496dcf

Please sign in to comment.