-
Notifications
You must be signed in to change notification settings - Fork 0
/
build.rs
107 lines (87 loc) · 3.04 KB
/
build.rs
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
use eyre::Result;
use once_cell::sync::Lazy;
use serde_json::{json, to_writer_pretty};
use std::{
fs::{create_dir_all, read_to_string, write, File},
path::PathBuf,
};
use toml::Value;
use wasm_pack::command::build::{Build, BuildOptions, Target};
static GEN_PATH: Lazy<PathBuf> = Lazy::new(|| PathBuf::from("dist/"));
fn main() -> Result<()> {
let env: Option<&'static str> = option_env!("WASME_IGNORE_BUILD_SCRIPT");
if env.unwrap_or_else(|| "false").to_uppercase() != "TRUE".to_owned() {
let package: Value = include_str!("Cargo.toml").parse()?;
let package = &package["package"];
create_dir_all(GEN_PATH.as_path())?;
// TODO: Package with `trunk build --release` the `src`.
//
// Remember to package it before `wasm-pack`, because `trunk` removes all files in the
// directory.
generate_wasm_files(package)?;
generate_manifest_json(package)?;
};
Ok(())
}
fn generate_wasm_files(package: &Value) -> Result<()> {
let package_name = package["name"]
.as_str()
.expect("package.name is set.")
.to_owned();
// FIX: Will the `trunk` dist folder work for the html web extension?
let mut command = Build::try_from_opts(BuildOptions {
path: Some(PathBuf::from("scripts/")),
disable_dts: true,
target: Target::NoModules,
out_name: Some(package_name.clone()),
out_dir: format!("../{}", GEN_PATH.to_string_lossy()),
..Default::default()
})
.expect("wasm-pack command was not able to be parsed.");
command.run().expect("wasm-pack build did not succeeded.");
let mut gen_js = GEN_PATH.join(package_name.clone());
gen_js.set_extension("js");
let gen_js_contents = read_to_string(&gen_js)?;
write(
gen_js,
format!(
"(function () {{
{gen_js_contents}
wasm_bindgen(browser.runtime.getURL('{package_name}_bg.wasm'));
}})()"
),
)?;
Ok(())
}
fn generate_manifest_json(package: &Value) -> Result<()> {
let mut manifest_json = json!({
// Used as default if not provided.
"manifest_version": 2,
"name": package["name"],
"description": package["description"],
"homepage_url": package["homepage"],
"version": package["version"],
});
if let Some(manifest) = package.get("metadata").and_then(|m| m.get("manifest_json")) {
let manifest_table = manifest
.as_table()
.expect("The `package.metadata.manifest_json` is not well formatted.")
.iter()
.map(|(key, value)| {
(
key.to_owned(),
serde_json::to_value(value).expect("valid value."),
)
});
manifest_json
.as_object_mut()
.expect("valid object.")
.extend(manifest_table);
}
// TODO: include `res` files into `web_accessible_resources`.
to_writer_pretty(
File::create(GEN_PATH.join("manifest.json"))?,
&manifest_json,
)?;
Ok(())
}