build: removed scss compiling from build and added bundling imports

This commit is contained in:
theo 2024-05-01 23:03:41 +02:00
parent ade4a86a74
commit f3fbf37e4d

View File

@ -3,30 +3,30 @@ use std::fs::{self, File};
use std::io::Write; use std::io::Write;
use std::path::Path; use std::path::Path;
use lightningcss::bundler::{Bundler, FileProvider};
use lightningcss::printer::PrinterOptions; use lightningcss::printer::PrinterOptions;
use lightningcss::stylesheet::{ParserOptions, StyleSheet}; use lightningcss::stylesheet::{MinifyOptions, ParserOptions};
fn main() -> Result<(), Box<dyn Error>> { fn main() -> Result<(), Box<dyn Error>> {
let scss_path = Path::new("styles/global.scss"); let input_path = Path::new("styles/global.css");
let css_output_path = Path::new("public/styles/global.css"); let output_path = Path::new("public/styles/global.css");
if let Some(parent) = css_output_path.parent() { if let Some(parent) = output_path.parent() {
fs::create_dir_all(parent)?; fs::create_dir_all(parent)?;
} }
let css = grass::from_path(scss_path, &grass::Options::default()) let fs = FileProvider::new();
.map_err(|e| Box::new(e) as Box<dyn Error>)?; let mut bundler = Bundler::new(&fs, None, ParserOptions::default());
let stylesheet = StyleSheet::parse(&css, ParserOptions::default()).unwrap(); let mut stylesheet = bundler.bundle(input_path).unwrap();
let minified_css = stylesheet let _ = stylesheet.minify(MinifyOptions::default());
.to_css(PrinterOptions { let minified_css = stylesheet.to_css(PrinterOptions {
minify: true, minify: true,
..PrinterOptions::default() ..PrinterOptions::default()
}) });
.unwrap();
let mut output_file = File::create(css_output_path)?; let mut output_file = File::create(output_path)?;
output_file.write_all(minified_css.code.as_bytes())?; output_file.write_all(minified_css.unwrap().code.as_bytes())?;
Ok(()) Ok(())
} }