From a646e5e4068d2976f56cd7ec67751680d5441f50 Mon Sep 17 00:00:00 2001 From: Ashley Williams Date: Tue, 4 Dec 2018 16:41:48 -0500 Subject: [PATCH] feat(styles): name compiled sass with sha --- .gitignore | 4 ++-- Cargo.lock | 1 + Cargo.toml | 1 + src/main.rs | 12 +++++++++++- 4 files changed, 15 insertions(+), 3 deletions(-) diff --git a/.gitignore b/.gitignore index db7ed209a..574a2597f 100644 --- a/.gitignore +++ b/.gitignore @@ -1,9 +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 diff --git a/Cargo.lock b/Cargo.lock index 2b17b3b8f..14a8d791d 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -1500,6 +1500,7 @@ dependencies = [ "serde 1.0.80 (registry+https://github.com/rust-lang/crates.io-index)", "serde_derive 1.0.80 (registry+https://github.com/rust-lang/crates.io-index)", "serde_yaml 0.8.7 (registry+https://github.com/rust-lang/crates.io-index)", + "siphasher 0.2.3 (registry+https://github.com/rust-lang/crates.io-index)", "toml 0.4.8 (registry+https://github.com/rust-lang/crates.io-index)", ] diff --git a/Cargo.toml b/Cargo.toml index 95b33eea2..a5cb29c30 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -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 = "*" diff --git a/src/main.rs b/src/main.rs index 50ce95ae7..a708a0436 100644 --- a/src/main.rs +++ b/src/main.rs @@ -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; @@ -19,9 +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}; @@ -227,12 +230,19 @@ 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())