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
use std::env;
use std::error::Error;
use std::ffi::OsStr;
use std::fs;
use std::path::{Path, PathBuf};
fn main() -> Result<(), Box<dyn Error>> {
let target_dir = find_target_dir()?;
let dir = target_dir.join("static");
fs::create_dir_all(&dir)?;
// NOTE: Never declare `cargo:rerun-if-changed` for `dir` or anything below it.
// It is pure output of this script, and cargo watches directories recursively,
// so watching it guarantees a false invalidation on every build: `include_static!`
// writes into `static/included` while downstream crates are being expanded, and
// every vertigo build-script unit sharing this target dir rewrites these files.
// Cleanup of `static/included` and `tailwind` belongs to vertigo-cli, which is
// the only thing that sets VERTIGO_BUNDLE (see cli's cargo_build.rs).
bundle_file(
"src/driver_module/wasm_run.js",
include_str!("src/driver_module/wasm_run.js"),
&dir,
"wasm_run.js",
)?;
bundle_file(
"src/driver_module/wasm_run.js.map",
include_str!("src/driver_module/wasm_run.js.map"),
&dir,
"wasm_run.js.map",
)?;
Ok(())
}
fn find_target_dir() -> Result<PathBuf, Box<dyn Error>> {
let out_dir = PathBuf::from(env::var("OUT_DIR")?);
let target_dir = out_dir
.ancestors()
.find(|dir| dir.file_name() == Some(OsStr::new("build")))
.and_then(Path::parent)
.ok_or_else(|| format!("Can't find target dir in OUT_DIR: {}", out_dir.display()))?;
Ok(target_dir.to_path_buf())
}
fn bundle_file(
in_path: &str,
content: &str,
out_dir: &Path,
file_name: &str,
) -> Result<(), Box<dyn Error>> {
// Invokes build script again if this file changed. These are the only inputs of
// this script, and emitting them switches cargo from "watch all sources" to
// "watch only these paths" (changes to build.rs itself are still covered, via the
// build script binary's own fingerprint).
println!("cargo:rerun-if-changed={in_path}");
let out_path = out_dir.join(file_name);
fs::write(&out_path, content.as_bytes())?;
println!("Bundled {}", out_path.to_string_lossy());
Ok(())
}