Preserve folder structure of SCSS content

This commit is contained in:
Erich Gubler 2018-03-16 15:20:03 -06:00
parent 2eacb8247b
commit 9fbbcca3c6
6 changed files with 28 additions and 10 deletions

View file

@ -528,11 +528,16 @@ impl Site {
self.copy_static_directories() self.copy_static_directories()
} }
pub fn compile_sass(&self, base_path: &PathBuf) -> Result<()> { pub fn compile_sass(&self, base_path: &Path) -> Result<()> {
ensure_directory_exists(&self.output_path)?; ensure_directory_exists(&self.output_path)?;
let base_path = base_path.to_string_lossy().replace("\\", "/"); let sass_path = {
let sass_glob = format!("{}/{}", base_path, "sass/**/*.scss"); let mut sass_path = PathBuf::from(base_path);
sass_path.push("sass");
sass_path
};
let sass_glob = format!("{}/**/*.scss", sass_path.display());
let files = glob(&sass_glob) let files = glob(&sass_glob)
.unwrap() .unwrap()
.filter_map(|e| e.ok()) .filter_map(|e| e.ok())
@ -542,13 +547,16 @@ impl Site {
let mut sass_options = Options::default(); let mut sass_options = Options::default();
sass_options.output_style = OutputStyle::Compressed; sass_options.output_style = OutputStyle::Compressed;
for file in files { for file in files {
let name = file.as_path().file_stem().unwrap().to_string_lossy(); let css = compile_file(&file, sass_options.clone())?;
let css = match compile_file(file.as_path(), sass_options.clone()) {
Ok(c) => c,
Err(e) => bail!(e)
};
create_file(&self.output_path.join(format!("{}.css", name)), &css)?; let path_inside_sass = file.strip_prefix(&sass_path).unwrap();
let parent_inside_sass = path_inside_sass.parent();
let css_output_path = self.output_path.join(path_inside_sass).with_extension("css");
if parent_inside_sass.is_some() {
create_dir_all(&css_output_path.parent().unwrap())?;
}
create_file(&css_output_path, &css)?;
} }
Ok(()) Ok(())

View file

@ -100,7 +100,7 @@ fn can_build_site_without_live_reload() {
site.set_output_path(&public); site.set_output_path(&public);
site.build().unwrap(); site.build().unwrap();
assert!(Path::new(&public).exists()); assert!(&public.exists());
assert!(file_exists!(public, "index.html")); assert!(file_exists!(public, "index.html"));
assert!(file_exists!(public, "sitemap.xml")); assert!(file_exists!(public, "sitemap.xml"));
assert!(file_exists!(public, "robots.txt")); assert!(file_exists!(public, "robots.txt"));
@ -140,6 +140,16 @@ fn can_build_site_without_live_reload() {
assert!(file_exists!(public, "sample.css")); assert!(file_exists!(public, "sample.css"));
assert!(file_exists!(public, "some.js")); assert!(file_exists!(public, "some.js"));
// SASS and SCSS files compile correctly
assert!(file_exists!(public, "blog.css"));
assert!(file_contains!(public, "blog.css", "red"));
assert!(file_contains!(public, "blog.css", "blue"));
assert!(!file_contains!(public, "blog.css", "@import \"included\""));
assert!(file_contains!(public, "blog.css", "2rem")); // check include
assert!(!file_exists!(public, "_included.css"));
assert!(file_exists!(public, "scss.css"));
assert!(file_exists!(public, "nested_sass/scss.css"));
// no live reload code // no live reload code
assert_eq!(file_contains!(public, "index.html", "/livereload.js?port=1112&mindelay=10"), false); assert_eq!(file_contains!(public, "index.html", "/livereload.js?port=1112&mindelay=10"), false);

View file

View file

0
test_site/sass/sass.sass Normal file
View file

0
test_site/sass/scss.scss Normal file
View file