Skip to content

Commit 8b28eed

Browse files
committed
Support nesting with unused symbols
1 parent 3902c1e commit 8b28eed

File tree

3 files changed

+41
-3
lines changed

3 files changed

+41
-3
lines changed

node/index.d.ts

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,12 @@ export interface TransformOptions {
2626
* Replaces user action pseudo classes with class names that can be applied from JavaScript.
2727
* This is useful for polyfills, for example.
2828
*/
29-
pseudoClasses?: PseudoClasses
29+
pseudoClasses?: PseudoClasses,
30+
/**
31+
* A list of class names, ids, and custom identifiers (e.g. @keyframes) that are known
32+
* to be unused. These will be removed during minification.
33+
*/
34+
unusedSymbols?: string[]
3035
}
3136

3237
export interface Drafts {

src/lib.rs

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9033,5 +9033,29 @@ mod tests {
90339033
});
90349034
let res = stylesheet.to_css(PrinterOptions::default()).unwrap();
90359035
assert_eq!(res.code, expected);
9036+
9037+
let source = r#"
9038+
.foo {
9039+
color: red;
9040+
9041+
&.bar {
9042+
color: green;
9043+
}
9044+
}
9045+
"#;
9046+
9047+
let expected = indoc!{r#"
9048+
.foo {
9049+
color: red;
9050+
}
9051+
"#};
9052+
9053+
let mut stylesheet = StyleSheet::parse("test.css".into(), source, ParserOptions { nesting: true, ..ParserOptions::default() }).unwrap();
9054+
stylesheet.minify(MinifyOptions {
9055+
unused_symbols: vec!["bar"].iter().map(|s| String::from(*s)).collect(),
9056+
..MinifyOptions::default()
9057+
});
9058+
let res = stylesheet.to_css(PrinterOptions::default()).unwrap();
9059+
assert_eq!(res.code, expected);
90369060
}
90379061
}

src/rules/style.rs

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,12 @@
11
use cssparser::SourceLocation;
22
use parcel_selectors::SelectorList;
3-
use crate::selector::{Selectors, is_compatible};
3+
use crate::selector::{Selectors, is_compatible, is_unused};
44
use crate::traits::ToCss;
55
use crate::printer::Printer;
66
use crate::declaration::DeclarationBlock;
77
use crate::vendor_prefix::VendorPrefix;
88
use crate::targets::Browsers;
9-
use crate::rules::{CssRuleList, ToCssWithContext, StyleContext};
9+
use crate::rules::{CssRuleList, CssRule, ToCssWithContext, StyleContext};
1010
use crate::compat::Feature;
1111
use crate::error::PrinterError;
1212
use super::MinifyContext;
@@ -23,6 +23,15 @@ pub struct StyleRule {
2323
impl StyleRule {
2424
pub(crate) fn minify(&mut self, context: &mut MinifyContext) {
2525
self.declarations.minify(context.handler, context.important_handler, context.logical_properties);
26+
27+
if !context.unused_symbols.is_empty() {
28+
self.rules.0.retain(|rule| {
29+
match rule {
30+
CssRule::Style(style) => !is_unused(&mut style.selectors.0.iter(), &context.unused_symbols),
31+
_ => true
32+
}
33+
});
34+
}
2635
}
2736

2837
pub fn is_compatible(&self, targets: Option<Browsers>) -> bool {

0 commit comments

Comments
 (0)