Skip to content

Commit 1efd103

Browse files
committed
internal/transformer: transform imports as well
1 parent dae5fe1 commit 1efd103

File tree

2 files changed

+43
-1
lines changed

2 files changed

+43
-1
lines changed

internal/transformer/custom_media_test.go

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,14 +3,18 @@ package transformer_test
33
import (
44
"testing"
55

6+
"github.com/stephen/cssc/internal/ast"
67
"github.com/stephen/cssc/internal/parser"
8+
"github.com/stephen/cssc/internal/printer"
79
"github.com/stephen/cssc/internal/sources"
810
"github.com/stephen/cssc/internal/transformer"
911
"github.com/stephen/cssc/transforms"
1012
"github.com/stretchr/testify/assert"
13+
"github.com/stretchr/testify/require"
1114
)
1215

1316
func TestCustomMedia(t *testing.T) {
17+
1418
assert.Equal(t, "@media (max-width:30em){.a{color:green}}@media (max-width:30em) and (script){.c{color:red}}", Transform(t, func(o *transformer.Options) {
1519
o.CustomMediaQueries = transforms.CustomMediaQueriesTransform
1620
}, `
@@ -55,3 +59,41 @@ func TestCustomMedia_Passthrough(t *testing.T) {
5559
.c { color: red; }
5660
}`))
5761
}
62+
63+
func TestCustomMediaViaImport(t *testing.T) {
64+
mainSource := &sources.Source{
65+
Path: "main.css",
66+
Content: `
67+
@import "other.css";
68+
69+
@media (--narrow-window) {
70+
.a { color: green; }
71+
}
72+
73+
@media (--narrow-window) and (script) {
74+
.c { color: red; }
75+
}`,
76+
}
77+
main, err := parser.Parse(mainSource)
78+
other, err := parser.Parse(&sources.Source{
79+
Path: "other.css",
80+
Content: `@custom-media --narrow-window (max-width: 30em);`,
81+
})
82+
83+
require.NoError(t, err)
84+
o := &transformer.Options{
85+
OriginalSource: mainSource,
86+
Reporter: &reporter{},
87+
ImportReplacements: map[*ast.AtRule]*ast.Stylesheet{
88+
main.Imports[0].AtRule: other,
89+
},
90+
Options: transforms.Options{
91+
CustomMediaQueries: transforms.CustomMediaQueriesTransform,
92+
},
93+
}
94+
95+
out, err := printer.Print(transformer.Transform(main, *o), printer.Options{})
96+
assert.NoError(t, err)
97+
98+
assert.Equal(t, "@media (max-width:30em){.a{color:green}}@media (max-width:30em) and (script){.c{color:red}}", out)
99+
}

internal/transformer/transformer.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -193,7 +193,7 @@ func (t *transformer) transformNodes(nodes []ast.Node) []ast.Node {
193193
t.addWarn(node, "@import transform does not yet support @supports or media queries")
194194
}
195195

196-
rv = append(rv, imported.Nodes...)
196+
rv = append(rv, t.transformNodes(imported.Nodes)...)
197197

198198
case "custom-media":
199199
func() {

0 commit comments

Comments
 (0)