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

Add support for shorten transform/translate3d #1390

Merged
merged 1 commit into from
Jun 26, 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
4 changes: 4 additions & 0 deletions internal/css_parser/css_decls.go
Original file line number Diff line number Diff line change
Expand Up @@ -172,6 +172,10 @@ 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
40 changes: 40 additions & 0 deletions internal/css_parser/css_decls_transform.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
package css_parser

import (
"strings"

"github.com/evanw/esbuild/internal/css_ast"
"github.com/evanw/esbuild/internal/css_lexer"
)

func (p *parser) mangleTransforms(tokens []css_ast.Token) []css_ast.Token {
for i, token := range tokens {
transformFuncName := strings.ToLower(token.Text)
switch transformFuncName {
case "translate3d":
token = p.mangleTranslate3d(token)
}
tokens[i] = token
}

return tokens
}

func (p *parser) mangleTranslate3d(token css_ast.Token) css_ast.Token {
transformArg := *token.Children
if len(transformArg) != 5 {
return token
}
// translate3d(0, 0, tz) => translateZ(tz)
var noWhitespace css_ast.WhitespaceFlags
argX, argY, argZ := transformArg[0], transformArg[2], transformArg[4]
if argX.Kind == css_lexer.TNumber && argX.Text == "0" && argY.Kind == css_lexer.TNumber && argY.Text == "0" {
token.Text = "translateZ"
argZ.Whitespace = noWhitespace
token.Children = &[]css_ast.Token{
argZ,
}
}

return token
}
9 changes: 9 additions & 0 deletions internal/css_parser/css_parser_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -1116,3 +1116,12 @@ func TestMangleTime(t *testing.T) {
expectPrintedMangle(t, "a { animation: b 1e3ms }", "a {\n animation: b 1e3ms;\n}\n")
expectPrintedMangle(t, "a { animation: b 1E3ms }", "a {\n animation: b 1E3ms;\n}\n")
}

func TestTransform(t *testing.T) {
expectPrintedMangle(t, "a { transform:translate3d(0, 0, 2px) }", "a {\n transform: translateZ(2px);\n}\n")
expectPrintedMangle(t, "a { transform:TRANSLATE3D(0, 0, 2) }", "a {\n transform: translateZ(2);\n}\n")
expectPrintedMangle(t, "a { transform:translate3d(1.5,1.5,3) }", "a {\n transform: translate3d(1.5, 1.5, 3);\n}\n")
expectPrintedMangle(t, "a { transform:translate3d(var(--foo)) }", "a {\n transform: translate3d(var(--foo));\n}\n")
expectPrintedMangle(t, "a { transform:perspective(500px) translate3d(0, 0, 2px) rotateY(3deg); }",
"a {\n transform: perspective(500px) translateZ(2px) rotateY(3deg);\n}\n")
}