Repository navigation
Expand file tree
/
Copy pathbuild.rs
More file actions
86 lines (77 loc) · 2.7 KB
/
Copy pathbuild.rs
File metadata and controls
86 lines (77 loc) · 2.7 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
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
fn main() {
let manifest_dir = std::env::var("CARGO_MANIFEST_DIR").unwrap();
let pkg_dir = std::path::Path::new(&manifest_dir).join("target/site/pkg");
let out_dir = std::env::var("OUT_DIR").unwrap();
let dest = std::path::Path::new(&out_dir).join("embedded_assets.rs");
let js = pkg_dir.join("transity.js");
let css = pkg_dir.join("transity.css");
let wasm = pkg_dir.join("transity.wasm");
// Apache ECharts for the trend charts, installed via `bun install`
let echarts = std::path::Path::new(&manifest_dir)
.join("node_modules/echarts/dist/echarts.min.js");
let assets_present = js.exists() && css.exists() && wasm.exists();
let ssr_enabled = std::env::var("CARGO_FEATURE_SSR").is_ok();
let driven_by_cargo_leptos = std::env::var("LEPTOS_OUTPUT_NAME").is_ok();
if ssr_enabled && !echarts.exists() {
println!(
"cargo:warning=ECharts missing at {}. \
Run `bun install` (or `make server-build`) before building — \
`transity server` will exit at startup otherwise.",
echarts.display()
);
}
if ssr_enabled && !assets_present && !driven_by_cargo_leptos {
println!(
"cargo:warning=Leptos hydration assets missing in {}. \
Run `cargo leptos build` (or `make server-build`) before \
installing — `transity server` will exit at startup otherwise.",
pkg_dir.display()
);
}
let code = if assets_present {
format!(
r#"
pub const JS: &[u8] = include_bytes!("{}");
pub const CSS: &[u8] = include_bytes!("{}");
pub const WASM: &[u8] = include_bytes!("{}");
pub const ASSETS_AVAILABLE: bool = true;
"#,
js.display(),
css.display(),
wasm.display(),
)
} else {
r#"
pub const JS: &[u8] = b"";
pub const CSS: &[u8] = b"";
pub const WASM: &[u8] = b"";
pub const ASSETS_AVAILABLE: bool = false;
"#
.to_string()
};
let code = code
+ &if echarts.exists() {
format!(
r#"
pub const ECHARTS: &[u8] = include_bytes!("{}");
pub const ECHARTS_AVAILABLE: bool = true;
"#,
echarts.display(),
)
} else {
r#"
pub const ECHARTS: &[u8] = b"";
pub const ECHARTS_AVAILABLE: bool = false;
"#
.to_string()
};
std::fs::write(&dest, code).unwrap();
println!("cargo:rerun-if-changed=target/site/pkg/transity.js");
println!("cargo:rerun-if-changed=target/site/pkg/transity.css");
println!("cargo:rerun-if-changed=target/site/pkg/transity.wasm");
println!("cargo:rerun-if-changed=node_modules/echarts/dist/echarts.min.js");
// bun copies files from its cache with their old mtimes, so a fresh
// install wouldn't trigger a rebuild. The package directory itself
// gets a current mtime when it's (re)installed.
println!("cargo:rerun-if-changed=node_modules/echarts");
}