Skip to content

Commit

Permalink
fix!: Switch from libsass to grass
Browse files Browse the repository at this point in the history
Not perfect SASS compatibility

Fixes cobalt-org#1050
  • Loading branch information
epage committed Nov 2, 2022
1 parent 270cda1 commit 9de79be
Show file tree
Hide file tree
Showing 3 changed files with 170 additions and 50 deletions.
182 changes: 155 additions & 27 deletions Cargo.lock

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

5 changes: 2 additions & 3 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -103,9 +103,8 @@ vimwiki = { version = "0.1", features = ["html"] }
file-serve = { version = "0.2.1", path = "crates/file-serve", optional = true }
notify = { version = "4", optional = true }

sass-rs = { version = "0.2", optional = true }

html-minifier = {version="3.0", optional = true }
grass = { version = "0.11.2", default-features = false }

[dev-dependencies]
trycmd = "0.14"
Expand All @@ -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"
Expand Down
33 changes: 13 additions & 20 deletions src/cobalt_model/sass.rs
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
use std::ffi;
use std::path;

#[cfg(feature = "sass")]
use sass_rs;
use serde::{Deserialize, Serialize};

use super::files;
Expand All @@ -13,19 +11,15 @@ 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<String>,
pub import_dir: std::path::PathBuf,
pub style: SassOutputStyle,
}

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),
}
}

Expand All @@ -37,7 +31,7 @@ impl SassBuilder {

#[derive(Debug, PartialEq, Eq)]
pub struct SassCompiler {
import_dir: Option<String>,
import_dir: std::path::PathBuf,
style: SassOutputStyle,
}

Expand All @@ -60,17 +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(failure::err_msg)?;
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)
Expand Down

0 comments on commit 9de79be

Please sign in to comment.