@@ -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
4749type 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 ,
0 commit comments