build.rs 3.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394
  1. use dioxus_interpreter_js::binary_protocol::SLEDGEHAMMER_JS;
  2. use std::io::Write;
  3. const EDITS_PATH: &str = {
  4. #[cfg(any(target_os = "android", target_os = "windows"))]
  5. {
  6. "http://dioxus.index.html/edits"
  7. }
  8. #[cfg(not(any(target_os = "android", target_os = "windows")))]
  9. {
  10. "dioxus://index.html/edits"
  11. }
  12. };
  13. fn check_gnu() {
  14. // WARN about wry support on windows gnu targets. GNU windows targets don't work well in wry currently
  15. if std::env::var("CARGO_CFG_WINDOWS").is_ok()
  16. && std::env::var("CARGO_CFG_TARGET_ENV").unwrap() == "gnu"
  17. && !cfg!(feature = "gnu")
  18. {
  19. 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")
  20. }
  21. }
  22. fn main() {
  23. check_gnu();
  24. let prevent_file_upload = r#"// Prevent file inputs from opening the file dialog on click
  25. let inputs = document.querySelectorAll("input");
  26. for (let input of inputs) {
  27. if (!input.getAttribute("data-dioxus-file-listener")) {
  28. // prevent file inputs from opening the file dialog on click
  29. const type = input.getAttribute("type");
  30. if (type === "file") {
  31. input.setAttribute("data-dioxus-file-listener", true);
  32. input.addEventListener("click", (event) => {
  33. let target = event.target;
  34. let target_id = find_real_id(target);
  35. if (target_id !== null) {
  36. const send = (event_name) => {
  37. 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 });
  38. window.ipc.postMessage(message);
  39. };
  40. send("change&input");
  41. }
  42. event.preventDefault();
  43. });
  44. }
  45. }
  46. }"#;
  47. let polling_request = format!(
  48. r#"// Poll for requests
  49. window.interpreter.wait_for_request = (headless) => {{
  50. fetch(new Request("{EDITS_PATH}"))
  51. .then(response => {{
  52. response.arrayBuffer()
  53. .then(bytes => {{
  54. // In headless mode, the requestAnimationFrame callback is never called, so we need to run the bytes directly
  55. if (headless) {{
  56. run_from_bytes(bytes);
  57. }}
  58. else {{
  59. requestAnimationFrame(() => {{
  60. run_from_bytes(bytes);
  61. }});
  62. }}
  63. window.interpreter.wait_for_request(headless);
  64. }});
  65. }})
  66. }}"#
  67. );
  68. let mut interpreter = SLEDGEHAMMER_JS
  69. .replace("/*POST_HANDLE_EDITS*/", prevent_file_upload)
  70. .replace("export", "")
  71. + &polling_request;
  72. while let Some(import_start) = interpreter.find("import") {
  73. let import_end = interpreter[import_start..]
  74. .find(|c| c == ';' || c == '\n')
  75. .map(|i| i + import_start)
  76. .unwrap_or_else(|| interpreter.len());
  77. interpreter.replace_range(import_start..import_end, "");
  78. }
  79. let js = format!("{interpreter}\nconst config = new InterpreterConfig(false);");
  80. use minify_js::*;
  81. let session = Session::new();
  82. let mut out = Vec::new();
  83. minify(&session, TopLevelMode::Module, js.as_bytes(), &mut out).unwrap();
  84. let minified = String::from_utf8(out).unwrap();
  85. let mut file = std::fs::File::create("src/minified.js").unwrap();
  86. file.write_all(minified.as_bytes()).unwrap();
  87. }