parcel-bundler / parcel-bundler/lightningcss
Problem trying to remove unused selectors from rust
Nobody has claimed this yet.
- Dominant language
- Rust
- Stars
- 7.7k
- Forks
- 302
- PR merge metrics
- No merged PRs in 30d
Description
Hello there, good day! Here I come again.
So, since last time, I've come with a second problem in one of the things I've been developing. Background: "I've collected a HashSet of unused CSS classes, and I'm using the visitor api to loop through them. Am I able to remove those classes with the visitor api?"
My HashSet looks something like this:
unused_symbols: std::collections::HashSet::from([
".counties".to_string(),
".counties h2::before".to_string()
]),
OBS: it's computer generated, so I've a couple hundred symbols like that.
My code to remove the css would look something like this:
pub struct CssRemover {
pub used: HashSet<String>,
pub unused: std::collections::HashSet<String>,
}
impl CssRemover {
pub fn new(used: &HashSet<String>) -> Self {
Self {
used: used.clone(),
unused: std::collections::HashSet::new(),
}
}
}
impl<'i> Visitor<'i> for CssRemover {
type Error = shared::anyhow::Error;
// const TYPES: VisitTypes = visit_types!(SELECTORS | MEDIA_QUERIES);
const TYPES: VisitTypes = visit_types!(SELECTORS);
// Todo: implement the unused keyframes as well, visit children to see if they have the animation anywhere
fn visit_media_query(&mut self, query: &mut MediaQuery<'i>) -> std::result::Result<(), Self::Error> {
log::info!("visited media: {:?}", query);
Ok(())
}
fn visit_selector(&mut self, sel: &mut Selector<'i>) -> std::result::Result<(), Self::Error> {
let local_name = sel.to_css_string(shared::lightningcss::stylesheet::PrinterOptions::default());
if let Err(e) = local_name {
log::error!("Error getting the name of the css selector: {e:?}");
return Ok(())
}
let local_name = local_name.unwrap();
if !self.used.contains(&local_name) {
log::error!("Unused property found: {local_name}");
// return Err(anyhow!("Testing removal"));
// I want to remove the selector here
if self.unused.insert(local_name) {
log::info!("Property added {}", self.unused.len());
}
}
Ok(())
}
}
Then in later own:
let mut visitor = CssRemover::new(used); // used is the hashmap with the used selectors in it
let content = // ... comes with the stylesheet content
let sheet = StyleSheet::parse(&content, ParserOptions {
error_recovery: true,
..ParserOptions::default()
});
if let Err(e) = sheet {
bail!("{e:?}")
}
let mut sheet = sheet.unwrap();
sheet.visit(&mut visitor)?; // uses the custom visitor
sheet.minify(MinifyOptions {
unused_symbols: std::collections::HashSet::from([
".counties".to_string(),
".counties h2::before".to_string()
]),
// This would be the visitor.unused, however I doesn't remove the unused properties (I tested that)
})?;
let output = sheet.to_css(PrinterOptions {
minify: false,
..PrinterOptions::default()
})?;
Contributor guide
First steps
- Read the whole issue, then the project's contributing guide.
- Comment on the issue to say you are picking it up — it saves two people doing the same work.
- Fork the repository and make your change on a branch.
- Open a pull request that references the issue number.
Research direction
Start with the Visitor implementation, especially visit_selector, and the StyleSheet::visit and minify calls shown in the issue. Reproduce the example with .counties and .counties h2::before, then inspect how MinifyOptions.unused_symbols is handled. Done means the supported way to remove unused selectors is established and verified, or the limitation is clearly documented.
Written by the indexing model from the issue text.
Assessment
- Tech stack
- rust
- Domain
- compilers
- Issue type
- Bug
- Difficulty
- 4/5
- Estimated time
- 3-5 days
- Activity status
- Stale
- Clarity
- Needs clarification
- Newbie friendliness
- 25/100