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