Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix: css empty rulesets should be removed #851

Merged
merged 1 commit into from
Feb 21, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions internal/bundler/linker.go
Original file line number Diff line number Diff line change
Expand Up @@ -4088,6 +4088,7 @@ func (repr *chunkReprCSS) generate(c *linkerContext, chunk *chunkInfo) func(gene
ast.Rules = rules

compileResult.printedCSS = css_printer.Print(ast, css_printer.Options{
MangleSyntax: c.options.MangleSyntax,
RemoveWhitespace: c.options.RemoveWhitespace,
ASCIIOnly: c.options.ASCIIOnly,
})
Expand Down
16 changes: 16 additions & 0 deletions internal/css_printer/css_printer.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ type printer struct {
}

type Options struct {
MangleSyntax bool
RemoveWhitespace bool
ASCIIOnly bool
}
Expand Down Expand Up @@ -89,6 +90,10 @@ func (p *printer) printRule(rule css_ast.R, indent int, omitTrailingSemicolon bo
}
indent++
for _, block := range r.Blocks {
if p.MangleSyntax && len(block.Rules) == 0 {
continue
}

if !p.RemoveWhitespace {
p.printIndent(indent)
}
Expand Down Expand Up @@ -117,6 +122,9 @@ func (p *printer) printRule(rule css_ast.R, indent int, omitTrailingSemicolon bo
p.print("}")

case *css_ast.RKnownAt:
if p.MangleSyntax && len(r.Rules) == 0 {
return
}
p.print("@")
whitespace := mayNeedWhitespaceAfter
if len(r.Prelude) == 0 {
Expand Down Expand Up @@ -153,13 +161,21 @@ func (p *printer) printRule(rule css_ast.R, indent int, omitTrailingSemicolon bo
}

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

p.printComplexSelectors(r.Selectors, indent)
if !p.RemoveWhitespace {
p.print(" ")
}
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
23 changes: 23 additions & 0 deletions internal/css_printer/css_printer_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,13 @@ func expectPrintedMinify(t *testing.T, contents string, expected string) {
})
}

func expectPrinteMangleSyntax(t *testing.T, contents string, expected string) {
t.Helper()
expectPrintedCommon(t, contents+" [minified]", contents, expected, Options{
MangleSyntax: true,
})
}

func expectPrintedASCII(t *testing.T, contents string, expected string) {
t.Helper()
expectPrintedCommon(t, contents+" [ascii]", contents, expected, Options{
Expand Down Expand Up @@ -399,3 +406,19 @@ func TestASCII(t *testing.T) {
// This character should always be escaped
expectPrinted(t, ".\\FEFF:after { content: '\uFEFF' }", ".\\feff:after {\n content: \"\\feff\";\n}\n")
}

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")
}