Skip to content

Commit

Permalink
css: add source mappings for renamed local names
Browse files Browse the repository at this point in the history
  • Loading branch information
evanw committed Jul 16, 2023
1 parent e29b8b6 commit 82706ab
Show file tree
Hide file tree
Showing 6 changed files with 28 additions and 18 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;AAAI;AAAA;",
"mappings": ";AAEI;AAAI;AAAyB;",
"names": []
}

Expand Down Expand Up @@ -58,7 +58,7 @@ a {
"imports": [],
"exports": [],
"inputs": {},
"bytes": 198
"bytes": 199
},
"entry.css": {
"imports": [
Expand Down
2 changes: 1 addition & 1 deletion internal/config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -234,7 +234,7 @@ func (loader Loader) IsTypeScript() bool {

func (loader Loader) CanHaveSourceMap() bool {
switch loader {
case LoaderJS, LoaderJSX, LoaderTS, LoaderTSNoAmbiguousLessThan, LoaderTSX, LoaderCSS, LoaderJSON, LoaderText:
case LoaderJS, LoaderJSX, LoaderTS, LoaderTSNoAmbiguousLessThan, LoaderTSX, LoaderCSS, LoaderLocalCSS, LoaderJSON, LoaderText:
return true
default:
return false
Expand Down
16 changes: 8 additions & 8 deletions internal/css_ast/css_ast.go
Original file line number Diff line number Diff line change
Expand Up @@ -786,18 +786,18 @@ type SS interface {
}

type SSHash struct {
Ref ast.Ref
Name ast.LocRef
}

func (a *SSHash) Equal(ss SS, check *CrossFileEqualityCheck) bool {
b, ok := ss.(*SSHash)
return ok && a.Ref == b.Ref
return ok && a.Name.Ref == b.Name.Ref
}

func (ss *SSHash) Hash() uint32 {
hash := uint32(1)
hash = helpers.HashCombine(hash, ss.Ref.SourceIndex)
hash = helpers.HashCombine(hash, ss.Ref.InnerIndex)
hash = helpers.HashCombine(hash, ss.Name.Ref.SourceIndex)
hash = helpers.HashCombine(hash, ss.Name.Ref.InnerIndex)
return hash
}

Expand All @@ -807,18 +807,18 @@ func (ss *SSHash) Clone() SS {
}

type SSClass struct {
Ref ast.Ref
Name ast.LocRef
}

func (a *SSClass) Equal(ss SS, check *CrossFileEqualityCheck) bool {
b, ok := ss.(*SSClass)
return ok && a.Ref == b.Ref
return ok && a.Name.Ref == b.Name.Ref
}

func (ss *SSClass) Hash() uint32 {
hash := uint32(2)
hash = helpers.HashCombine(hash, ss.Ref.SourceIndex)
hash = helpers.HashCombine(hash, ss.Ref.InnerIndex)
hash = helpers.HashCombine(hash, ss.Name.Ref.SourceIndex)
hash = helpers.HashCombine(hash, ss.Name.Ref.InnerIndex)
return hash
}

Expand Down
11 changes: 9 additions & 2 deletions internal/css_parser/css_parser_selector.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package css_parser
import (
"fmt"

"github.com/evanw/esbuild/internal/ast"
"github.com/evanw/esbuild/internal/css_ast"
"github.com/evanw/esbuild/internal/css_lexer"
"github.com/evanw/esbuild/internal/logger"
Expand Down Expand Up @@ -298,14 +299,20 @@ subclassSelectors:
if (p.current().Flags & css_lexer.IsID) == 0 {
break subclassSelectors
}
nameLoc := logger.Loc{Start: p.current().Range.Loc.Start + 1}
name := p.decoded()
sel.SubclassSelectors = append(sel.SubclassSelectors, &css_ast.SSHash{Ref: p.symbolForName(name)})
sel.SubclassSelectors = append(sel.SubclassSelectors, &css_ast.SSHash{
Name: ast.LocRef{Loc: nameLoc, Ref: p.symbolForName(name)},
})
p.advance()

case css_lexer.TDelimDot:
p.advance()
nameLoc := p.current().Range.Loc
name := p.decoded()
sel.SubclassSelectors = append(sel.SubclassSelectors, &css_ast.SSClass{Ref: p.symbolForName(name)})
sel.SubclassSelectors = append(sel.SubclassSelectors, &css_ast.SSClass{
Name: ast.LocRef{Loc: nameLoc, Ref: p.symbolForName(name)},
})
if !p.expect(css_lexer.TIdent) {
return
}
Expand Down
11 changes: 7 additions & 4 deletions internal/css_printer/css_printer.go
Original file line number Diff line number Diff line change
Expand Up @@ -432,11 +432,11 @@ func (p *printer) printCompoundSelector(sel css_ast.CompoundSelector, isFirst bo

// This deliberately does not use identHash. From the specification:
// "In <id-selector>, the <hash-token>'s value must be an identifier."
p.printSymbol(s.Ref, identNormal, whitespace)
p.printSymbol(s.Name.Loc, s.Name.Ref, identNormal, whitespace)

case *css_ast.SSClass:
p.print(".")
p.printSymbol(s.Ref, identNormal, whitespace)
p.printSymbol(s.Name.Loc, s.Name.Ref, identNormal, whitespace)

case *css_ast.SSAttribute:
p.print("[")
Expand Down Expand Up @@ -819,11 +819,14 @@ func (p *printer) printIdent(text string, mode identMode, whitespace trailingWhi
}
}

func (p *printer) printSymbol(ref ast.Ref, mode identMode, whitespace trailingWhitespace) {
func (p *printer) printSymbol(loc logger.Loc, ref ast.Ref, mode identMode, whitespace trailingWhitespace) {
ref = ast.FollowSymbols(p.symbols, ref)
originalName := p.symbols.Get(ref).OriginalName
name, ok := p.options.LocalNames[ref]
if !ok {
name = p.symbols.Get(ref).OriginalName
name = originalName
} else if p.options.AddSourceMappings && name != originalName {
p.builder.AddSourceMapping(loc, originalName, p.css)
}
p.printIdent(name, mode, 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: 312,
bytes: 315,
exports: [],
imports: [],
inputs: {},
Expand Down

0 comments on commit 82706ab

Please sign in to comment.