Skip to content

Commit

Permalink
css: parser separates layers pre/post @import
Browse files Browse the repository at this point in the history
  • Loading branch information
evanw committed Aug 11, 2023
1 parent 78b59e6 commit bb16fb7
Show file tree
Hide file tree
Showing 3 changed files with 32 additions and 9 deletions.
9 changes: 6 additions & 3 deletions internal/css_ast/css_ast.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,9 +35,12 @@ type AST struct {
GlobalScope map[string]ast.LocRef
Composes map[ast.Ref]*Composes

// This contains all layer names in the file. It can be used to replace the
// layer-related side effects of importing this file.
Layers [][]string
// These contain all layer names in the file. It can be used to replace the
// layer-related side effects of importing this file. They are split into two
// groups (those before and after "@import" rules) so that the linker can put
// them in the right places.
LayersPreImport [][]string
LayersPostImport [][]string
}

type Composes struct {
Expand Down
17 changes: 14 additions & 3 deletions internal/css_parser/css_parser.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,8 @@ type parser struct {
nestingWarnings map[logger.Loc]struct{}
tracker logger.LineColumnTracker
enclosingAtMedia [][]css_ast.Token
layers [][]string
layersPreImport [][]string
layersPostImport [][]string
enclosingLayer []string
anonLayerCount int
index int
Expand All @@ -42,6 +43,7 @@ type parser struct {
options Options
nestingIsPresent bool
makeLocalSymbols bool
hasSeenAtImport bool
}

type Options struct {
Expand Down Expand Up @@ -153,7 +155,8 @@ func Parse(log logger.Log, source logger.Source, options Options) css_ast.AST {
LocalScope: p.localScope,
GlobalScope: p.globalScope,
Composes: p.composes,
Layers: p.layers,
LayersPreImport: p.layersPreImport,
LayersPostImport: p.layersPostImport,
}
}

Expand Down Expand Up @@ -359,7 +362,7 @@ func (p *parser) recordAtLayerRule(layers [][]string) {
clone := make([]string, 0, len(p.enclosingLayer)+len(layer))
layer = append(append(clone, p.enclosingLayer...), layer...)
}
p.layers = append(p.layers, layer)
p.layersPostImport = append(p.layersPostImport, layer)
}
}

Expand Down Expand Up @@ -1248,6 +1251,14 @@ abortRuleParser:
Path: logger.Path{Text: path},
Range: r,
})

// Fill in the pre-import layers once we see the first "@import"
if !p.hasSeenAtImport {
p.hasSeenAtImport = true
p.layersPreImport = p.layersPostImport
p.layersPostImport = nil
}

return css_ast.Rule{Loc: atRange.Loc, Data: &css_ast.RAtImport{
ImportRecordIndex: importRecordIndex,
ImportConditions: importConditions,
Expand Down
15 changes: 12 additions & 3 deletions internal/linker/linker.go
Original file line number Diff line number Diff line change
Expand Up @@ -3496,7 +3496,8 @@ func (c *linkerContext) findImportedFilesInCSSOrder(entryPoints []uint32) (exter

// Remove this redundant entry either if it has no named layers
// or if it's wrapped in an anonymous layer without a name
hasNamedLayers := len(c.graph.Files[order.sourceIndex].InputFile.Repr.(*graph.CSSRepr).AST.Layers) > 0
tree := c.graph.Files[order.sourceIndex].InputFile.Repr.(*graph.CSSRepr).AST
hasNamedLayers := len(tree.LayersPreImport) > 0 || len(tree.LayersPostImport) > 0
hasAnonymousLayer := false
for _, conditions := range order.conditions {
if len(conditions.Layers) == 1 {
Expand Down Expand Up @@ -5891,9 +5892,17 @@ func (c *linkerContext) generateChunkCSS(chunkIndex int, chunkWaitGroup *sync.Wa
}
rules = append(rules, rule)
}
} else if len(ast.Layers) > 0 {
} else if pre, post := len(ast.LayersPreImport), len(ast.LayersPostImport); pre > 0 || post > 0 {
// Only include "@layer" information for redundant import order entries
rules = []css_ast.Rule{{Data: &css_ast.RAtLayer{Names: ast.Layers}}}
var layers [][]string
if pre == 0 {
layers = ast.LayersPostImport
} else if post == 0 {
layers = ast.LayersPreImport
} else {
layers = append(append(make([][]string, 0, pre+post), ast.LayersPreImport...), ast.LayersPostImport...)
}
rules = []css_ast.Rule{{Data: &css_ast.RAtLayer{Names: layers}}}
}

for i := len(entry.conditions) - 1; i >= 0; i-- {
Expand Down

0 comments on commit bb16fb7

Please sign in to comment.