Skip to content

Commit

Permalink
Minify embedded css and js with additional lib flags (#1922)
Browse files Browse the repository at this point in the history
  • Loading branch information
JeremyBarbosa authored and Keats committed Feb 16, 2023
1 parent cce5684 commit bedc6d7
Showing 1 changed file with 59 additions and 0 deletions.
59 changes: 59 additions & 0 deletions components/site/src/minify.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@ use libs::minify_html::{minify, Cfg};
pub fn html(html: String) -> Result<String> {
let mut cfg = Cfg::spec_compliant();
cfg.keep_html_and_head_opening_tags = true;
cfg.minify_css = true;
cfg.minify_js = true;

let minified = minify(html.as_bytes(), &cfg);
match std::str::from_utf8(&minified) {
Expand Down Expand Up @@ -83,4 +85,61 @@ mod tests {
let res = html(input.to_owned()).unwrap();
assert_eq!(res, expected);
}

// https://github.com/getzola/zola/issues/1765
#[test]
fn can_minify_css() {
let input = r#"
<!doctype html>
<html>
<head>
<meta charset="utf-8">
<style>
p {
color: white;
margin-left: 10000px;
}
</style>
</head>
<body>
<p>Example blog post</p>
FOO BAR
</body>
</html>
"#;
let expected = r#"<!doctype html><html><head><meta charset=utf-8><style>p{color:white;margin-left:10000px}</style><body><p>Example blog post</p> FOO BAR"#;
let res = html(input.to_owned()).unwrap();
assert_eq!(res, expected);
}

// https://github.com/getzola/zola/issues/1765
#[test]
fn can_minify_js() {
let input = r#"
<!doctype html>
<html>
<head>
<meta charset="utf-8">
<script>
alert("Hello World!");
console.log("Some information: %o", information);
</script>
</head>
<body>
<p>Example blog post</p>
FOO BAR
</body>
</html>
"#;
let expected = r#"<!doctype html><html><head><meta charset=utf-8><script>alert("Hello World!");console.log("Some information: %o",information)</script><body><p>Example blog post</p> FOO BAR"#;
let res = html(input.to_owned()).unwrap();
assert_eq!(res, expected);
}
}

0 comments on commit bedc6d7

Please sign in to comment.