minify js at runtime in liveview

This commit is contained in:
Evan Almloff 2024-02-01 10:10:05 -06:00
parent e2e0a934fd
commit 2371c9eed3
4 changed files with 45 additions and 6 deletions

40
Cargo.lock generated
View file

@ -83,6 +83,15 @@ dependencies = [
"zerocopy", "zerocopy",
] ]
[[package]]
name = "aho-corasick"
version = "0.7.20"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "cc936419f96fa211c1b9166887b38e5e40b19958e5b895be7c1f93adec7071ac"
dependencies = [
"memchr",
]
[[package]] [[package]]
name = "aho-corasick" name = "aho-corasick"
version = "1.1.2" version = "1.1.2"
@ -2715,6 +2724,7 @@ dependencies = [
"dioxus-interpreter-js", "dioxus-interpreter-js",
"futures-channel", "futures-channel",
"futures-util", "futures-util",
"minify-js",
"once_cell", "once_cell",
"pretty_env_logger", "pretty_env_logger",
"rocket", "rocket",
@ -4213,7 +4223,7 @@ version = "0.4.14"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "57da3b9b5b85bd66f31093f8c408b90a74431672542466497dcbdfdc02034be1" checksum = "57da3b9b5b85bd66f31093f8c408b90a74431672542466497dcbdfdc02034be1"
dependencies = [ dependencies = [
"aho-corasick", "aho-corasick 1.1.2",
"bstr 1.9.0", "bstr 1.9.0",
"log", "log",
"regex-automata 0.4.3", "regex-automata 0.4.3",
@ -4558,6 +4568,7 @@ source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "43a3c133739dddd0d2990f9a4bdf8eb4b21ef50e4851ca85ab661199821d510e" checksum = "43a3c133739dddd0d2990f9a4bdf8eb4b21ef50e4851ca85ab661199821d510e"
dependencies = [ dependencies = [
"ahash 0.8.7", "ahash 0.8.7",
"bumpalo",
] ]
[[package]] [[package]]
@ -6073,6 +6084,16 @@ dependencies = [
"unicase", "unicase",
] ]
[[package]]
name = "minify-js"
version = "0.5.6"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "22d6c512a82abddbbc13b70609cb2beff01be2c7afff534d6e5e1c85e438fc8b"
dependencies = [
"lazy_static",
"parse-js",
]
[[package]] [[package]]
name = "minimal-lexical" name = "minimal-lexical"
version = "0.2.1" version = "0.2.1"
@ -6802,6 +6823,19 @@ dependencies = [
"windows-targets 0.48.5", "windows-targets 0.48.5",
] ]
[[package]]
name = "parse-js"
version = "0.17.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "9ec3b11d443640ec35165ee8f6f0559f1c6f41878d70330fe9187012b5935f02"
dependencies = [
"aho-corasick 0.7.20",
"bumpalo",
"hashbrown 0.13.2",
"lazy_static",
"memchr",
]
[[package]] [[package]]
name = "password-hash" name = "password-hash"
version = "0.4.2" version = "0.4.2"
@ -7785,7 +7819,7 @@ version = "1.10.2"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "380b951a9c5e80ddfd6136919eef32310721aa4aacd4889a8d39124b026ab343" checksum = "380b951a9c5e80ddfd6136919eef32310721aa4aacd4889a8d39124b026ab343"
dependencies = [ dependencies = [
"aho-corasick", "aho-corasick 1.1.2",
"memchr", "memchr",
"regex-automata 0.4.3", "regex-automata 0.4.3",
"regex-syntax 0.8.2", "regex-syntax 0.8.2",
@ -7806,7 +7840,7 @@ version = "0.4.3"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "5f804c7828047e88b2d32e2d7fe5a105da8ee3264f01902f796c8e067dc2483f" checksum = "5f804c7828047e88b2d32e2d7fe5a105da8ee3264f01902f796c8e067dc2483f"
dependencies = [ dependencies = [
"aho-corasick", "aho-corasick 1.1.2",
"memchr", "memchr",
"regex-syntax 0.8.2", "regex-syntax 0.8.2",
] ]

View file

@ -84,7 +84,6 @@ exitcode = "1.1.2"
[build-dependencies] [build-dependencies]
# dioxus-interpreter-js = { workspace = true, features = ["binary-protocol"] } # dioxus-interpreter-js = { workspace = true, features = ["binary-protocol"] }
# minify-js = "0.5.6"
# These tests need to be run on the main thread, so they cannot use rust's test harness. # These tests need to be run on the main thread, so they cannot use rust's test harness.
[[test]] [[test]]

View file

@ -24,6 +24,7 @@ serde = { version = "1.0.151", features = ["derive"] }
serde_json = "1.0.91" serde_json = "1.0.91"
dioxus-html = { workspace = true, features = ["serialize", "eval", "mounted"] } dioxus-html = { workspace = true, features = ["serialize", "eval", "mounted"] }
rustc-hash = { workspace = true } rustc-hash = { workspace = true }
minify-js = "0.5.6"
dioxus-core = { workspace = true, features = ["serialize"] } dioxus-core = { workspace = true, features = ["serialize"] }
dioxus-interpreter-js = { workspace = true, features = ["binary-protocol"] } dioxus-interpreter-js = { workspace = true, features = ["binary-protocol"] }
dioxus-hot-reload = { workspace = true, optional = true } dioxus-hot-reload = { workspace = true, optional = true }
@ -62,7 +63,6 @@ dioxus = { workspace = true }
[build-dependencies] [build-dependencies]
# dioxus-interpreter-js = { workspace = true, features = ["binary-protocol"] } # dioxus-interpreter-js = { workspace = true, features = ["binary-protocol"] }
# minify-js = "0.5.6"
[features] [features]
default = ["hot-reload"] default = ["hot-reload"]

View file

@ -31,6 +31,7 @@ pub enum LiveViewError {
fn handle_edits_code() -> String { fn handle_edits_code() -> String {
use dioxus_interpreter_js::binary_protocol::SLEDGEHAMMER_JS; use dioxus_interpreter_js::binary_protocol::SLEDGEHAMMER_JS;
use minify_js::{minify, Session, TopLevelMode};
let serialize_file_uploads = r#"if ( let serialize_file_uploads = r#"if (
target.tagName === "INPUT" && target.tagName === "INPUT" &&
@ -82,7 +83,12 @@ fn handle_edits_code() -> String {
let main_js = include_str!("./main.js"); let main_js = include_str!("./main.js");
format!("{interpreter}\n{main_js}") let js = format!("{interpreter}\n{main_js}");
let session = Session::new();
let mut out = Vec::new();
minify(&session, TopLevelMode::Module, js.as_bytes(), &mut out).unwrap();
String::from_utf8(out).unwrap()
} }
/// This script that gets injected into your app connects this page to the websocket endpoint /// This script that gets injected into your app connects this page to the websocket endpoint