Skip to content

Commit

Permalink
Build css on compile time
Browse files Browse the repository at this point in the history
  • Loading branch information
Mnwa committed Aug 17, 2024
1 parent 1a12a33 commit 59be2ba
Show file tree
Hide file tree
Showing 4 changed files with 30 additions and 11 deletions.
6 changes: 5 additions & 1 deletion css-minifier-web/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -11,4 +11,8 @@ 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"] }
serde = { version = "1.0", features = ["derive"] }

[build-dependencies]
build_const = "0.2.2"
css-minify = { version = "0.5", path = "../css-minify" }
16 changes: 16 additions & 0 deletions css-minifier-web/build.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
use std::env::current_dir;
use std::fs::File;
use std::io::Read;
use std::path::Path;
use build_const::{src_file, ConstWriter};
use css_minify::optimizations::{Level, Minifier};

fn main() {
let main = src_file!("styles.rs");
let consts = ConstWriter::from_path(Path::new(main)).unwrap();
let mut consts = consts.finish_dependencies();
let mut file = File::open(current_dir().unwrap().join("static/main.css")).unwrap();
let mut styles = String::new();
file.read_to_string(&mut styles).unwrap();
consts.add_value("STYLES", "&str", Minifier::default().minify(styles.as_str(), Level::Three).unwrap());
}
18 changes: 8 additions & 10 deletions css-minifier-web/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@ use std::hash::{Hash, Hasher};
use std::str::FromStr;
use yarte::Template;

mod styles;

#[get("/")]
async fn index() -> impl Responder {
match Template::call(&IndexTemplate::default()) {
Expand Down Expand Up @@ -55,14 +57,10 @@ async fn main() -> std::io::Result<()> {
let mut hasher = DefaultHasher::default();
let minifier = web::Data::new(Minifier::default());

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

minified_css.hash(&mut hasher);
styles::STYLES.hash(&mut hasher);

let output_css = web::Data::new(MinifiedCss {
css: minified_css,
css: styles::STYLES,
hash: hasher.finish().to_string(),
});

Expand All @@ -75,13 +73,13 @@ 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 {
css: String,
css: &'static str,
hash: String,
}

Expand Down
1 change: 1 addition & 0 deletions css-minifier-web/src/styles.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
pub const STYLES: &str = "html{line-height:1.15;-webkit-text-size-adjust:100%}body{margin:0;font-family:'Oswald',sans-serif}main{display:block}h1{font-size:2em;margin:.67em 0}hr{box-sizing:content-box;height:0;overflow:visible}pre{font:1em monospace,monospace}a{background:transparent}abbr[title]{border-bottom:none;text-decoration:underline dotted}b,strong{font-weight:700er}code,kbd,samp{font:1em monospace,monospace}small{font-size:80%}sub,sup{font-size:75%;line-height:0;position:relative;vertical-align:baseline}sub{bottom:-0.25em}sup{top:-0.5em}img{border:none}button,input,optgroup,select,textarea{margin:0;font:100%/1.15 inherit}button,input{overflow:visible}button,select{text-transform:none}[type=\"button\"],[type=\"reset\"],[type=\"submit\"],button{-webkit-appearance:button}[type=\"button\"]::-moz-focus-inner,[type=\"reset\"]::-moz-focus-inner,[type=\"submit\"]::-moz-focus-inner ,button::-moz-focus-inner{border:none;padding:0}[type=\"button\"]:-moz-focusring,[type=\"reset\"]:-moz-focusring,[type=\"submit\"]:-moz-focusring ,button:-moz-focusring{outline:1px dotted ButtonText}fieldset{padding:.35em .75em .625em}legend{box-sizing:border-box;color:inherit;display:table;max-width:100%;padding:0;white-space:normal}progress{vertical-align:baseline}textarea{overflow:auto}[type=\"checkbox\"],[type=\"radio\"]{box-sizing:border-box;padding:0}[type=\"number\"]::-webkit-inner-spin-button,[type=\"number\"]::-webkit-outer-spin-button {height:auto}[type=\"search\"]{-webkit-appearance:textfield;outline-offset:-2px}[type=\"search\"]::-webkit-search-decoration {-webkit-appearance:none}::-webkit-file-upload-button {-webkit-appearance:button;font:inherit}details{display:block}summary{display:list-item}template{display:none}[hidden]{display:none}.header{display:flex;justify-content:space-around;align-items:center;padding:1rem 0;background:#bdbdbd}.nav{display:flex}.brand{margin-right:1rem;font-size:20px;font-weight:700;text-decoration:none;color:#000}.main{background:#efefef}.container{width:100%;margin:0 auto;padding:20px 0}.heading{padding:1rem 0;margin:0}.columns{display:grid;grid-template-columns:1fr}.column{padding:10px 20px}.textarea{width:100%;border:none;min-width:50%;font:14px/16px 'Oswald',monospace;box-sizing:border-box;padding:16px}textarea:focus {outline:none;box-shadow:0 13px 38px 0 #212121}textarea:disabled {border:1px solid #212121;border-radius:10px}.code-block{background:#212121;color:#fafafa;padding:20px;max-width:100%;white-space:pre-wrap}.footer{padding:10px;display:flex;justify-content:center;align-items:center;background:#8c8c8c}.link{color:black;text-decoration:none}.submit{background:#212121;color:#fafafa;border:none;padding:15px 32px;text-align:center;text-decoration:none;font-size:16px;cursor:pointer;margin:10px 0}.options{display:flex;justify-content:space-between;align-items:flex-start;flex-direction:column}@media (min-width:576px){.container{max-width:540px}}@media (min-width:768px){.container{max-width:720px}.options{flex-direction:row;align-items:center}.submit{margin:0}}@media (min-width:992px){.container{max-width:960px}.columns{grid-template-columns:1fr 1fr}}@media (min-width:1200px){.container{max-width:1200px}}";

0 comments on commit 59be2ba

Please sign in to comment.