Skip to content

Commit c655f83

Browse files
Nemo157jyn514
authored andcommitted
Switch from sass-rs to grass
Also simplifies the building of css: * Auto-detect files to build using standard sass convention of non-_ prefixed files * Manually concatenate vendored css (grass doesn't support importing raw css files yet)
1 parent d333838 commit c655f83

File tree

7 files changed

+157
-76
lines changed

7 files changed

+157
-76
lines changed

Cargo.lock

Lines changed: 119 additions & 27 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Cargo.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -118,10 +118,10 @@ fn-error-context = "0.2.0"
118118
[build-dependencies]
119119
time = "0.3"
120120
git2 = { version = "0.14", default-features = false }
121-
sass-rs = "0.2.2"
122121
string_cache_codegen = "0.5.1"
123122
walkdir = "2"
124123
anyhow = { version = "1.0.42", features = ["backtrace"] }
124+
grass = { version = "0.11.0", default-features = false }
125125

126126
[[bench]]
127127
name = "compression"

build.rs

Lines changed: 37 additions & 46 deletions
Original file line numberDiff line numberDiff line change
@@ -47,58 +47,49 @@ fn get_git_hash() -> Result<Option<String>> {
4747
}
4848
}
4949

50-
fn compile_sass_file(
51-
out_dir: &Path,
52-
name: &str,
53-
target: &str,
54-
include_paths: &[String],
55-
) -> Result<()> {
56-
use sass_rs::{Context, Options, OutputStyle};
57-
58-
const STYLE_DIR: &str = concat!(env!("CARGO_MANIFEST_DIR"), "/templates/style");
59-
60-
let include_paths = {
61-
let mut paths = vec![STYLE_DIR.to_owned()];
62-
paths.extend_from_slice(include_paths);
63-
paths
64-
};
65-
66-
for path in &include_paths {
67-
for entry in walkdir::WalkDir::new(path) {
68-
println!("cargo:rerun-if-changed={}", entry?.path().display());
69-
}
70-
}
50+
fn compile_sass_file(src: &Path, dest: &Path) -> Result<()> {
51+
let css = grass::from_path(
52+
src.to_str()
53+
.context("source file path must be a utf-8 string")?,
54+
&grass::Options::default().style(grass::OutputStyle::Compressed),
55+
)
56+
.map_err(|e| Error::msg(e.to_string()))?;
7157

72-
let mut context =
73-
Context::new_file(format!("{}/{}.scss", STYLE_DIR, name)).map_err(Error::msg)?;
74-
context.set_options(Options {
75-
output_style: OutputStyle::Compressed,
76-
include_paths,
77-
..Default::default()
78-
});
79-
80-
let css = context.compile().map_err(Error::msg)?;
81-
let dest_path = out_dir.join(format!("{}.css", target));
82-
std::fs::write(dest_path, css)?;
58+
std::fs::write(dest, css)?;
8359

8460
Ok(())
8561
}
8662

8763
fn compile_sass(out_dir: &Path) -> Result<()> {
88-
// Compile base.scss -> style.css
89-
compile_sass_file(out_dir, "base", "style", &[])?;
90-
91-
// Compile rustdoc.scss -> rustdoc.css
92-
compile_sass_file(out_dir, "rustdoc", "rustdoc", &[])?;
93-
compile_sass_file(out_dir, "rustdoc-2021-12-05", "rustdoc-2021-12-05", &[])?;
94-
95-
// Compile vendored.scss -> vendored.css
96-
compile_sass_file(
97-
out_dir,
98-
"vendored",
99-
"vendored",
100-
&[concat!(env!("CARGO_MANIFEST_DIR"), "/vendor/pure-css/css").to_owned()],
101-
)?;
64+
const STYLE_DIR: &str = "templates/style";
65+
66+
for entry in walkdir::WalkDir::new(STYLE_DIR) {
67+
let entry = entry?;
68+
println!(
69+
"cargo:rerun-if-changed={}",
70+
entry
71+
.path()
72+
.to_str()
73+
.with_context(|| format!("{} is a non-utf-8 path", entry.path().display()))?
74+
);
75+
let file_name = entry.file_name().to_str().unwrap();
76+
if entry.metadata()?.is_file() && !file_name.starts_with('_') {
77+
let dest = out_dir
78+
.join(entry.path().strip_prefix(STYLE_DIR)?)
79+
.with_extension("css");
80+
compile_sass_file(entry.path(), &dest).with_context(|| {
81+
format!("compiling {} to {}", entry.path().display(), dest.display())
82+
})?;
83+
}
84+
}
85+
86+
// Compile vendored.css
87+
println!("cargo:rerun-if-changed=vendor/pure-css/css/pure-min.css");
88+
let pure = std::fs::read_to_string("vendor/pure-css/css/pure-min.css")?;
89+
println!("cargo:rerun-if-changed=vendor/pure-css/css/grids-responsive-min.css");
90+
let grids = std::fs::read_to_string("vendor/pure-css/css/grids-responsive-min.css")?;
91+
let vendored = pure + &grids;
92+
std::fs::write(out_dir.join("vendored").with_extension("css"), vendored)?;
10293

10394
Ok(())
10495
}
File renamed without changes.
File renamed without changes.

templates/style/vendored.scss

Lines changed: 0 additions & 2 deletions
This file was deleted.

0 commit comments

Comments
 (0)