fix: apply lightningcss to bundle once again. Seems the right thing to do

This commit is contained in:
Yiffy Dev 2024-11-02 09:15:11 +05:00
commit d59729e6fe
No known key found for this signature in database
GPG key ID: FB83DFEAFB3F4709
2 changed files with 57 additions and 41 deletions

View file

@ -145,10 +145,14 @@ impl SabryBuilder {
.append(true)
.open(path)?;
// merged bundle CSS does require another lightningcss pass
let mut buffer = String::new();
for (_, code) in &self.state.loaded_css_modules {
write!(file, " {}", self.state.css_prelude)?;
write!(file, " {code}")?;
buffer.push_str(code);
}
buffer = self.css_compiler.lightningcss(&buffer)?;
write!(file, "{buffer}")?;
}
Ok(())

View file

@ -19,6 +19,8 @@ impl CompilerAdapter {
Self { config }
}
/// Compile given SASS/SCSS into CSS
/// with respect to self.config
pub fn compile_module(
&self,
syntax: OneSyntaxToRuleThemAll,
@ -28,10 +30,19 @@ impl CompilerAdapter {
let mut css = grass::from_string(code, &options)?;
if self.config.css.minify {
//its lightningcss time!
css = match self.lightningcss(&css) {
Ok(c) => c,
Err(e) => return Err(e),
};
Ok(css)
}
/// Perform lightningcss transformations on given css
/// with respect to self.config.css.minify on minification
pub fn lightningcss(&self, css: &str) -> Result<String, SabryCompilerError> {
let mut lightsheet = match lightningcss::stylesheet::StyleSheet::parse(
&css,
css,
lightningcss::stylesheet::ParserOptions::default(),
) {
Ok(s) => s,
@ -59,16 +70,17 @@ impl CompilerAdapter {
..Default::default()
};
lightsheet.minify(minify_options)?;
if self.config.css.minify {
lightsheet.minify(minify_options)?
}
css = match lightsheet.to_css(printer_options) {
let css = match lightsheet.to_css(printer_options) {
Ok(c) => {
drop(lightsheet);
c.code
}
Err(e) => return Err(SabryCompilerError::LightPrint(e)),
};
}
Ok(css)
}