From 8ea42f7b4598b8d67c9c27948ffb174fddb1bd9c Mon Sep 17 00:00:00 2001 From: Ed Page Date: Tue, 1 Nov 2022 21:33:36 -0500 Subject: [PATCH] fix!: Switch from libsass to grass Not perfect SASS compatibility Fixes #1050 --- Cargo.lock | 188 +++++++++++++++++++++++++++++++++------ Cargo.toml | 5 +- src/cobalt_model/sass.rs | 34 +++---- 3 files changed, 176 insertions(+), 51 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index 9019dff2..93379ac1 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -17,6 +17,17 @@ version = "1.0.2" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "f26201604c87b1e01bd3d98f8d5d9a8fcbb815e8cedb41ffccbeb4bf593a35fe" +[[package]] +name = "ahash" +version = "0.7.6" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "fcb51a0695d8f838b1ee009b3fbf66bda078cd64590202a864a8f3e8c4315c47" +dependencies = [ + "getrandom", + "once_cell", + "version_check", +] + [[package]] name = "aho-corasick" version = "0.7.20" @@ -151,6 +162,12 @@ dependencies = [ "rustc-demangle", ] +[[package]] +name = "beef" +version = "0.5.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "3a8241f3ebb85c056b509d4327ad0358fbbba6ffb340bf388f26350aeda225b1" + [[package]] name = "bincode" version = "1.3.3" @@ -319,6 +336,7 @@ dependencies = [ "engarde", "env_logger", "file-serve", + "grass", "html-minifier", "human-panic", "ignore", @@ -337,7 +355,6 @@ dependencies = [ "regex", "relative-path", "rss", - "sass-rs", "scrawl", "serde", "serde_json", @@ -384,6 +401,12 @@ dependencies = [ "walkdir", ] +[[package]] +name = "codemap" +version = "0.1.3" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "b9e769b5c8c8283982a987c6e948e540254f1058d5a74b8794914d4ef5fc2a24" + [[package]] name = "colorchoice" version = "1.0.0" @@ -805,6 +828,32 @@ dependencies = [ "regex", ] +[[package]] +name = "grass" +version = "0.11.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "bc5bedc3dbd71dcdd41900e1f58e4d431fa69dd67c04ae1f86ae1a0339edd849" +dependencies = [ + "beef", + "codemap", + "indexmap", + "lasso", + "num-bigint", + "num-rational", + "num-traits", + "once_cell", + "phf", +] + +[[package]] +name = "hashbrown" +version = "0.11.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "ab5ef0d4909ef3724cc8cce6ccc8572c5c817592e9285f5464f8e86f8bd3726e" +dependencies = [ + "ahash", +] + [[package]] name = "hashbrown" version = "0.12.3" @@ -954,7 +1003,7 @@ source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "bd070e393353796e801d209ad339e89596eb4c8d430d18ede6a1cced8fafbd99" dependencies = [ "autocfg", - "hashbrown", + "hashbrown 0.12.3", ] [[package]] @@ -1094,6 +1143,15 @@ dependencies = [ "static_assertions", ] +[[package]] +name = "lasso" +version = "0.5.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "e8647c8a01e5f7878eacb2c323c4c949fdb63773110f0686c7810769874b7e0a" +dependencies = [ + "hashbrown 0.11.2", +] + [[package]] name = "lazy_static" version = "1.4.0" @@ -1308,6 +1366,18 @@ dependencies = [ "num-traits", ] +[[package]] +name = "num-rational" +version = "0.4.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "0638a1c9d0a3c0914158145bc76cff373a75a627e6ecbfb71cbe6f453a5a19b0" +dependencies = [ + "autocfg", + "num-bigint", + "num-integer", + "num-traits", +] + [[package]] name = "num-traits" version = "0.2.15" @@ -1430,10 +1500,54 @@ dependencies = [ ] [[package]] -name = "pkg-config" -version = "0.3.27" +name = "phf" +version = "0.9.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "b2ac8b67553a7ca9457ce0e526948cad581819238f4a9d1ea74545851fa24f37" +dependencies = [ + "phf_macros", + "phf_shared", + "proc-macro-hack", +] + +[[package]] +name = "phf_generator" +version = "0.9.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "d43f3220d96e0080cc9ea234978ccd80d904eafb17be31bb0f76daaea6493082" +dependencies = [ + "phf_shared", + "rand", +] + +[[package]] +name = "phf_macros" +version = "0.9.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "b706f5936eb50ed880ae3009395b43ed19db5bff2ebd459c95e7bf013a89ab86" +dependencies = [ + "phf_generator", + "phf_shared", + "proc-macro-hack", + "proc-macro2", + "quote", + "syn 1.0.109", +] + +[[package]] +name = "phf_shared" +version = "0.9.0" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "26072860ba924cbfa98ea39c8c19b4dd6a4a25423dbdf219c1eca91aa0cf6964" +checksum = "a68318426de33640f02be62b4ae8eb1261be2efbc337b60c54d845bf4484e0d9" +dependencies = [ + "siphasher", +] + +[[package]] +name = "ppv-lite86" +version = "0.2.17" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "5b40af805b3121feab8a3c29f04d8ad262fa8e0561883e7653e024ae4479e6de" [[package]] name = "proc-exit" @@ -1441,6 +1555,12 @@ version = "2.0.1" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "dbd05acff31c8b626109e8e1db042b9bddb2b3ffba01ead93d15b78517a282ca" +[[package]] +name = "proc-macro-hack" +version = "0.5.20+deprecated" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "dc375e1527247fe1a97d8b7156678dfe7c1af2fc075c9a4db3690ecd2a148068" + [[package]] name = "proc-macro2" version = "1.0.59" @@ -1480,6 +1600,36 @@ dependencies = [ "proc-macro2", ] +[[package]] +name = "rand" +version = "0.8.5" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "34af8d1a0e25924bc5b7c43c079c942339d8f0a8b57c39049bef581b46327404" +dependencies = [ + "libc", + "rand_chacha", + "rand_core", +] + +[[package]] +name = "rand_chacha" +version = "0.3.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "e6c10a63a0fa32252be49d21e7709d4d4baf8d231c2dbce1eaa8141b9b127d88" +dependencies = [ + "ppv-lite86", + "rand_core", +] + +[[package]] +name = "rand_core" +version = "0.6.4" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "ec0be4795e2f6a28069bec0b5ff3e2ac9bafc99e6a9a7dc3547996c5c816922c" +dependencies = [ + "getrandom", +] + [[package]] name = "rayon" version = "1.7.0" @@ -1599,28 +1749,6 @@ dependencies = [ "winapi-util", ] -[[package]] -name = "sass-rs" -version = "0.2.2" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "cabcf7c6e55053f359911187ac401409aad2dc14338cae972dec266fee486abd" -dependencies = [ - "libc", - "sass-sys", -] - -[[package]] -name = "sass-sys" -version = "0.4.22" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "933bca23b402377f0ab71e79732a826deffc748013746ac3314f6abc7f9fc51c" -dependencies = [ - "cc", - "libc", - "num_cpus", - "pkg-config", -] - [[package]] name = "scopeguard" version = "1.1.0" @@ -1709,6 +1837,12 @@ version = "2.2.1" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "420acb44afdae038210c99e69aae24109f32f15500aa708e81d46c9f29d55fcf" +[[package]] +name = "siphasher" +version = "0.3.10" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "7bd3e3206899af3f8b12af284fafc038cc1dc2b41d1b89dd17297221c5d225de" + [[package]] name = "sitemap" version = "0.4.1" diff --git a/Cargo.toml b/Cargo.toml index 42b04306..ab1dce00 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -100,12 +100,11 @@ dunce = "1.0.4" file-serve = { version = "0.2.3", path = "crates/file-serve", optional = true } notify = { version = "6", optional = true, default-features = false, features = ["macos_fsevent"] } -sass-rs = { version = "0.2", optional = true } - html-minifier = {version="3.0", optional = true } anyhow = "1.0.71" anstream = "0.3.2" chrono = "0.4.26" +grass = { version = "0.11.2", default-features = false } [dev-dependencies] trycmd = "0.14" @@ -118,7 +117,7 @@ preview_unstable = ["cobalt-config/preview_unstable"] serve = ["file-serve", "notify"] syntax-highlight = ["engarde/syntax"] -sass = ['sass-rs'] +sass = [] [profile.release] lto = "thin" diff --git a/src/cobalt_model/sass.rs b/src/cobalt_model/sass.rs index 5f5e9995..fbdfb1c2 100644 --- a/src/cobalt_model/sass.rs +++ b/src/cobalt_model/sass.rs @@ -1,8 +1,6 @@ use std::ffi; use std::path; -#[cfg(feature = "sass")] -use sass_rs; use serde::{Deserialize, Serialize}; use super::files; @@ -13,7 +11,7 @@ pub use cobalt_config::SassOutputStyle; #[derive(Debug, Clone, PartialEq, Eq, Default, Serialize, Deserialize)] #[serde(deny_unknown_fields, default)] pub struct SassBuilder { - pub import_dir: Option, + pub import_dir: std::path::PathBuf, pub style: SassOutputStyle, } @@ -21,11 +19,7 @@ impl SassBuilder { pub fn from_config(config: cobalt_config::Sass, source: &path::Path) -> Self { Self { style: config.style, - import_dir: source - .join(config.import_dir) - .into_os_string() - .into_string() - .ok(), + import_dir: source.join(config.import_dir), } } @@ -37,7 +31,7 @@ impl SassBuilder { #[derive(Debug, PartialEq, Eq)] pub struct SassCompiler { - import_dir: Option, + import_dir: std::path::PathBuf, style: SassOutputStyle, } @@ -60,18 +54,16 @@ impl SassCompiler { file_path: &path::Path, minify: &Minify, ) -> Result<()> { - let sass_opts = sass_rs::Options { - include_paths: self.import_dir.iter().cloned().collect(), - output_style: match self.style { - SassOutputStyle::Nested => sass_rs::OutputStyle::Nested, - SassOutputStyle::Expanded => sass_rs::OutputStyle::Expanded, - SassOutputStyle::Compact => sass_rs::OutputStyle::Compact, - SassOutputStyle::Compressed => sass_rs::OutputStyle::Compressed, - }, - ..Default::default() - }; - let content = - sass_rs::compile_file(file_path, sass_opts).map_err(|e| anyhow::format_err!("{e}"))?; + let sass_opts = grass::Options::default() + .style(match self.style { + SassOutputStyle::Nested | SassOutputStyle::Expanded => grass::OutputStyle::Expanded, + SassOutputStyle::Compact | SassOutputStyle::Compressed => { + grass::OutputStyle::Compressed + } + }) + .load_path(&self.import_dir); + let raw = std::fs::read_to_string(file_path)?; + let content = grass::from_string(raw, &sass_opts)?; let rel_src = file_path .strip_prefix(source)