From 9fbbcca3c63ef674fd24ed0b3cb066d056bf3adb Mon Sep 17 00:00:00 2001 From: Erich Gubler Date: Fri, 16 Mar 2018 15:20:03 -0600 Subject: [PATCH] Preserve folder structure of SCSS content --- components/site/src/lib.rs | 26 +++++++++++++++++--------- components/site/tests/site.rs | 12 +++++++++++- test_site/sass/nested_sass/sass.sass | 0 test_site/sass/nested_sass/scss.scss | 0 test_site/sass/sass.sass | 0 test_site/sass/scss.scss | 0 6 files changed, 28 insertions(+), 10 deletions(-) create mode 100644 test_site/sass/nested_sass/sass.sass create mode 100644 test_site/sass/nested_sass/scss.scss create mode 100644 test_site/sass/sass.sass create mode 100644 test_site/sass/scss.scss diff --git a/components/site/src/lib.rs b/components/site/src/lib.rs index 661adb8c..eaeabc59 100644 --- a/components/site/src/lib.rs +++ b/components/site/src/lib.rs @@ -528,11 +528,16 @@ impl Site { 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)?; - let base_path = base_path.to_string_lossy().replace("\\", "/"); - let sass_glob = format!("{}/{}", base_path, "sass/**/*.scss"); + let sass_path = { + 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) .unwrap() .filter_map(|e| e.ok()) @@ -542,13 +547,16 @@ impl Site { let mut sass_options = Options::default(); sass_options.output_style = OutputStyle::Compressed; for file in files { - let name = file.as_path().file_stem().unwrap().to_string_lossy(); - let css = match compile_file(file.as_path(), sass_options.clone()) { - Ok(c) => c, - Err(e) => bail!(e) - }; + let css = compile_file(&file, sass_options.clone())?; - 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(()) diff --git a/components/site/tests/site.rs b/components/site/tests/site.rs index efef8567..9470b116 100644 --- a/components/site/tests/site.rs +++ b/components/site/tests/site.rs @@ -100,7 +100,7 @@ fn can_build_site_without_live_reload() { site.set_output_path(&public); site.build().unwrap(); - assert!(Path::new(&public).exists()); + assert!(&public.exists()); assert!(file_exists!(public, "index.html")); assert!(file_exists!(public, "sitemap.xml")); 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, "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 assert_eq!(file_contains!(public, "index.html", "/livereload.js?port=1112&mindelay=10"), false); diff --git a/test_site/sass/nested_sass/sass.sass b/test_site/sass/nested_sass/sass.sass new file mode 100644 index 00000000..e69de29b diff --git a/test_site/sass/nested_sass/scss.scss b/test_site/sass/nested_sass/scss.scss new file mode 100644 index 00000000..e69de29b diff --git a/test_site/sass/sass.sass b/test_site/sass/sass.sass new file mode 100644 index 00000000..e69de29b diff --git a/test_site/sass/scss.scss b/test_site/sass/scss.scss new file mode 100644 index 00000000..e69de29b