Skip to content

Commit

Permalink
Make minifier shareable
Browse files Browse the repository at this point in the history
  • Loading branch information
Mnwa committed Aug 17, 2024
1 parent 9b86eb9 commit 8bb64b3
Show file tree
Hide file tree
Showing 7 changed files with 50 additions and 48 deletions.
10 changes: 6 additions & 4 deletions css-minifier-web/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,9 @@ async fn minify_css(
minifier: web::Data<Minifier>,
) -> impl Responder {
let level = Level::from_str(&request.level).unwrap_or(Level::One);
let output_css = minifier.minify(&request.input_css, level).unwrap_or_else(|e| e.to_string());
let output_css = minifier
.minify(&request.input_css, level)
.unwrap_or_else(|e| e.to_string());

match Template::call(&IndexTemplate {
input_css: Some(request.into_inner().input_css),
Expand Down Expand Up @@ -73,9 +75,9 @@ async fn main() -> std::io::Result<()> {
.service(minify_css)
.service(main_css)
})
.bind(std::env::var("HTTP_HOST").unwrap_or_else(|_| "0.0.0.0:8081".into()))?
.run()
.await
.bind(std::env::var("HTTP_HOST").unwrap_or_else(|_| "0.0.0.0:8081".into()))?
.run()
.await
}

struct MinifiedCss {
Expand Down
8 changes: 4 additions & 4 deletions css-minify/src/optimizations/font.rs
Original file line number Diff line number Diff line change
Expand Up @@ -34,25 +34,25 @@ mod test {
Some(Selector::Class("test".into())),
vec![],
)]
.into(),
.into(),
parameters: {
let mut tmp = IndexMap::new();
tmp.insert("font-weight".into(), "bold".into());
tmp.into()
},
}), ])),
}),])),
CssEntities(vec![CssEntity::Block(Block {
selectors: vec![SelectorWithPseudoClasses(
Some(Selector::Class("test".into())),
vec![],
)]
.into(),
.into(),
parameters: {
let mut tmp = IndexMap::new();
tmp.insert("font-weight".into(), "700".into());
tmp.into()
},
}), ])
}),])
)
}
}
16 changes: 8 additions & 8 deletions css-minify/src/optimizations/merge_blocks.rs
Original file line number Diff line number Diff line change
Expand Up @@ -43,15 +43,15 @@ impl Transform for MergeBlocks {
screen,
entities: self.transform_many(entities),
}
.into(),
.into(),
CssEntity::Supports(Supports {
conditions,
entities,
}) => Supports {
conditions,
entities,
}) => Supports {
conditions,
entities: self.transform_many(entities),
}
.into(),
.into(),
entity => entity,
})
.collect(),
Expand Down Expand Up @@ -79,7 +79,7 @@ mod test {
Some(Selector::Class("test".into())),
vec![]
)]
.into(),
.into(),
parameters: {
let mut tmp = IndexMap::new();
tmp.insert("background-color".into(), "#f64e60 !important".into());
Expand All @@ -91,7 +91,7 @@ mod test {
Some(Selector::Class("test".into())),
vec![]
)]
.into(),
.into(),
parameters: {
let mut tmp = IndexMap::new();
tmp.insert("color".into(), "#f64e60 !important".into());
Expand All @@ -104,7 +104,7 @@ mod test {
Some(Selector::Class("test".into())),
vec![]
)]
.into(),
.into(),
parameters: {
let mut tmp = IndexMap::new();
tmp.insert("background-color".into(), "#f64e60 !important".into());
Expand Down
16 changes: 8 additions & 8 deletions css-minify/src/optimizations/merge_m_n_p.rs
Original file line number Diff line number Diff line change
Expand Up @@ -235,7 +235,7 @@ mod test {
Parameters(map)
},
}
.into()
.into()
),
Block {
selectors: Selectors::default(),
Expand All @@ -245,7 +245,7 @@ mod test {
Parameters(map)
},
}
.into()
.into()
)
}

Expand All @@ -264,7 +264,7 @@ mod test {
Parameters(map)
},
}
.into()
.into()
),
Block {
selectors: Selectors::default(),
Expand All @@ -274,7 +274,7 @@ mod test {
Parameters(map)
},
}
.into()
.into()
)
}

