-
Notifications
You must be signed in to change notification settings - Fork 26
Expand file tree
/
Copy pathbuild.rs
More file actions
63 lines (49 loc) · 2.11 KB
/
build.rs
File metadata and controls
63 lines (49 loc) · 2.11 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
use flate2::Compression;
use flate2::write::GzEncoder;
use std::env;
use std::fs;
use std::io::{self, Write};
use std::path::Path;
// Minifies and compresses js, css and html
fn main() {
println!("cargo:rerun-if-changed=static/script.js");
println!("cargo:rerun-if-changed=static/style.css");
println!("cargo:rerun-if-changed=static/index.html");
let out_dir = env::var("CARGO_MANIFEST_DIR").expect("CARGO_MANIFEST_DIR env var undefined");
let static_dir = Path::new(&out_dir).join("static");
// --- Process JavaScript ---
let js_path = static_dir.join("script.js");
let gz_js_path = static_dir.join("script.js.gz");
if let Ok(js_code) = fs::read_to_string(&js_path) {
let minified_js = minifier::js::minify(&js_code);
compress_with_gzip(minified_js.to_string().as_bytes(), &gz_js_path)
.expect("Failed to compress minified JS with Gzip");
} else {
eprintln!("Warning: Could not read {}", js_path.display());
}
// --- Process CSS ---
let css_path = static_dir.join("style.css");
let gz_css_path = static_dir.join("style.css.gz");
if let Ok(css_code) = fs::read_to_string(&css_path) {
let minified_css = minifier::css::minify(&css_code).expect("Failed to minify CSS");
compress_with_gzip(minified_css.to_string().as_bytes(), &gz_css_path)
.expect("Failed to compress minified CSS with Gzip");
} else {
eprintln!("Warning: Could not read {}", css_path.display());
}
// --- Process HTML ---
let html_path = static_dir.join("index.html");
let gz_html_path = static_dir.join("index.html.gz");
if let Ok(html_content) = fs::read(&html_path) {
compress_with_gzip(&html_content, &gz_html_path)
.expect("Failed to compress index.html with Gzip");
} else {
eprintln!("Warning: Could not read {}", html_path.display());
}
}
fn compress_with_gzip(data: &[u8], output_path: &Path) -> io::Result<()> {
let output_file = fs::File::create(output_path)?;
let mut encoder = GzEncoder::new(output_file, Compression::best());
encoder.write_all(data)?;
Ok(())
}