Skip to content

Commit bb16fb7

Browse files
committed
css: parser separates layers pre/post @import
1 parent 78b59e6 commit bb16fb7

3 files changed

Lines changed: 32 additions & 9 deletions

File tree

internal/css_ast/css_ast.go

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -35,9 +35,12 @@ type AST struct {
3535
GlobalScope map[string]ast.LocRef
3636
Composes map[ast.Ref]*Composes
3737

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

4346
type Composes struct {

internal/css_parser/css_parser.go

Lines changed: 14 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,8 @@ type parser struct {
3131
nestingWarnings map[logger.Loc]struct{}
3232
tracker logger.LineColumnTracker
3333
enclosingAtMedia [][]css_ast.Token
34-
layers [][]string
34+
layersPreImport [][]string
35+
layersPostImport [][]string
3536
enclosingLayer []string
3637
anonLayerCount int
3738
index int
@@ -42,6 +43,7 @@ type parser struct {
4243
options Options
4344
nestingIsPresent bool
4445
makeLocalSymbols bool
46+
hasSeenAtImport bool
4547
}
4648

4749
type Options struct {
@@ -153,7 +155,8 @@ func Parse(log logger.Log, source logger.Source, options Options) css_ast.AST {
153155
LocalScope: p.localScope,
154156
GlobalScope: p.globalScope,
155157
Composes: p.composes,
156-
Layers: p.layers,
158+
LayersPreImport: p.layersPreImport,
159+
LayersPostImport: p.layersPostImport,
157160
}
158161
}
159162

@@ -359,7 +362,7 @@ func (p *parser) recordAtLayerRule(layers [][]string) {
359362
clone := make([]string, 0, len(p.enclosingLayer)+len(layer))
360363
layer = append(append(clone, p.enclosingLayer...), layer...)
361364
}
362-
p.layers = append(p.layers, layer)
365+
p.layersPostImport = append(p.layersPostImport, layer)
363366
}
364367
}
365368

@@ -1248,6 +1251,14 @@ abortRuleParser:
12481251
Path: logger.Path{Text: path},
12491252
Range: r,
12501253
})
1254+
1255+
// Fill in the pre-import layers once we see the first "@import"
1256+
if !p.hasSeenAtImport {
1257+
p.hasSeenAtImport = true
1258+
p.layersPreImport = p.layersPostImport
1259+
p.layersPostImport = nil
1260+
}
1261+
12511262
return css_ast.Rule{Loc: atRange.Loc, Data: &css_ast.RAtImport{
12521263
ImportRecordIndex: importRecordIndex,
12531264
ImportConditions: importConditions,

internal/linker/linker.go

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3496,7 +3496,8 @@ func (c *linkerContext) findImportedFilesInCSSOrder(entryPoints []uint32) (exter
34963496

34973497
// Remove this redundant entry either if it has no named layers
34983498
// or if it's wrapped in an anonymous layer without a name
3499-
hasNamedLayers := len(c.graph.Files[order.sourceIndex].InputFile.Repr.(*graph.CSSRepr).AST.Layers) > 0
3499+
tree := c.graph.Files[order.sourceIndex].InputFile.Repr.(*graph.CSSRepr).AST
3500+
hasNamedLayers := len(tree.LayersPreImport) > 0 || len(tree.LayersPostImport) > 0
35003501
hasAnonymousLayer := false
35013502
for _, conditions := range order.conditions {
35023503
if len(conditions.Layers) == 1 {
@@ -5891,9 +5892,17 @@ func (c *linkerContext) generateChunkCSS(chunkIndex int, chunkWaitGroup *sync.Wa
58915892
}
58925893
rules = append(rules, rule)
58935894
}
5894-
} else if len(ast.Layers) > 0 {
5895+
} else if pre, post := len(ast.LayersPreImport), len(ast.LayersPostImport); pre > 0 || post > 0 {
58955896
// Only include "@layer" information for redundant import order entries
5896-
rules = []css_ast.Rule{{Data: &css_ast.RAtLayer{Names: ast.Layers}}}
5897+
var layers [][]string
5898+
if pre == 0 {
5899+
layers = ast.LayersPostImport
5900+
} else if post == 0 {
5901+
layers = ast.LayersPreImport
5902+
} else {
5903+
layers = append(append(make([][]string, 0, pre+post), ast.LayersPreImport...), ast.LayersPostImport...)
5904+
}
5905+
rules = []css_ast.Rule{{Data: &css_ast.RAtLayer{Names: layers}}}
58975906
}
58985907

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

0 commit comments

Comments
 (0)