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 • committed by spushk
commit fca2410f09
Signed by: spushk
GPG key ID: 7797B761E105E8AA
2 changed files with 57 additions and 41 deletions

View file

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

View file

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