Skip to content

[WIP] feat(styles): name compiled sass with sha #544

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 3 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 3 additions & 2 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
/target/
**/*.rs.bk
/node_modules
/static/styles/app.css
/static/styles/fonts.css
/static/styles/*.map
.sass-cache
localhost*
/static/styles/vendor.css
/static/styles/app_*.css
/static/styles/fonts_*.css
1 change: 1 addition & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ serde_yaml = "0.8.7"
sass-rs = "0.2.1"
reqwest = "0.9.5"
toml = "0.4"
siphasher = "0.2.3"

[dependencies.rocket_contrib]
version = "*"
Expand Down
27 changes: 26 additions & 1 deletion src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ extern crate rand;
extern crate reqwest;
extern crate rocket;
extern crate sass_rs;
extern crate siphasher;
extern crate toml;

extern crate rocket_contrib;
Expand All @@ -19,8 +20,11 @@ mod rust_version;
use group::*;
use production::User;

use std::collections::hash_map::DefaultHasher;
use std::collections::HashMap;
use std::fs;
use std::fs::File;
use std::hash::Hasher;
use std::io::prelude::*;
use std::path::{Path, PathBuf};

Expand Down Expand Up @@ -226,21 +230,42 @@ fn catch_error() -> Template {
not_found()
}

fn hash_css(css: &str) -> String {
let mut hasher = DefaultHasher::new();
hasher.write(css.as_bytes());
hasher.finish().to_string()
}

fn compile_sass(filename: &str) {
let scss_file = format!("./src/styles/{}.scss", filename);
let css_file = format!("./static/styles/{}.css", filename);

let css = compile_file(&scss_file, Options::default())
.expect(&format!("couldn't compile sass: {}", &scss_file));
let css_sha = format!("{}_{}", filename, hash_css(&css));
let css_file = format!("./static/styles/{}.css", css_sha);
let mut file =
File::create(&css_file).expect(&format!("couldn't make css file: {}", &css_file));
file.write_all(&css.into_bytes())
.expect(&format!("couldn't write css file: {}", &css_file));
}

fn concat_vendor_css(files: Vec<&str>) {
let mut concatted = String::new();
for filestem in files {
let vendor_path = format!("./static/styles/{}.css", filestem);
let mut file = File::open(vendor_path).expect("couldn't read vendor css");
let mut contents = String::new();
file.read_to_string(&mut contents)
.expect("couldn't read vendor css");
concatted.push_str(&contents);
}
fs::write("./static/styles/vendor.css", &concatted).expect("couldn't write vendor css");
}

fn main() {
compile_sass("app");
compile_sass("fonts");
concat_vendor_css(vec!["skeleton", "tachyons"]);

rocket::ignite()
.attach(Template::fairing())
Expand Down
Loading