Skip to content

Commit

Permalink
fix: ignore failures during minification
Browse files Browse the repository at this point in the history
If minification fails, then simply use the original content.
  • Loading branch information
ctron committed Dec 20, 2023
1 parent 07ec22d commit a3c694d
Show file tree
Hide file tree
Showing 4 changed files with 24 additions and 19 deletions.
2 changes: 1 addition & 1 deletion src/pipelines/html.rs
Original file line number Diff line number Diff line change
Expand Up @@ -181,7 +181,7 @@ impl HtmlPipeline {

// Assemble a new output index.html file.
let output_html = match self.cfg.release && !self.cfg.no_minification {
true => minify_html(target_html.html().as_bytes())?,
true => minify_html(target_html.html().as_bytes()),
false => target_html.html().as_bytes().to_vec(),
};

Expand Down
6 changes: 3 additions & 3 deletions src/pipelines/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -256,7 +256,7 @@ impl AssetFile {

bytes = if minify {
match file_type {
AssetFileType::Css => minify_css(bytes)?,
AssetFileType::Css => minify_css(bytes),
AssetFileType::Icon(image_type) => match image_type {
ImageType::Png => oxipng::optimize_from_memory(
bytes.as_ref(),
Expand All @@ -265,8 +265,8 @@ impl AssetFile {
.with_context(|| format!("error optimizing PNG {:?}", &self.path))?,
ImageType::Other => bytes,
},
AssetFileType::Js => minify_js(&bytes, TopLevelMode::Global)?,
AssetFileType::Mjs => minify_js(&bytes, TopLevelMode::Module)?,
AssetFileType::Js => minify_js(bytes, TopLevelMode::Global),
AssetFileType::Mjs => minify_js(bytes, TopLevelMode::Module),
_ => bytes,
}
} else {
Expand Down
2 changes: 1 addition & 1 deletion src/pipelines/rust/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -679,7 +679,7 @@ impl RustApp {
.context("error reading JS loader file")?;

let write_bytes = match self.cfg.release && !self.cfg.no_minification {
true => minify_js(&bytes, mode).unwrap_or(bytes),
true => minify_js(bytes, mode),
false => bytes,
};

Expand Down
33 changes: 19 additions & 14 deletions src/processing/minify.rs
Original file line number Diff line number Diff line change
@@ -1,35 +1,40 @@
use anyhow::anyhow;
use minify_js::TopLevelMode;

/// perform JS minification
pub fn minify_js(bytes: &[u8], mode: TopLevelMode) -> anyhow::Result<Vec<u8>> {
pub fn minify_js(bytes: Vec<u8>, mode: TopLevelMode) -> Vec<u8> {
let mut result: Vec<u8> = vec![];
let session = minify_js::Session::new();
minify_js::minify(&session, mode, bytes, &mut result)
.map_err(|err| anyhow!("Failed to minify JS: {err}"))?;

Ok(result)
match minify_js::minify(&session, mode, &bytes, &mut result) {
Ok(()) => result,
Err(err) => {
tracing::warn!("Failed to minify JS: {err}");
bytes
}
}
}

/// perform CSS minification
pub fn minify_css(bytes: Vec<u8>) -> anyhow::Result<Vec<u8>> {
pub fn minify_css(bytes: Vec<u8>) -> Vec<u8> {
use css_minify::optimizations::*;

if let Ok(css) = std::str::from_utf8(&bytes) {
Ok(Minifier::default()
.minify(css, Level::Three)
.map_err(|err| anyhow!("Failed to minify CSS: {err}"))?
.into_bytes())
} else {
Ok(bytes)
match Minifier::default().minify(css, Level::Three) {
Ok(result) => return result.into_bytes(),
Err(err) => {
tracing::warn!("Failed to minify CSS: {err}");
}
}
}

bytes
}

/// perform HTML minification
pub fn minify_html(html: &[u8]) -> anyhow::Result<Vec<u8>> {
pub fn minify_html(html: &[u8]) -> Vec<u8> {
let mut minify_cfg = minify_html::Cfg::spec_compliant();
minify_cfg.minify_css = true;
minify_cfg.minify_js = true;
minify_cfg.keep_closing_tags = true;
Ok(minify_html::minify(html, &minify_cfg))
minify_html::minify(html, &minify_cfg)
}

0 comments on commit a3c694d

Please sign in to comment.