Преглед изворни кода

minify js at runtime in liveview

Evan Almloff пре 1 година
родитељ
комит
2371c9eed3
4 измењених фајлова са 45 додато и 6 уклоњено
  1. 37 3
      Cargo.lock
  2. 0 1
      packages/desktop/Cargo.toml
  3. 1 1
      packages/liveview/Cargo.toml
  4. 7 1
      packages/liveview/src/lib.rs

+ 37 - 3
Cargo.lock

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

+ 0 - 1
packages/desktop/Cargo.toml

@@ -84,7 +84,6 @@ exitcode = "1.1.2"
 
 [build-dependencies]
 # 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.
 [[test]]

+ 1 - 1
packages/liveview/Cargo.toml

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

+ 7 - 1
packages/liveview/src/lib.rs

@@ -31,6 +31,7 @@ pub enum LiveViewError {
 
 fn handle_edits_code() -> String {
     use dioxus_interpreter_js::binary_protocol::SLEDGEHAMMER_JS;
+    use minify_js::{minify, Session, TopLevelMode};
 
     let serialize_file_uploads = r#"if (
         target.tagName === "INPUT" &&
@@ -82,7 +83,12 @@ fn handle_edits_code() -> String {
 
     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