dioxus/packages/desktop/_build.rs

95 lines
3.8 KiB
Rust
Raw Normal View History

2023-10-19 21:40:08 +00:00
use dioxus_interpreter_js::binary_protocol::SLEDGEHAMMER_JS;
use std::io::Write;
2023-10-20 18:05:13 +00:00
const EDITS_PATH: &str = {
#[cfg(any(target_os = "android", target_os = "windows"))]
{
"http://dioxus.index.html/edits"
}
#[cfg(not(any(target_os = "android", target_os = "windows")))]
{
"dioxus://index.html/edits"
}
};
fn check_gnu() {
2023-12-07 13:10:53 +00:00
// WARN about wry support on windows gnu targets. GNU windows targets don't work well in wry currently
if std::env::var("CARGO_CFG_WINDOWS").is_ok()
&& std::env::var("CARGO_CFG_TARGET_ENV").unwrap() == "gnu"
&& !cfg!(feature = "gnu")
{
println!("cargo:warning=GNU windows targets have some limitations within Wry. Using the MSVC windows toolchain is recommended. If you would like to use continue using GNU, you can read https://github.com/wravery/webview2-rs#cross-compilation and disable this warning by adding the gnu feature to dioxus-desktop in your Cargo.toml")
}
}
fn main() {
check_gnu();
let prevent_file_upload = r#"// Prevent file inputs from opening the file dialog on click
let inputs = document.querySelectorAll("input");
for (let input of inputs) {
if (!input.getAttribute("data-dioxus-file-listener")) {
// prevent file inputs from opening the file dialog on click
const type = input.getAttribute("type");
if (type === "file") {
input.setAttribute("data-dioxus-file-listener", true);
input.addEventListener("click", (event) => {
let target = event.target;
let target_id = find_real_id(target);
if (target_id !== null) {
const send = (event_name) => {
2023-10-20 14:18:16 +00:00
const message = window.interpreter.serializeIpcMessage("file_diolog", { accept: target.getAttribute("accept"), directory: target.getAttribute("webkitdirectory") === "true", multiple: target.hasAttribute("multiple"), target: parseInt(target_id), bubbles: event_bubbles(event_name), event: event_name });
window.ipc.postMessage(message);
};
send("change&input");
}
event.preventDefault();
});
}
}
}"#;
2023-10-20 18:05:13 +00:00
let polling_request = format!(
r#"// Poll for requests
2023-12-08 21:14:32 +00:00
window.interpreter.wait_for_request = (headless) => {{
2023-10-20 18:05:13 +00:00
fetch(new Request("{EDITS_PATH}"))
.then(response => {{
2023-10-20 14:18:16 +00:00
response.arrayBuffer()
2023-10-20 18:05:13 +00:00
.then(bytes => {{
2023-12-08 21:14:32 +00:00
// In headless mode, the requestAnimationFrame callback is never called, so we need to run the bytes directly
if (headless) {{
2023-12-05 02:45:26 +00:00
run_from_bytes(bytes);
2023-12-08 21:14:32 +00:00
}}
else {{
requestAnimationFrame(() => {{
run_from_bytes(bytes);
}});
}}
window.interpreter.wait_for_request(headless);
2023-10-20 18:05:13 +00:00
}});
}})
}}"#
);
2023-10-20 14:06:19 +00:00
let mut interpreter = SLEDGEHAMMER_JS
.replace("/*POST_HANDLE_EDITS*/", prevent_file_upload)
2023-10-20 14:18:16 +00:00
.replace("export", "")
2023-10-20 18:05:13 +00:00
+ &polling_request;
while let Some(import_start) = interpreter.find("import") {
let import_end = interpreter[import_start..]
.find(|c| c == ';' || c == '\n')
.map(|i| i + import_start)
.unwrap_or_else(|| interpreter.len());
interpreter.replace_range(import_start..import_end, "");
}
let js = format!("{interpreter}\nconst config = new InterpreterConfig(false);");
2023-10-20 14:18:16 +00:00
use minify_js::*;
let session = Session::new();
let mut out = Vec::new();
minify(&session, TopLevelMode::Module, js.as_bytes(), &mut out).unwrap();
let minified = String::from_utf8(out).unwrap();
let mut file = std::fs::File::create("src/minified.js").unwrap();
file.write_all(minified.as_bytes()).unwrap();
}