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
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
use crate::{
config::{CrateConfig, ExecutableType},
error::{Error, Result},
};
use std::{
io::{Read, Write},
process::Command,
};
use wasm_bindgen_cli_support::Bindgen;
pub fn build(config: &CrateConfig) -> Result<()> {
let CrateConfig {
out_dir,
crate_dir,
target_dir,
static_dir,
executable,
..
} = config;
let t_start = std::time::Instant::now();
log::info!("Running build commands...");
let mut cmd = Command::new("cargo");
cmd.current_dir(&crate_dir)
.arg("build")
.arg("--target")
.arg("wasm32-unknown-unknown")
.stdout(std::process::Stdio::piped())
.stderr(std::process::Stdio::piped());
if config.release {
cmd.arg("--release");
}
match executable {
ExecutableType::Binary(name) => cmd.arg("--bin").arg(name),
ExecutableType::Lib(name) => cmd.arg("--lib").arg(name),
ExecutableType::Example(name) => cmd.arg("--example").arg(name),
};
let mut child = cmd.spawn()?;
let output = child.wait()?;
if output.success() {
log::info!("Build complete!");
} else {
log::error!("Build failed!");
let mut reason = String::new();
child.stderr.unwrap().read_to_string(&mut reason)?;
return Err(Error::BuildFailed(reason));
}
let bindgen_outdir = out_dir.join("wasm");
let mut bindgen_builder = Bindgen::new();
let release_type = match config.release {
true => "release",
false => "debug",
};
let input_path = match executable {
ExecutableType::Binary(name) | ExecutableType::Lib(name) => target_dir
.join(format!("wasm32-unknown-unknown/{}", release_type))
.join(format!("{}.wasm", name)),
ExecutableType::Example(name) => target_dir
.join(format!("wasm32-unknown-unknown/{}/examples", release_type))
.join(format!("{}.wasm", name)),
};
bindgen_builder
.input_path(input_path)
.web(true)?
.debug(true)
.demangle(true)
.keep_debug(true)
.remove_name_section(false)
.remove_producers_section(false)
.out_name("module")
.generate(&bindgen_outdir)?;
log::info!("Writing to '{:#?}' directory...", out_dir);
let mut file = std::fs::File::create(out_dir.join("index.html"))?;
file.write_all(gen_page("./wasm/module.js").as_str().as_bytes())?;
let copy_options = fs_extra::dir::CopyOptions::new();
match fs_extra::dir::copy(static_dir, out_dir, ©_options) {
Ok(_) => {}
Err(_e) => {
log::warn!("Error copying dir");
}
}
let t_end = std::time::Instant::now();
log::info!("Done in {}ms! 🎉", (t_end - t_start).as_millis());
Ok(())
}
fn gen_page(module: &str) -> String {
format!(
r#"
<html>
<head>
<meta content="text/html;charset=utf-8" http-equiv="Content-Type" />
<meta charset="UTF-8" />
</head>
<body>
<div id="main">
</div>
<!-- Note the usage of `type=module` here as this is an ES6 module -->
<script type="module">
import init from "{}";
init("./wasm/module_bg.wasm");
</script>
<div id="dioxusroot"> </div>
</body>
</html>
"#,
module
)
}