Expand All @@ -293,7 +293,7 @@ mod test {
Parameters(map)
},
}
.into()
.into()
),
Block {
selectors: Selectors::default(),
Expand All @@ -303,7 +303,7 @@ mod test {
Parameters(map)
},
}
.into()
.into()
)
}

Expand All @@ -322,7 +322,7 @@ mod test {
Parameters(map)
},
}
.into()
.into()
),
Block {
selectors: Selectors::default(),
Expand All @@ -332,7 +332,7 @@ mod test {
Parameters(map)
},
}
.into()
.into()
)
}
}
22 changes: 11 additions & 11 deletions css-minify/src/optimizations/merge_media.rs
Original file line number Diff line number Diff line change
Expand Up @@ -47,13 +47,13 @@ impl Transform for MergeMedia {
.filter(|e| !matches!(e, CssEntity::Media(_)))
.map(|e| match e {
CssEntity::Supports(Supports {
conditions,
entities,
}) => Supports {
conditions,
entities,
}) => Supports {
conditions,
entities: self.transform_many(entities),
}
.into(),
.into(),
entity => entity,
})
.collect(),
Expand Down Expand Up @@ -88,10 +88,10 @@ mod test {
Some(Selector::Class("test".into())),
vec![]
)]
.into(),
.into(),
parameters: Default::default()
})]
.into()
.into()
}),
CssEntity::Media(Media {
screen: Value::from("only screen and (max-width: 992px)"),
Expand All @@ -100,10 +100,10 @@ mod test {
Some(Selector::Class("test2".into())),
vec![]
)]
.into(),
.into(),
parameters: Default::default()
})]
.into()
.into()
})
])),
CssEntities(vec![CssEntity::Media(Media {
Expand All @@ -114,19 +114,19 @@ mod test {
Some(Selector::Class("test".into())),
vec![]
)]
.into(),
.into(),
parameters: Default::default()
}),
CssEntity::Block(Block {
selectors: vec![SelectorWithPseudoClasses(
Some(Selector::Class("test2".into())),
vec![]
)]
.into(),
.into(),
parameters: Default::default()
})
]
.into()
.into()
})])
)
}
Expand Down
20 changes: 10 additions & 10 deletions css-minify/src/optimizations/merge_shorthand.rs
Original file line number Diff line number Diff line change
Expand Up @@ -508,7 +508,7 @@ mod test {
Parameters(map)
},
}
.into()
.into()
),
Block {
selectors: Selectors::default(),
Expand All @@ -521,7 +521,7 @@ mod test {
Parameters(map)
},
}
.into()
.into()
)
}

Expand All @@ -540,7 +540,7 @@ mod test {
Parameters(map)
},
}
.into()
.into()
),
Block {
selectors: Selectors::default(),
Expand All @@ -553,7 +553,7 @@ mod test {
Parameters(map)
},
}
.into()
.into()
)
}

Expand All @@ -569,7 +569,7 @@ mod test {
Parameters(map)
},
}
.into()
.into()
),
Block {
selectors: Selectors::default(),
Expand All @@ -579,7 +579,7 @@ mod test {
Parameters(map)
},
}
.into()
.into()
)
}

Expand All @@ -597,7 +597,7 @@ mod test {
Parameters(map)
},
}
.into()
.into()
),
Block {
selectors: Selectors::default(),
Expand All @@ -607,7 +607,7 @@ mod test {
Parameters(map)
},
}
.into()
.into()
)
}

Expand All @@ -625,7 +625,7 @@ mod test {
Parameters(map)
},
}
.into()
.into()
),
Block {
selectors: Selectors::default(),
Expand All @@ -635,7 +635,7 @@ mod test {
Parameters(map)
},
}
.into()
.into()
)
}
}
6 changes: 3 additions & 3 deletions css-minify/src/optimizations/transformer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -56,9 +56,9 @@ pub trait Transform {
.into(),
}),
CssEntity::Supports(Supports {
conditions,
entities,
}) => CssEntity::Supports(Supports {
conditions,
entities,
}) => CssEntity::Supports(Supports {
conditions,
entities: entities
.0
Expand Down

0 comments on commit 8bb64b3

Please sign in to comment.