Skip to content

Commit 44b4641

Browse files
authored
Merge adjacent layers with the same name (parcel-bundler#216)
1 parent 3dd150d commit 44b4641

File tree

3 files changed

+59
-2
lines changed

3 files changed

+59
-2
lines changed

src/lib.rs

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6471,7 +6471,42 @@ mod tests {
64716471
},
64726472
);
64736473
}
6474+
#[test]
6475+
fn test_merge_layers() {
6476+
test(
6477+
r#"
6478+
@layer foo {
6479+
.foo {
6480+
color: red;
6481+
}
6482+
}
6483+
@layer foo {
6484+
.bar {
6485+
background: #fff;
6486+
}
6487+
6488+
.baz {
6489+
color: #fff;
6490+
}
6491+
}
6492+
"#,
6493+
indoc! {r#"
6494+
@layer foo {
6495+
.foo {
6496+
color: red;
6497+
}
64746498
6499+
.bar {
6500+
background: #fff;
6501+
}
6502+
6503+
.baz {
6504+
color: #fff;
6505+
}
6506+
}
6507+
"#},
6508+
);
6509+
}
64756510
#[test]
64766511
fn test_merge_rules() {
64776512
test(

src/rules/layer.rs

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
//! The `@layer` rule.
22
3-
use super::{CssRuleList, Location};
4-
use crate::error::{ParserError, PrinterError};
3+
use super::{CssRuleList, Location, MinifyContext};
4+
use crate::error::{MinifyError, ParserError, PrinterError};
55
use crate::printer::Printer;
66
use crate::traits::{Parse, ToCss};
77
use crate::values::string::CowArcStr;
@@ -113,6 +113,18 @@ pub struct LayerBlockRule<'i> {
113113
pub loc: Location,
114114
}
115115

116+
impl<'i> LayerBlockRule<'i> {
117+
pub(crate) fn minify(
118+
&mut self,
119+
context: &mut MinifyContext<'_, 'i>,
120+
parent_is_unused: bool,
121+
) -> Result<bool, MinifyError> {
122+
self.rules.minify(context, parent_is_unused)?;
123+
124+
Ok(self.rules.0.is_empty())
125+
}
126+
}
127+
116128
impl<'i> ToCss for LayerBlockRule<'i> {
117129
fn to_css<W>(&self, dest: &mut Printer<W>) -> Result<(), PrinterError>
118130
where

src/rules/mod.rs

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -301,6 +301,16 @@ impl<'i> CssRuleList<'i> {
301301
continue;
302302
}
303303
}
304+
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+
}
309+
if layer.minify(context, parent_is_unused)? {
310+
continue;
311+
}
312+
}
313+
}
304314
CssRule::MozDocument(document) => document.minify(context)?,
305315
CssRule::Style(style) => {
306316
if parent_is_unused || style.minify(context, parent_is_unused)? {

0 commit comments

Comments
 (0)