fix: apply lightningcss to bundle once again. Seems the right thing to do
This commit is contained in:
parent
747b2bca21
commit
d59729e6fe
2 changed files with 57 additions and 41 deletions
|
|
@ -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(())
|
||||||
|
|
|
||||||
|
|
@ -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,48 +30,58 @@ impl CompilerAdapter {
|
||||||
|
|
||||||
let mut css = grass::from_string(code, &options)?;
|
let mut css = grass::from_string(code, &options)?;
|
||||||
|
|
||||||
|
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,
|
||||||
|
lightningcss::stylesheet::ParserOptions::default(),
|
||||||
|
) {
|
||||||
|
Ok(s) => s,
|
||||||
|
Err(e) => {
|
||||||
|
return Err(SabryCompilerError::LightParse {
|
||||||
|
kind: e.kind.to_string(),
|
||||||
|
loc: e.loc,
|
||||||
|
})
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
|
let targets = Targets {
|
||||||
|
browsers: Some(self.config.lightningcss.targets.clone().into()),
|
||||||
|
..Default::default()
|
||||||
|
};
|
||||||
|
|
||||||
|
let printer_options = PrinterOptions {
|
||||||
|
minify: self.config.css.minify,
|
||||||
|
targets,
|
||||||
|
..Default::default()
|
||||||
|
};
|
||||||
|
|
||||||
|
let minify_options = MinifyOptions {
|
||||||
|
targets,
|
||||||
|
..Default::default()
|
||||||
|
};
|
||||||
|
|
||||||
if self.config.css.minify {
|
if self.config.css.minify {
|
||||||
//its lightningcss time!
|
lightsheet.minify(minify_options)?
|
||||||
let mut lightsheet = match lightningcss::stylesheet::StyleSheet::parse(
|
|
||||||
&css,
|
|
||||||
lightningcss::stylesheet::ParserOptions::default(),
|
|
||||||
) {
|
|
||||||
Ok(s) => s,
|
|
||||||
Err(e) => {
|
|
||||||
return Err(SabryCompilerError::LightParse {
|
|
||||||
kind: e.kind.to_string(),
|
|
||||||
loc: e.loc,
|
|
||||||
})
|
|
||||||
}
|
|
||||||
};
|
|
||||||
|
|
||||||
let targets = Targets {
|
|
||||||
browsers: Some(self.config.lightningcss.targets.clone().into()),
|
|
||||||
..Default::default()
|
|
||||||
};
|
|
||||||
|
|
||||||
let printer_options = PrinterOptions {
|
|
||||||
minify: self.config.css.minify,
|
|
||||||
targets,
|
|
||||||
..Default::default()
|
|
||||||
};
|
|
||||||
|
|
||||||
let minify_options = MinifyOptions {
|
|
||||||
targets,
|
|
||||||
..Default::default()
|
|
||||||
};
|
|
||||||
|
|
||||||
lightsheet.minify(minify_options)?;
|
|
||||||
|
|
||||||
css = match lightsheet.to_css(printer_options) {
|
|
||||||
Ok(c) => {
|
|
||||||
drop(lightsheet);
|
|
||||||
c.code
|
|
||||||
}
|
|
||||||
Err(e) => return Err(SabryCompilerError::LightPrint(e)),
|
|
||||||
};
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
let css = match lightsheet.to_css(printer_options) {
|
||||||
|
Ok(c) => {
|
||||||
|
drop(lightsheet);
|
||||||
|
c.code
|
||||||
|
}
|
||||||
|
Err(e) => return Err(SabryCompilerError::LightPrint(e)),
|
||||||
|
};
|
||||||
|
|
||||||
Ok(css)
|
Ok(css)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue