Skip to content

Commit

Permalink
css: emit mappings for tokens
Browse files Browse the repository at this point in the history
  • Loading branch information
evanw committed Jul 17, 2023
1 parent 9410725 commit e3f6eb8
Show file tree
Hide file tree
Showing 10 changed files with 50 additions and 39 deletions.
4 changes: 2 additions & 2 deletions internal/bundler_tests/snapshots/snapshots_loader.txt
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ TestEmptyLoaderCSS
"version": 3,
"sources": ["entry.css"],
"sourcesContent": ["\n\t\t\t\t@import 'a.empty';\n\t\t\t\ta { background: url(b.empty) }\n\t\t\t"],
"mappings": ";AAEI;AAAA,EAAI;AAAyB;",
"mappings": ";AAEI;AAAA,EAAI,YAAY;AAAa;",
"names": []
}

Expand Down Expand Up @@ -58,7 +58,7 @@ a {
"imports": [],
"exports": [],
"inputs": {},
"bytes": 204
"bytes": 208
},
"entry.css": {
"imports": [
Expand Down
3 changes: 3 additions & 0 deletions internal/css_ast/css_ast.go
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,9 @@ type Token struct {
// contains the decoded string contents for "TString" tokens.
Text string // 16 bytes

// The source location at the start of the token
Loc logger.Loc // 4 bytes

// URL tokens have an associated import record at the top-level of the AST.
// This index points to that import record.
ImportRecordIndex uint32 // 4 bytes
Expand Down
3 changes: 2 additions & 1 deletion internal/css_parser/css_decls.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,9 @@ import (
"github.com/evanw/esbuild/internal/logger"
)

func (p *parser) commaToken() css_ast.Token {
func (p *parser) commaToken(loc logger.Loc) css_ast.Token {
t := css_ast.Token{
Loc: loc,
Kind: css_lexer.TComma,
Text: ",",
}
Expand Down
16 changes: 10 additions & 6 deletions internal/css_parser/css_decls_border_radius.go
Original file line number Diff line number Diff line change
Expand Up @@ -189,6 +189,7 @@ func (borderRadius *borderRadiusTracker) compactRules(rules []css_ast.Rule, keyR
whitespace = css_ast.WhitespaceBefore | css_ast.WhitespaceAfter
}
tokens = append(tokens, css_ast.Token{
Loc: tokens[len(tokens)-1].Loc,
Kind: css_lexer.TDelimSlash,
Text: "/",
Whitespace: whitespace,
Expand All @@ -197,17 +198,20 @@ func (borderRadius *borderRadiusTracker) compactRules(rules []css_ast.Rule, keyR
}

// Remove all of the existing declarations
rules[borderRadius.corners[0].ruleIndex] = css_ast.Rule{}
rules[borderRadius.corners[1].ruleIndex] = css_ast.Rule{}
rules[borderRadius.corners[2].ruleIndex] = css_ast.Rule{}
rules[borderRadius.corners[3].ruleIndex] = css_ast.Rule{}
var minLoc logger.Loc
for i, corner := range borderRadius.corners {
if loc := rules[corner.ruleIndex].Loc; i == 0 || loc.Start < minLoc.Start {
minLoc = loc
}
rules[corner.ruleIndex] = css_ast.Rule{}
}

// Insert the combined declaration where the last rule was
rules[borderRadius.corners[3].ruleIndex].Data = &css_ast.RDeclaration{
rules[borderRadius.corners[3].ruleIndex] = css_ast.Rule{Loc: minLoc, Data: &css_ast.RDeclaration{
Key: css_ast.DBorderRadius,
KeyText: "border-radius",
Value: tokens,
KeyRange: keyRange,
Important: borderRadius.important,
}
}}
}
15 changes: 9 additions & 6 deletions internal/css_parser/css_decls_box.go
Original file line number Diff line number Diff line change
Expand Up @@ -180,17 +180,20 @@ func (box *boxTracker) compactRules(rules []css_ast.Rule, keyRange logger.Range,
)

// Remove all of the existing declarations
rules[box.sides[0].ruleIndex] = css_ast.Rule{}
rules[box.sides[1].ruleIndex] = css_ast.Rule{}
rules[box.sides[2].ruleIndex] = css_ast.Rule{}
rules[box.sides[3].ruleIndex] = css_ast.Rule{}
var minLoc logger.Loc
for i, side := range box.sides {
if loc := rules[side.ruleIndex].Loc; i == 0 || loc.Start < minLoc.Start {
minLoc = loc
}
rules[side.ruleIndex] = css_ast.Rule{}
}

// Insert the combined declaration where the last rule was
rules[box.sides[3].ruleIndex].Data = &css_ast.RDeclaration{
rules[box.sides[3].ruleIndex] = css_ast.Rule{Loc: minLoc, Data: &css_ast.RDeclaration{
Key: box.key,
KeyText: box.keyText,
Value: tokens,
KeyRange: keyRange,
Important: box.important,
}
}}
}
34 changes: 17 additions & 17 deletions internal/css_parser/css_decls_color.go
Original file line number Diff line number Diff line change
Expand Up @@ -289,12 +289,12 @@ func (p *parser) lowerColor(token css_ast.Token) css_ast.Token {
hex = expandHex(hex)
token.Kind = css_lexer.TFunction
token.Text = "rgba"
commaToken := p.commaToken()
commaToken := p.commaToken(token.Loc)
token.Children = &[]css_ast.Token{
{Kind: css_lexer.TNumber, Text: strconv.Itoa(hexR(hex))}, commaToken,
{Kind: css_lexer.TNumber, Text: strconv.Itoa(hexG(hex))}, commaToken,
{Kind: css_lexer.TNumber, Text: strconv.Itoa(hexB(hex))}, commaToken,
{Kind: css_lexer.TNumber, Text: floatToStringForColor(float64(hexA(hex)) / 255)},
{Loc: token.Loc, Kind: css_lexer.TNumber, Text: strconv.Itoa(hexR(hex))}, commaToken,
{Loc: token.Loc, Kind: css_lexer.TNumber, Text: strconv.Itoa(hexG(hex))}, commaToken,
{Loc: token.Loc, Kind: css_lexer.TNumber, Text: strconv.Itoa(hexB(hex))}, commaToken,
{Loc: token.Loc, Kind: css_lexer.TNumber, Text: floatToStringForColor(float64(hexA(hex)) / 255)},
}
}

Expand All @@ -303,12 +303,12 @@ func (p *parser) lowerColor(token css_ast.Token) css_ast.Token {
if hex, ok := parseHex(text); ok {
token.Kind = css_lexer.TFunction
token.Text = "rgba"
commaToken := p.commaToken()
commaToken := p.commaToken(token.Loc)
token.Children = &[]css_ast.Token{
{Kind: css_lexer.TNumber, Text: strconv.Itoa(hexR(hex))}, commaToken,
{Kind: css_lexer.TNumber, Text: strconv.Itoa(hexG(hex))}, commaToken,
{Kind: css_lexer.TNumber, Text: strconv.Itoa(hexB(hex))}, commaToken,
{Kind: css_lexer.TNumber, Text: floatToStringForColor(float64(hexA(hex)) / 255)},
{Loc: token.Loc, Kind: css_lexer.TNumber, Text: strconv.Itoa(hexR(hex))}, commaToken,
{Loc: token.Loc, Kind: css_lexer.TNumber, Text: strconv.Itoa(hexG(hex))}, commaToken,
{Loc: token.Loc, Kind: css_lexer.TNumber, Text: strconv.Itoa(hexB(hex))}, commaToken,
{Loc: token.Loc, Kind: css_lexer.TNumber, Text: floatToStringForColor(float64(hexA(hex)) / 255)},
}
}
}
Expand Down Expand Up @@ -346,7 +346,7 @@ func (p *parser) lowerColor(token css_ast.Token) css_ast.Token {
removeAlpha = true
args[0].Whitespace = 0
args[1].Whitespace = 0
commaToken := p.commaToken()
commaToken := p.commaToken(token.Loc)
token.Children = &[]css_ast.Token{
args[0], commaToken,
args[1], commaToken,
Expand All @@ -372,7 +372,7 @@ func (p *parser) lowerColor(token css_ast.Token) css_ast.Token {
args[0].Whitespace = 0
args[1].Whitespace = 0
args[2].Whitespace = 0
commaToken := p.commaToken()
commaToken := p.commaToken(token.Loc)
token.Children = &[]css_ast.Token{
args[0], commaToken,
args[1], commaToken,
Expand Down Expand Up @@ -632,17 +632,17 @@ func (p *parser) mangleColor(token css_ast.Token, hex uint32) css_ast.Token {
} else {
token.Kind = css_lexer.TFunction
token.Text = "rgba"
commaToken := p.commaToken()
commaToken := p.commaToken(token.Loc)
index := hexA(hex) * 4
alpha := alphaFractionTable[index : index+4]
if space := strings.IndexByte(alpha, ' '); space != -1 {
alpha = alpha[:space]
}
token.Children = &[]css_ast.Token{
{Kind: css_lexer.TNumber, Text: strconv.Itoa(hexR(hex))}, commaToken,
{Kind: css_lexer.TNumber, Text: strconv.Itoa(hexG(hex))}, commaToken,
{Kind: css_lexer.TNumber, Text: strconv.Itoa(hexB(hex))}, commaToken,
{Kind: css_lexer.TNumber, Text: alpha},
{Loc: token.Loc, Kind: css_lexer.TNumber, Text: strconv.Itoa(hexR(hex))}, commaToken,
{Loc: token.Loc, Kind: css_lexer.TNumber, Text: strconv.Itoa(hexG(hex))}, commaToken,
{Loc: token.Loc, Kind: css_lexer.TNumber, Text: strconv.Itoa(hexB(hex))}, commaToken,
{Loc: token.Loc, Kind: css_lexer.TNumber, Text: alpha},
}
}

Expand Down
1 change: 1 addition & 0 deletions internal/css_parser/css_decls_font_family.go
Original file line number Diff line number Diff line change
Expand Up @@ -102,6 +102,7 @@ func (p *parser) mangleFamilyNameOrGenericName(result []css_ast.Token, tokens []
whitespace = css_ast.WhitespaceBefore
}
result = append(result, css_ast.Token{
Loc: t.Loc,
Kind: css_lexer.TIdent,
Text: name,
Whitespace: whitespace,
Expand Down
1 change: 1 addition & 0 deletions internal/css_parser/css_parser.go
Original file line number Diff line number Diff line change
Expand Up @@ -1523,6 +1523,7 @@ loop:
break loop
}
token := css_ast.Token{
Loc: t.Range.Loc,
Kind: t.Kind,
Text: t.DecodedText(p.source.Contents),
Whitespace: nextWhitespace,
Expand Down
10 changes: 4 additions & 6 deletions internal/css_printer/css_printer.go
Original file line number Diff line number Diff line change
Expand Up @@ -263,9 +263,6 @@ func (p *printer) printRule(rule css_ast.Rule, indent int32, omitTrailingSemicol
p.printRuleBlock(r.Rules, indent, r.CloseBraceLoc)

case *css_ast.RQualified:
if p.options.AddSourceMappings {
p.builder.AddSourceMapping(rule.Loc, "", p.css)
}
hasWhitespaceAfter := p.printTokens(r.Prelude, printTokensOpts{})
if !hasWhitespaceAfter && !p.options.MinifyWhitespace {
p.print(" ")
Expand Down Expand Up @@ -293,9 +290,6 @@ func (p *printer) printRule(rule css_ast.Rule, indent int32, omitTrailingSemicol
}

case *css_ast.RBadDeclaration:
if p.options.AddSourceMappings {
p.builder.AddSourceMapping(rule.Loc, "", p.css)
}
p.printTokens(r.Tokens, printTokensOpts{})
if !omitTrailingSemicolon {
p.print(";")
Expand Down Expand Up @@ -934,6 +928,10 @@ func (p *printer) printTokens(tokens []css_ast.Token, opts printTokensOpts) bool
whitespace = canDiscardWhitespaceAfter
}

if p.options.AddSourceMappings {
p.builder.AddSourceMapping(t.Loc, "", p.css)
}

switch t.Kind {
case css_lexer.TIdent:
p.printIdent(t.Text, identNormal, whitespace)
Expand Down
2 changes: 1 addition & 1 deletion scripts/js-api-tests.js
Original file line number Diff line number Diff line change
Expand Up @@ -1576,7 +1576,7 @@ body {
},
},
[makePath(output + '.map')]: {
bytes: 325,
bytes: 335,
exports: [],
imports: [],
inputs: {},
Expand Down

0 comments on commit e3f6eb8

Please sign in to comment.