Skip to content

Commit 6d05c73

Browse files
committed
Add onlyTopLevel option prevent eject nested media queries from parent
1 parent 1efc41d commit 6d05c73

File tree

5 files changed

+118
-14
lines changed

5 files changed

+118
-14
lines changed

index.js

+36-13
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ module.exports = (opts = {}) => {
1212
{
1313
sort: 'mobile-first',
1414
configuration: false,
15+
onlyTopLevel: false,
1516
},
1617
opts
1718
)
@@ -26,26 +27,48 @@ module.exports = (opts = {}) => {
2627
let atRules = [];
2728

2829
root.walkAtRules('media', atRule => {
29-
let query = atRule.params
30+
if (opts.onlyTopLevel && atRule.parent.type === 'root') {
31+
let query = atRule.params
3032

31-
if (!atRules[query]) {
32-
atRules[query] = new AtRule({
33-
name: atRule.name,
34-
params: atRule.params,
35-
source: atRule.source
33+
if (!atRules[query]) {
34+
atRules[query] = new AtRule({
35+
name: atRule.name,
36+
params: atRule.params,
37+
source: atRule.source
38+
})
39+
}
40+
41+
atRule.nodes.forEach(node => {
42+
atRules[query].append(node.clone())
3643
})
44+
45+
atRule.remove()
3746
}
3847

39-
atRule.nodes.forEach(node => {
40-
atRules[query].append(node.clone())
41-
})
48+
if (!opts.onlyTopLevel) {
49+
let query = atRule.params
4250

43-
atRule.remove()
44-
})
51+
if (!atRules[query]) {
52+
atRules[query] = new AtRule({
53+
name: atRule.name,
54+
params: atRule.params,
55+
source: atRule.source
56+
})
57+
}
4558

46-
sortAtRules(Object.keys(atRules), opts.sort, sortCSSmq).forEach(query => {
47-
root.append(atRules[query])
59+
atRule.nodes.forEach(node => {
60+
atRules[query].append(node.clone())
61+
})
62+
63+
atRule.remove()
64+
}
4865
})
66+
67+
if (atRules) {
68+
sortAtRules(Object.keys(atRules), opts.sort, sortCSSmq).forEach(query => {
69+
root.append(atRules[query])
70+
})
71+
}
4972
}
5073
}
5174
}

index.test.js

+6
Original file line numberDiff line numberDiff line change
@@ -146,3 +146,9 @@ it('postcss nested -> media minmax', async () => {
146146
let output = fs.readFileSync('./test/postcss.media.minmax.out.css', 'utf8')
147147
await mediaMinmaxAfterNestedRun(input, output)
148148
})
149+
150+
it('only top level', async () => {
151+
let input = fs.readFileSync('./test/only-top-level.in.css', 'utf8')
152+
let output = fs.readFileSync('./test/only-top-level.out.css', 'utf8')
153+
await run(input, output, { onlyTopLevel: true })
154+
})

package-lock.json

+1-1
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

test/only-top-level.in.css

+39
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
@media (min-width: 700px) {
2+
.a {
3+
color: #00FF00;
4+
}
5+
}
6+
7+
@supports (animation-name: test) {
8+
@media (min-width: 250px) {
9+
.b {
10+
color: #FF0000;
11+
}
12+
}
13+
14+
@media (min-width: 700px) {
15+
.b {
16+
color: #FF0000;
17+
}
18+
}
19+
20+
@media (min-width: 150px) {
21+
.b {
22+
color: #FF0000;
23+
}
24+
}
25+
}
26+
27+
@media print {
28+
@media (min-width: 700px) {
29+
.b {
30+
color: #FF0000;
31+
}
32+
}
33+
}
34+
35+
@media (width >= 400px) {
36+
.a {
37+
color: #00FF00;
38+
}
39+
}

test/only-top-level.out.css

+36
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
@supports (animation-name: test) {
2+
@media (min-width: 250px) {
3+
.b {
4+
color: #FF0000;
5+
}
6+
}
7+
8+
@media (min-width: 700px) {
9+
.b {
10+
color: #FF0000;
11+
}
12+
}
13+
14+
@media (min-width: 150px) {
15+
.b {
16+
color: #FF0000;
17+
}
18+
}
19+
}
20+
@media (width >= 400px) {
21+
.a {
22+
color: #00FF00;
23+
}
24+
}
25+
@media (min-width: 700px) {
26+
.a {
27+
color: #00FF00;
28+
}
29+
}
30+
@media print {
31+
@media (min-width: 700px) {
32+
.b {
33+
color: #FF0000;
34+
}
35+
}
36+
}

0 commit comments

Comments
 (0)