From e2e0a934fde4ee78e18ee4940dacbd5109405072 Mon Sep 17 00:00:00 2001 From: Evan Almloff Date: Thu, 1 Feb 2024 10:07:03 -0600 Subject: [PATCH] remove minified js from build scripts --- packages/desktop/.gitignore | 1 - packages/desktop/_build.rs | 94 -------------------------------- packages/desktop/build.rs | 13 +++++ packages/desktop/src/protocol.rs | 76 +++++++++++++++++++++++++- packages/liveview/.gitignore | 1 - packages/liveview/_build.rs | 64 ---------------------- packages/liveview/src/lib.rs | 60 +++++++++++++++++++- 7 files changed, 145 insertions(+), 164 deletions(-) delete mode 100644 packages/desktop/.gitignore delete mode 100644 packages/desktop/_build.rs create mode 100644 packages/desktop/build.rs delete mode 100644 packages/liveview/.gitignore delete mode 100644 packages/liveview/_build.rs diff --git a/packages/desktop/.gitignore b/packages/desktop/.gitignore deleted file mode 100644 index 1aec3a9f9..000000000 --- a/packages/desktop/.gitignore +++ /dev/null @@ -1 +0,0 @@ -/src/minified.js \ No newline at end of file diff --git a/packages/desktop/_build.rs b/packages/desktop/_build.rs deleted file mode 100644 index 8d1f4b3e5..000000000 --- a/packages/desktop/_build.rs +++ /dev/null @@ -1,94 +0,0 @@ -use dioxus_interpreter_js::binary_protocol::SLEDGEHAMMER_JS; - -use std::io::Write; - -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() { - // 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) => { - 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(); - }); - } - } - }"#; - let polling_request = format!( - r#"// Poll for requests - window.interpreter.wait_for_request = (headless) => {{ - fetch(new Request("{EDITS_PATH}")) - .then(response => {{ - response.arrayBuffer() - .then(bytes => {{ - // In headless mode, the requestAnimationFrame callback is never called, so we need to run the bytes directly - if (headless) {{ - run_from_bytes(bytes); - }} - else {{ - requestAnimationFrame(() => {{ - run_from_bytes(bytes); - }}); - }} - window.interpreter.wait_for_request(headless); - }}); - }}) - }}"# - ); - let mut interpreter = SLEDGEHAMMER_JS - .replace("/*POST_HANDLE_EDITS*/", prevent_file_upload) - .replace("export", "") - + &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);"); - - 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(); -} diff --git a/packages/desktop/build.rs b/packages/desktop/build.rs new file mode 100644 index 000000000..6d90fe746 --- /dev/null +++ b/packages/desktop/build.rs @@ -0,0 +1,13 @@ +fn check_gnu() { + // 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(); +} diff --git a/packages/desktop/src/protocol.rs b/packages/desktop/src/protocol.rs index 57cff9aa5..b903c63e0 100644 --- a/packages/desktop/src/protocol.rs +++ b/packages/desktop/src/protocol.rs @@ -1,11 +1,82 @@ use crate::{assets::*, edits::EditQueue}; +use dioxus_interpreter_js::SLEDGEHAMMER_JS; use std::path::{Path, PathBuf}; use wry::{ http::{status::StatusCode, Request, Response}, RequestAsyncResponder, Result, }; -static MINIFIED: &str = include_str!("./minified.js"); +fn handle_edits_code() -> String { + 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" + } + }; + + 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) => { + 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(); + }); + } + } + }"#; + let polling_request = format!( + r#"// Poll for requests + window.interpreter.wait_for_request = (headless) => {{ + fetch(new Request("{EDITS_PATH}")) + .then(response => {{ + response.arrayBuffer() + .then(bytes => {{ + // In headless mode, the requestAnimationFrame callback is never called, so we need to run the bytes directly + if (headless) {{ + run_from_bytes(bytes); + }} + else {{ + requestAnimationFrame(() => {{ + run_from_bytes(bytes); + }}); + }} + window.interpreter.wait_for_request(headless); + }}); + }}) + }}"# + ); + let mut interpreter = SLEDGEHAMMER_JS + .replace("/*POST_HANDLE_EDITS*/", prevent_file_upload) + .replace("export", "") + + &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, ""); + } + + format!("{interpreter}\nconst config = new InterpreterConfig(false);") +} + static DEFAULT_INDEX: &str = include_str!("./index.html"); /// Build the index.html file we use for bootstrapping a new app @@ -162,10 +233,11 @@ fn serve_from_fs(path: PathBuf) -> Result>> { /// - headless: is this page being loaded but invisible? Important because not all windows are visible and the /// interpreter can't connect until the window is ready. fn module_loader(root_id: &str, headless: bool) -> String { + let js = handle_edits_code(); format!( r#" "# )