Repository navigation
Expand file tree
/
Copy pathbuild.rs
More file actions
58 lines (53 loc) · 2.01 KB
/
Copy pathbuild.rs
File metadata and controls
58 lines (53 loc) · 2.01 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
//! 构建脚本
//! 1. Windows 下把 `assets/icon.ico` 编进 exe 资源(资源 ID 1,与 `Icon::from_resource(1)` 对应);
//! 2. 把前端静态资源用 brotli 压一遍再内嵌——mermaid 3.5 MB 压完只剩 0.75 MB,
//! 响应带 `Content-Encoding: br` 由 WebView 解码,可执行文件因此小一半以上。
use std::env;
use std::fs;
use std::io;
use std::path::{Path, PathBuf};
/// 需要压缩后内嵌的资源(与 `src/assets.rs` 里的常量一一对应)。
const COMPRESSED: &[&str] = &[
"index.html",
"app.css",
"app.js",
"editor.js",
"hljs.min.js",
"hljs-github.min.css",
"hljs-github-dark.min.css",
"mermaid.min.js",
];
fn main() {
if env::var("CARGO_CFG_TARGET_OS").as_deref() == Ok("windows") {
winresource::WindowsResource::new()
.set_icon("assets/icon.ico")
.compile()
.expect("无法把 assets/icon.ico 嵌入 exe 资源");
}
compress_assets();
}
fn compress_assets() {
let out_dir = PathBuf::from(env::var("OUT_DIR").expect("缺少 OUT_DIR"));
let mut params = brotli::enc::BrotliEncoderParams::default();
params.quality = 11;
params.lgwin = 24;
let mut raw_total = 0usize;
let mut packed_total = 0usize;
for name in COMPRESSED {
let src = Path::new("assets").join(name);
println!("cargo:rerun-if-changed={}", src.display());
let raw = fs::read(&src).unwrap_or_else(|err| panic!("读取 {name} 失败:{err}"));
let mut packed = Vec::new();
brotli::BrotliCompress(&mut &raw[..], &mut packed, ¶ms)
.unwrap_or_else(|err: io::Error| panic!("压缩 {name} 失败:{err}"));
fs::write(out_dir.join(format!("{name}.br")), &packed)
.unwrap_or_else(|err| panic!("写出 {name}.br 失败:{err}"));
raw_total += raw.len();
packed_total += packed.len();
}
println!(
"cargo:warning=静态资源 brotli:{} KB → {} KB",
raw_total / 1024,
packed_total / 1024
);
}