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 6423313 commit 5548ed7
Show file tree
Hide file tree
Showing 11 changed files with 72 additions and 78 deletions.
7 changes: 3 additions & 4 deletions css-minifier-web/Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,15 +1,14 @@
[package]
name = "css-minifier-web"
version = "0.3.0"
version = "0.5.0"
authors = ["Mnwa <[email protected]>"]
edition = "2021"

# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html

[dependencies]
css-minify = { version = "0.4", path = "../css-minify" }
css-minify = { version = "0.5", path = "../css-minify" }
actix-web = { version = "4" }
actix-files = "0.6"
yarte = "0.15"
serde = { version = "1.0", features = ["derive"] }
parking_lot = "0.12"
serde = { version = "1.0", features = ["derive"] }
17 changes: 6 additions & 11 deletions css-minifier-web/src/main.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
use actix_web::{get, post, web, App, HttpResponse, HttpServer, Responder};
use css_minify::optimizations::{Level, Minifier};
use parking_lot::Mutex;
use serde::Deserialize;
use std::collections::hash_map::DefaultHasher;
use std::hash::{Hash, Hasher};
Expand All @@ -22,13 +21,10 @@ async fn index() -> impl Responder {
#[post("/")]
async fn minify_css(
request: web::Form<MinifyRequest>,
minifier: web::Data<Mutex<Minifier>>,
minifier: web::Data<Minifier>,
) -> impl Responder {
let level = Level::from_str(&request.level).unwrap_or(Level::One);
let output_css = match minifier.lock().minify(&request.input_css, level) {
Ok(output_css) => output_css,
Err(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 All @@ -55,10 +51,9 @@ async fn main_css(css: web::Data<MinifiedCss>) -> impl Responder {
#[actix_web::main]
async fn main() -> std::io::Result<()> {
let mut hasher = DefaultHasher::default();
let minifier = web::Data::new(Mutex::new(Minifier::default()));
let minifier = web::Data::new(Minifier::default());

let minified_css = minifier
.lock()
.minify(include_str!("../static/main.css"), Level::Three)
.expect("invalid css");

Expand All @@ -78,9 +73,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
2 changes: 1 addition & 1 deletion css-minifier/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ license = "Apache-2.0/MIT"
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html

[dependencies]
css-minify = { version = "0.4", path = "../css-minify" }
css-minify = { version = "0.5", path = "../css-minify" }
clap = { version = "^4", features = ["derive"] }
shellexpand = "3.1"
indoc = "2.0"
2 changes: 1 addition & 1 deletion css-minify/Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "css-minify"
version = "0.4.2"
version = "0.5.0"
authors = ["Mnwa <[email protected]>"]
edition = "2021"
description = "CSS parser and minificator based on nom"
Expand Down
10 changes: 5 additions & 5 deletions css-minify/src/optimizations/font.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ use crate::structure::{Parameters, Value};
pub(crate) struct FontTransformer;

impl Transform for FontTransformer {
fn transform_parameters(&mut self, mut parameters: Parameters) -> Parameters {
fn transform_parameters(&self, mut parameters: Parameters) -> Parameters {
parameters
.iter_mut()
.filter(|(name, _)| matches!(name.as_str(), "font-weight"))
Expand Down 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()
},
}),])
}), ])
)
}
}
22 changes: 11 additions & 11 deletions css-minify/src/optimizations/merge_blocks.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,15 +6,15 @@ use indexmap::map::IndexMap;
pub struct MergeBlocks;

impl Transform for MergeBlocks {
fn transform_parameters(&mut self, parameters: Parameters) -> Parameters {
fn transform_parameters(&self, parameters: Parameters) -> Parameters {
parameters
}

fn transform(&mut self, entity: CssEntity) -> CssEntity {
fn transform(&self, entity: CssEntity) -> CssEntity {
entity
}

fn transform_many(&mut self, entities: CssEntities) -> CssEntities {
fn transform_many(&self, entities: CssEntities) -> CssEntities {
let mut blocks_to_merge = IndexMap::new();
entities
.0
Expand Down 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
18 changes: 9 additions & 9 deletions css-minify/src/optimizations/merge_m_n_p.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ pub struct Margin(Option<Value>, Option<Value>, Option<Value>, Option<Value>);
pub struct Padding(Option<Value>, Option<Value>, Option<Value>, Option<Value>);

impl Transform for Merge {
fn transform_parameters(&mut self, mut parameters: Parameters) -> Parameters {
fn transform_parameters(&self, mut parameters: Parameters) -> Parameters {
let mut margin = Margin::default();
let mut padding = Padding::default();
parameters.0.iter().for_each(|(name, val)| {
Expand Down 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()
)
}
}
28 changes: 14 additions & 14 deletions css-minify/src/optimizations/merge_media.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,11 @@ use indexmap::map::IndexMap;
pub struct MergeMedia;

impl Transform for MergeMedia {
fn transform_parameters(&mut self, parameters: Parameters) -> Parameters {
fn transform_parameters(&self, parameters: Parameters) -> Parameters {
parameters
}

fn transform(&mut self, entity: CssEntity) -> CssEntity {
fn transform(&self, entity: CssEntity) -> CssEntity {
match entity {
CssEntity::Media(mut m) => {
m.screen = m.screen.replace(": ", ":");
Expand All @@ -20,7 +20,7 @@ impl Transform for MergeMedia {
}
}

fn transform_many(&mut self, entities: CssEntities) -> CssEntities {
fn transform_many(&self, entities: CssEntities) -> CssEntities {
let mut media = IndexMap::new();
entities
.0
Expand All @@ -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
Loading

0 comments on commit 5548ed7

Please sign in to comment.