From c35673df849f9befc02509a886b621692187ac5b 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 | 288 +++++++++++++++++++++++++++++++-------- Cargo.toml | 5 +- src/cobalt_model/sass.rs | 34 ++--- 3 files changed, 248 insertions(+), 79 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index d14a2b6f..7e3643da 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.8" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "891477e0c6a8957309ee5c45a6368af3ae14bb510732d2684ffa19af310920f9" +dependencies = [ + "getrandom", + "once_cell", + "version_check", +] + [[package]] name = "aho-corasick" version = "1.1.3" @@ -135,7 +146,7 @@ checksum = "edf3ee19dbc0a46d740f6f0926bde8c50f02bdbc7b536842da28f6ac56513a8b" dependencies = [ "proc-macro2", "quote", - "syn", + "syn 2.0.71", ] [[package]] @@ -153,6 +164,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" @@ -214,6 +231,12 @@ version = "3.16.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "79296716171880943b8470b5f8d03aa55eb2e645a4874bdbb28adb49162e012c" +[[package]] +name = "byteorder" +version = "1.5.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "1fd0f2584146f6f2ef48085050886acf353beff7305ebd1ae69500e27c67f64b" + [[package]] name = "cc" version = "1.1.1" @@ -297,7 +320,7 @@ dependencies = [ "heck", "proc-macro2", "quote", - "syn", + "syn 2.0.71", ] [[package]] @@ -325,6 +348,7 @@ dependencies = [ "engarde", "env_logger", "file-serve", + "grass", "html-minifier", "human-panic", "ignore", @@ -343,7 +367,6 @@ dependencies = [ "regex", "relative-path", "rss", - "sass-rs", "scrawl", "serde", "serde_json", @@ -389,6 +412,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.2" @@ -500,7 +529,7 @@ dependencies = [ "proc-macro2", "quote", "strsim", - "syn", + "syn 2.0.71", ] [[package]] @@ -511,7 +540,7 @@ checksum = "d336a2a514f6ccccaa3e09b02d41d35330c07ddf03a62165fcec10bb561c7806" dependencies = [ "darling_core", "quote", - "syn", + "syn 2.0.71", ] [[package]] @@ -541,7 +570,7 @@ dependencies = [ "darling", "proc-macro2", "quote", - "syn", + "syn 2.0.71", ] [[package]] @@ -551,7 +580,7 @@ source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "206868b8242f27cecce124c19fd88157fbd0dd334df2587f36417bafbc85097b" dependencies = [ "derive_builder_core", - "syn", + "syn 2.0.71", ] [[package]] @@ -600,7 +629,7 @@ dependencies = [ "enum-ordinalize", "proc-macro2", "quote", - "syn", + "syn 2.0.71", ] [[package]] @@ -643,7 +672,7 @@ checksum = "0d28318a75d4aead5c4db25382e8ef717932d0346600cacae6357eb5941bc5ff" dependencies = [ "proc-macro2", "quote", - "syn", + "syn 2.0.71", ] [[package]] @@ -812,6 +841,33 @@ dependencies = [ "regex-syntax", ] +[[package]] +name = "grass" +version = "0.11.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "bc5bedc3dbd71dcdd41900e1f58e4d431fa69dd67c04ae1f86ae1a0339edd849" +dependencies = [ + "beef", + "codemap", + "indexmap 1.9.3", + "lasso", + "num-bigint", + "num-rational", + "num-traits", + "once_cell", + "phf", + "rand", +] + +[[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" @@ -830,12 +886,6 @@ version = "0.5.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "2304e00983f87ffb38b55b444b5e3b60a884b5d30c0fca7d82fe33449bbe55ea" -[[package]] -name = "hermit-abi" -version = "0.3.9" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "d231dfb89cfffdbc30e7fc41579ed6066ad03abda9e567ccafae602b97ec5024" - [[package]] name = "html-escape" version = "0.2.13" @@ -1081,6 +1131,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.5.0" @@ -1144,7 +1203,7 @@ checksum = "77a5aa659a76b649f0d639ef0b9c067a9499c42a9d7f3e7832e279f791704966" dependencies = [ "proc-macro2", "quote", - "syn", + "syn 2.0.71", ] [[package]] @@ -1257,6 +1316,16 @@ dependencies = [ "windows-sys 0.48.0", ] +[[package]] +name = "num-bigint" +version = "0.4.6" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "a5e44f723f1133c9deac646763579fdb3ac745e418f2a7af9cd0c431da1f20b9" +dependencies = [ + "num-integer", + "num-traits", +] + [[package]] name = "num-conv" version = "0.1.0" @@ -1264,22 +1333,32 @@ source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "51d515d32fb182ee37cda2ccdcb92950d6a3c2893aa280e540671c2cd0f3b1d9" [[package]] -name = "num-traits" -version = "0.2.19" +name = "num-integer" +version = "0.1.46" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "071dfc062690e90b734c0b2273ce72ad0ffa95f0c74596bc250dcfd960262841" +checksum = "7969661fd2958a5cb096e56c8e1ad0444ac2bbcd0061bd28660485a44879858f" dependencies = [ - "autocfg", + "num-traits", ] [[package]] -name = "num_cpus" -version = "1.16.0" +name = "num-rational" +version = "0.4.2" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "4161fcb6d602d4d2081af7c3a45852d875a03dd337a6bfdd6e06407b61342a43" +checksum = "f83d14da390562dca69fc84082e73e548e1ad308d24accdedd2720017cb37824" dependencies = [ - "hermit-abi", - "libc", + "num-bigint", + "num-integer", + "num-traits", +] + +[[package]] +name = "num-traits" +version = "0.2.19" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "071dfc062690e90b734c0b2273ce72ad0ffa95f0c74596bc250dcfd960262841" +dependencies = [ + "autocfg", ] [[package]] @@ -1372,7 +1451,7 @@ dependencies = [ "pest_meta", "proc-macro2", "quote", - "syn", + "syn 2.0.71", ] [[package]] @@ -1387,10 +1466,48 @@ dependencies = [ ] [[package]] -name = "pkg-config" -version = "0.3.30" +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 = "d231b230927b5e4ad203db57bbcbee2802f6bce620b1e4a9024a07d94e2907ec" +checksum = "a68318426de33640f02be62b4ae8eb1261be2efbc337b60c54d845bf4484e0d9" +dependencies = [ + "siphasher", +] [[package]] name = "powerfmt" @@ -1398,12 +1515,27 @@ version = "0.2.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "439ee305def115ba05938db6eb1644ff94165c5ab5e9420d1c1bcedbba909391" +[[package]] +name = "ppv-lite86" +version = "0.2.20" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "77957b295656769bb8ad2b6a6b09d897d94f05c41b069aede1fcdaa675eaea04" +dependencies = [ + "zerocopy", +] + [[package]] name = "proc-exit" version = "2.0.2" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "77a5390699eb9ac50677729fda96fb8339d4629f257cc6cfa6eaa673730f8f63" +[[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.86" @@ -1450,6 +1582,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.10.0" @@ -1563,28 +1725,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 = "scrawl" version = "2.0.0" @@ -1608,7 +1748,7 @@ checksum = "e0cd7e117be63d3c3678776753929474f3b04a43a080c744d6b0ae2a8c28e222" dependencies = [ "proc-macro2", "quote", - "syn", + "syn 2.0.71", ] [[package]] @@ -1668,6 +1808,12 @@ version = "2.5.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "fa42c91313f1d05da9b26f267f931cf178d4aba455b4c4622dd7355eb80c6640" +[[package]] +name = "siphasher" +version = "0.3.11" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "38b58827f4464d87d377d175e90bf58eb00fd8716ff0a62f80356b5e61555d0d" + [[package]] name = "sitemap" version = "0.4.1" @@ -1732,6 +1878,17 @@ version = "0.11.1" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "7da8b5736845d9f2fcb837ea5d9e2628564b3b043a70948a3f0b778838c5fb4f" +[[package]] +name = "syn" +version = "1.0.109" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "72b64191b275b66ffe2469e8af2c1cfe3bafa67b529ead792a6d0160888b4237" +dependencies = [ + "proc-macro2", + "quote", + "unicode-ident", +] + [[package]] name = "syn" version = "2.0.71" @@ -1803,7 +1960,7 @@ checksum = "a4558b58466b9ad7ca0f102865eccc95938dca1a74a856f2b57b6629050da261" dependencies = [ "proc-macro2", "quote", - "syn", + "syn 2.0.71", ] [[package]] @@ -2053,7 +2210,7 @@ dependencies = [ "once_cell", "proc-macro2", "quote", - "syn", + "syn 2.0.71", "wasm-bindgen-shared", ] @@ -2075,7 +2232,7 @@ checksum = "e94f17b526d0a461a191c78ea52bbce64071ed5c04c9ffe424dcb38f74171bb7" dependencies = [ "proc-macro2", "quote", - "syn", + "syn 2.0.71", "wasm-bindgen-backend", "wasm-bindgen-shared", ] @@ -2266,3 +2423,24 @@ checksum = "56c1936c4cc7a1c9ab21a1ebb602eb942ba868cbd44a99cb7cdc5892335e1c85" dependencies = [ "linked-hash-map", ] + +[[package]] +name = "zerocopy" +version = "0.7.35" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "1b9b4fd18abc82b8136838da5d50bae7bdea537c574d8dc1a34ed098d6c166f0" +dependencies = [ + "byteorder", + "zerocopy-derive", +] + +[[package]] +name = "zerocopy-derive" +version = "0.7.35" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "fa4f8080344d4671fb4e831a13ad1e68092748387dfc4f55e356242fae12ce3e" +dependencies = [ + "proc-macro2", + "quote", + "syn 2.0.71", +] diff --git a/Cargo.toml b/Cargo.toml index 01404b9f..1e59e4ce 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -172,13 +172,12 @@ dunce = "1.0.4" file-serve = { version = "0.3.5", 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.82" anstream = "0.6.13" chrono = "0.4.38" anstyle = "1.0.7" +grass = { version = "0.11.2", features = ["random"], default-features = false, optional = true } [dev-dependencies] trycmd = "0.15" @@ -191,7 +190,7 @@ preview_unstable = ["cobalt-config/preview_unstable"] serve = ["file-serve", "notify"] syntax-highlight = ["engarde/syntax"] -sass = ['sass-rs'] +sass = ["dep:grass"] [profile.dev] panic = "abort" diff --git a/src/cobalt_model/sass.rs b/src/cobalt_model/sass.rs index 0603a474..960e3525 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(crate) 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: 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: 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)