Skip to content

Commit

Permalink
css: implement minification for all matrix forms
Browse files Browse the repository at this point in the history
  • Loading branch information
evanw committed Jun 27, 2021
1 parent 6164456 commit 80c92d4
Show file tree
Hide file tree
Showing 5 changed files with 596 additions and 39 deletions.
18 changes: 18 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,24 @@

This change was contributed by [@Gusted](https://github.com/Gusted).

* Minify syntax in the CSS `transform` property ([#1390](https://github.com/evanw/esbuild/pull/1390))

This release includes various size reductions for CSS transform matrix syntax when minification is enabled:

```css
/* Original code */
div {
transform: translate3d(0, 0, 10px) scale3d(200%, 200%, 1) rotate3d(0, 0, 1, 45deg);
}

/* Output with "--minify-syntax" */
div {
transform: translateZ(10px) scale(2) rotate(45deg);
}
```

The `translate3d` to `translateZ` conversion was contributed by [@steambap](https://github.com/steambap).

## 0.12.9

* Allow `this` with `--define` ([#1361](https://github.com/evanw/esbuild/issues/1361))
Expand Down
41 changes: 35 additions & 6 deletions internal/css_ast/css_ast.go
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,13 @@ type Token struct {
Whitespace WhitespaceFlags // 1 byte
}

type WhitespaceFlags uint8

const (
WhitespaceBefore WhitespaceFlags = 1 << iota
WhitespaceAfter
)

func (a Token) Equal(b Token) bool {
if a.Kind == b.Kind && a.Text == b.Text && a.ImportRecordIndex == b.ImportRecordIndex && a.Whitespace == b.Whitespace {
if a.Children == nil && b.Children == nil {
Expand Down Expand Up @@ -136,6 +143,18 @@ func TokensEqualIgnoringWhitespace(a []Token, b []Token) bool {
return true
}

func TokensAreCommaSeparated(tokens []Token) bool {
if n := len(tokens); (n & 1) != 0 {
for i := 1; i < n; i += 2 {
if tokens[i].Kind != css_lexer.TComma {
return false
}
}
return true
}
return false
}

func (t Token) FractionForPercentage() (float64, bool) {
if t.Kind == css_lexer.TPercentage {
if f, err := strconv.ParseFloat(t.PercentageValue(), 64); err == nil {
Expand Down Expand Up @@ -163,12 +182,14 @@ func (t *Token) TurnLengthIntoNumberIfZero() bool {
return false
}

type WhitespaceFlags uint8

const (
WhitespaceBefore WhitespaceFlags = 1 << iota
WhitespaceAfter
)
func (t *Token) TurnLengthOrPercentageIntoNumberIfZero() bool {
if t.Kind == css_lexer.TPercentage && t.PercentageValue() == "0" {
t.Kind = css_lexer.TNumber
t.Text = "0"
return true
}
return t.TurnLengthIntoNumberIfZero()
}

func (t Token) PercentageValue() string {
return t.Text[:len(t.Text)-1]
Expand All @@ -182,6 +203,14 @@ func (t Token) DimensionUnit() string {
return t.Text[t.UnitOffset:]
}

func (t Token) IsZero() bool {
return t.Kind == css_lexer.TNumber && t.Text == "0"
}

func (t Token) IsOne() bool {
return t.Kind == css_lexer.TNumber && t.Text == "1"
}

type R interface {
Equal(rule R) bool
Hash() (uint32, bool)
Expand Down
9 changes: 5 additions & 4 deletions internal/css_parser/css_decls.go
Original file line number Diff line number Diff line change
Expand Up @@ -105,6 +105,11 @@ func (p *parser) processDeclarations(rules []css_ast.R) []css_ast.R {
}
}

case css_ast.DTransform:
if p.options.MangleSyntax {
decl.Value = p.mangleTransforms(decl.Value)
}

case css_ast.DBoxShadow:
if p.options.MangleSyntax {
decl.Value = p.mangleBoxShadows(decl.Value)
Expand Down Expand Up @@ -172,10 +177,6 @@ func (p *parser) processDeclarations(rules []css_ast.R) []css_ast.R {
if p.options.MangleSyntax {
borderRadius.mangleCorner(rules, decl, i, p.options.RemoveWhitespace, borderRadiusBottomLeft)
}
case css_ast.DTransform:
if p.options.MangleSyntax {
decl.Value = p.mangleTransforms(decl.Value)
}
}
}

Expand Down
Loading

0 comments on commit 80c92d4

Please sign in to comment.