Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Minify embedded css and js with additional lib flags #1922

Merged
merged 1 commit into from
Oct 11, 2022
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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);
}
}