Skip to content

Commit ae87b83

Browse files
committed
Merge adjacent @media and @supports rules with identical queries
Closes parcel-bundler#104
1 parent ab1d1e1 commit ae87b83

File tree

2 files changed

+162
-4
lines changed

2 files changed

+162
-4
lines changed

src/lib.rs

Lines changed: 142 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6504,6 +6504,7 @@ mod tests {
65046504
},
65056505
);
65066506
}
6507+
65076508
#[test]
65086509
fn test_merge_layers() {
65096510
test(
@@ -6537,6 +6538,7 @@ mod tests {
65376538
"#},
65386539
);
65396540
}
6541+
65406542
#[test]
65416543
fn test_merge_rules() {
65426544
test(
@@ -7063,6 +7065,146 @@ mod tests {
70637065
);
70647066
}
70657067

7068+
#[test]
7069+
fn test_merge_media_rules() {
7070+
test(
7071+
r#"
7072+
@media (hover) {
7073+
.foo {
7074+
color: red;
7075+
}
7076+
}
7077+
@media (hover) {
7078+
.foo {
7079+
background: #fff;
7080+
}
7081+
7082+
.baz {
7083+
color: #fff;
7084+
}
7085+
}
7086+
"#,
7087+
indoc! {r#"
7088+
@media (hover) {
7089+
.foo {
7090+
color: red;
7091+
background: #fff;
7092+
}
7093+
7094+
.baz {
7095+
color: #fff;
7096+
}
7097+
}
7098+
"#},
7099+
);
7100+
7101+
test(
7102+
r#"
7103+
@media (hover) {
7104+
.foo {
7105+
color: red;
7106+
}
7107+
}
7108+
@media (min-width: 250px) {
7109+
.foo {
7110+
background: #fff;
7111+
}
7112+
7113+
.baz {
7114+
color: #fff;
7115+
}
7116+
}
7117+
"#,
7118+
indoc! {r#"
7119+
@media (hover) {
7120+
.foo {
7121+
color: red;
7122+
}
7123+
}
7124+
7125+
@media (min-width: 250px) {
7126+
.foo {
7127+
background: #fff;
7128+
}
7129+
7130+
.baz {
7131+
color: #fff;
7132+
}
7133+
}
7134+
"#},
7135+
);
7136+
}
7137+
7138+
#[test]
7139+
fn test_merge_supports() {
7140+
test(
7141+
r#"
7142+
@supports (flex: 1) {
7143+
.foo {
7144+
color: red;
7145+
}
7146+
}
7147+
@supports (flex: 1) {
7148+
.foo {
7149+
background: #fff;
7150+
}
7151+
7152+
.baz {
7153+
color: #fff;
7154+
}
7155+
}
7156+
"#,
7157+
indoc! {r#"
7158+
@supports (flex: 1) {
7159+
.foo {
7160+
color: red;
7161+
background: #fff;
7162+
}
7163+
7164+
.baz {
7165+
color: #fff;
7166+
}
7167+
}
7168+
"#},
7169+
);
7170+
7171+
test(
7172+
r#"
7173+
@supports (flex: 1) {
7174+
.foo {
7175+
color: red;
7176+
}
7177+
}
7178+
@supports (display: grid) {
7179+
.foo {
7180+
background: #fff;
7181+
}
7182+
7183+
.baz {
7184+
color: #fff;
7185+
}
7186+
}
7187+
"#,
7188+
indoc! {r#"
7189+
@supports (flex: 1) {
7190+
.foo {
7191+
color: red;
7192+
}
7193+
}
7194+
7195+
@supports (display: grid) {
7196+
.foo {
7197+
background: #fff;
7198+
}
7199+
7200+
.baz {
7201+
color: #fff;
7202+
}
7203+
}
7204+
"#},
7205+
);
7206+
}
7207+
70667208
#[test]
70677209
fn test_opacity() {
70687210
minify_test(".foo { opacity: 0 }", ".foo{opacity:0}");

src/rules/mod.rs

Lines changed: 20 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -291,21 +291,37 @@ impl<'i> CssRuleList<'i> {
291291
}
292292
}
293293
CssRule::Media(media) => {
294+
if let Some(CssRule::Media(last_rule)) = rules.last_mut() {
295+
if last_rule.query == media.query {
296+
last_rule.rules.0.extend(media.rules.0.drain(..));
297+
last_rule.minify(context, parent_is_unused)?;
298+
continue;
299+
}
300+
}
301+
294302
if media.minify(context, parent_is_unused)? {
295303
continue;
296304
}
297305
}
298306
CssRule::Supports(supports) => {
307+
if let Some(CssRule::Supports(last_rule)) = rules.last_mut() {
308+
if last_rule.condition == supports.condition {
309+
last_rule.rules.0.extend(supports.rules.0.drain(..));
310+
last_rule.minify(context, parent_is_unused)?;
311+
continue;
312+
}
313+
}
314+
299315
supports.minify(context, parent_is_unused)?;
300316
if supports.rules.0.is_empty() {
301317
continue;
302318
}
303319
}
304320
CssRule::LayerBlock(layer) => {
305-
if let Some(CssRule::LayerBlock(last_style_rule)) = rules.last_mut() {
306-
if last_style_rule.name == layer.name {
307-
last_style_rule.rules.0.extend(layer.rules.0.drain(..));
308-
last_style_rule.minify(context, parent_is_unused)?;
321+
if let Some(CssRule::LayerBlock(last_rule)) = rules.last_mut() {
322+
if last_rule.name == layer.name {
323+
last_rule.rules.0.extend(layer.rules.0.drain(..));
324+
last_rule.minify(context, parent_is_unused)?;
309325
continue;
310326
}
311327
}

0 commit comments

Comments
 (0)