From ebdcb6a90a374582eb8df4399354cd8f6f9580a7 Mon Sep 17 00:00:00 2001 From: Greg Burri Date: Wed, 6 Nov 2024 17:48:51 +0100 Subject: [PATCH] Frontend: - Remove we_alloce - Replace webpack by rspack --- Cargo.lock | 106 +++++++----------- README.md | 4 + TODO.md | 7 +- frontend/Cargo.toml | 2 +- frontend/deploy.nu | 2 +- frontend/package.json | 14 +-- .../{webpack.config.js => rspack.config.js} | 8 +- frontend/src/handles.rs | 4 +- frontend/src/lib.rs | 12 +- frontend/start.nu | 1 + 10 files changed, 60 insertions(+), 100 deletions(-) rename frontend/{webpack.config.js => rspack.config.js} (66%) diff --git a/Cargo.lock b/Cargo.lock index 84749ed..785972b 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -23,7 +23,7 @@ version = "0.8.11" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "e89da841a80418a9b391ebaea17f5c112ffaaa96f621d2c285b5174da76b9011" dependencies = [ - "cfg-if 1.0.0", + "cfg-if", "once_cell", "version_check", "zerocopy", @@ -61,9 +61,9 @@ dependencies = [ [[package]] name = "anstream" -version = "0.6.17" +version = "0.6.18" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "23a1e53f0f5d86382dafe1cf314783b2044280f406e7e1506368220ad11b1338" +checksum = "8acc5369981196006228e28809f761875c0327210a891e941f4c683b3a99529b" dependencies = [ "anstyle", "anstyle-parse", @@ -299,7 +299,7 @@ source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "8d82cb332cdfaed17ae235a638438ac4d4839913cc2af585c3c6746e8f8bee1a" dependencies = [ "addr2line", - "cfg-if 1.0.0", + "cfg-if", "libc", "miniz_oxide", "object", @@ -381,19 +381,13 @@ checksum = "9ac0150caa2ae65ca5bd83f25c7de183dea78d4d366469f148435e2acfbad0da" [[package]] name = "cc" -version = "1.1.34" +version = "1.1.36" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "67b9470d453346108f93a59222a9a1a5724db32d0a4727b7ab7ace4b4d822dc9" +checksum = "baee610e9452a8f6f0a1b6194ec09ff9e2d85dea54432acdae41aa0761c95d70" dependencies = [ "shlex", ] -[[package]] -name = "cfg-if" -version = "0.1.10" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "4785bdd1c96b2a846b2bd7cc02e86b6b3dbf14e7e53446c4f54c92a361040822" - [[package]] name = "cfg-if" version = "1.0.0" @@ -510,7 +504,7 @@ version = "0.1.7" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "a06aeb73f470f66dcdbf7223caeebb85984942f22f1adb2a088cf9668146bbbc" dependencies = [ - "cfg-if 1.0.0", + "cfg-if", "wasm-bindgen", ] @@ -725,7 +719,7 @@ version = "0.8.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "136d1b5283a1ab77bd9257427ffd09d8667ced0570b6f938942bc7568ed5b943" dependencies = [ - "cfg-if 1.0.0", + "cfg-if", "home", "windows-sys 0.48.0", ] @@ -781,7 +775,6 @@ dependencies = [ "console_error_panic_hook", "wasm-bindgen", "web-sys", - "wee_alloc", ] [[package]] @@ -872,7 +865,7 @@ version = "0.2.15" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "c4567c8db10ae91089c99af84c68c38da3ec2f087c3f82960bcdbf3656b6f4d7" dependencies = [ - "cfg-if 1.0.0", + "cfg-if", "libc", "wasi", ] @@ -895,9 +888,9 @@ dependencies = [ [[package]] name = "hashbrown" -version = "0.15.0" +version = "0.15.1" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "1e087f84d4f86bf4b218b927129862374b72199ae7d8657835f1e89000eea4fb" +checksum = "3a9bfc1af68b1726ea47d3d5109de126281def866b33970e10fbab11b5dafab3" [[package]] name = "hashlink" @@ -959,7 +952,7 @@ version = "0.4.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "f9c7c7c8ac16c798734b8a24560c1362120597c40d5e1459f09498f8f6c8f2ba" dependencies = [ - "cfg-if 1.0.0", + "cfg-if", "libc", "windows", ] @@ -1203,24 +1196,23 @@ dependencies = [ [[package]] name = "idna" -version = "0.5.0" +version = "1.0.3" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "634d9b1461af396cad843f47fdba5597a4f9e6ddd4bfb6ff5d85028c25cb12f6" +checksum = "686f825264d630750a544639377bae737628043f20d38bbc029e8f29ea968a7e" dependencies = [ - "unicode-bidi", - "unicode-normalization", + "idna_adapter", + "smallvec", + "utf8_iter", ] [[package]] -name = "idna" -version = "1.0.2" +name = "idna_adapter" +version = "1.2.0" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "bd69211b9b519e98303c015e21a007e293db403b6c85b9b124e133d25e242cdd" +checksum = "daca1df1c957320b2cf139ac61e7bd64fed304c5040df000a745aa1de3b4ef71" dependencies = [ "icu_normalizer", "icu_properties", - "smallvec", - "utf8_iter", ] [[package]] @@ -1230,7 +1222,7 @@ source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "707907fe3c25f5424cce2cb7e1cbcafee6bdbe735ca90ef77c29e84591e5b9da" dependencies = [ "equivalent", - "hashbrown 0.15.0", + "hashbrown 0.15.1", ] [[package]] @@ -1288,7 +1280,7 @@ dependencies = [ "futures-util", "hostname", "httpdate", - "idna 1.0.2", + "idna", "mime", "nom", "percent-encoding", @@ -1375,7 +1367,7 @@ version = "0.10.6" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "d89e7ee0cfbedfc4da3340218492196241d89eefb6dab27de5df917a6d2e78cf" dependencies = [ - "cfg-if 1.0.0", + "cfg-if", "digest", ] @@ -1385,12 +1377,6 @@ version = "2.7.4" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "78ca9ab1a0babb1e7d5695e3530886289c18cf2f87ec19a575a0abdce112e3a3" -[[package]] -name = "memory_units" -version = "0.4.0" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "8452105ba047068f40ff7093dd1d9da90898e63dd61736462e9cdda6a90ad3c3" - [[package]] name = "mime" version = "0.3.17" @@ -1550,7 +1536,7 @@ version = "0.9.10" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "1e401f977ab385c9e4e3ab30627d6f26d00e2c73eef317493c4ec6d468726cf8" dependencies = [ - "cfg-if 1.0.0", + "cfg-if", "libc", "redox_syscall", "smallvec", @@ -1794,7 +1780,7 @@ source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "c17fa4cb658e3583423e915b9f3acc01cceaee1860e33d59ebae66adc3a2dc0d" dependencies = [ "cc", - "cfg-if 1.0.0", + "cfg-if", "getrandom", "libc", "spin", @@ -1842,9 +1828,9 @@ checksum = "719b953e2095829ee67db738b3bfa9fa368c94900df327b3f07fe6e794d2fe1f" [[package]] name = "rustix" -version = "0.38.38" +version = "0.38.39" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "aa260229e6538e52293eeb577aabd09945a09d6d9cc0fc550ed7529056c2e32a" +checksum = "375116bee2be9ed569afe2154ea6a99dfdffd257f533f187498c2a8f5feaf4ee" dependencies = [ "bitflags", "errno", @@ -1972,7 +1958,7 @@ version = "0.10.6" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "e3bf829a2d51ab4a5ddf1352d8470c140cadc8301b2ae1789db023f01cedd6ba" dependencies = [ - "cfg-if 1.0.0", + "cfg-if", "cpufeatures", "digest", ] @@ -1983,7 +1969,7 @@ version = "0.10.8" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "793db75ad2bcafc3ffa7c68b215fee268f537982cd901d132f89c6343f3a3dc8" dependencies = [ - "cfg-if 1.0.0", + "cfg-if", "cpufeatures", "digest", ] @@ -2300,7 +2286,7 @@ source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "799c883d55abdb5e98af1a7b3f23b9b6de8ecada0ecac058672d7635eb48ca7b" dependencies = [ "cc", - "cfg-if 1.0.0", + "cfg-if", "libc", "psm", "windows-sys 0.59.0", @@ -2369,7 +2355,7 @@ version = "3.13.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "f0f2c9fc62d0beef6951ccffd757e241266a2c833136efbe35af6cd2567dca5b" dependencies = [ - "cfg-if 1.0.0", + "cfg-if", "fastrand", "once_cell", "rustix", @@ -2378,18 +2364,18 @@ dependencies = [ [[package]] name = "thiserror" -version = "1.0.66" +version = "1.0.68" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "5d171f59dbaa811dbbb1aee1e73db92ec2b122911a48e1390dfe327a821ddede" +checksum = "02dd99dc800bbb97186339685293e1cc5d9df1f8fae2d0aecd9ff1c77efea892" dependencies = [ "thiserror-impl", ] [[package]] name = "thiserror-impl" -version = "1.0.66" +version = "1.0.68" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "b08be0f17bd307950653ce45db00cd31200d82b624b36e181337d9c7d92765b5" +checksum = "a7c61ec9a6f64d2793d8a45faba21efbe3ced62a886d44c36a009b2b519b4c7e" dependencies = [ "proc-macro2", "quote", @@ -2402,7 +2388,7 @@ version = "1.1.8" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "8b9ef9bad013ada3808854ceac7b46812a6465ba368859a37e2100283d2d719c" dependencies = [ - "cfg-if 1.0.0", + "cfg-if", "once_cell", ] @@ -2712,12 +2698,12 @@ checksum = "8ecb6da28b8a351d773b68d5825ac39017e680750f980f3a1a85cd8dd28a47c1" [[package]] name = "url" -version = "2.5.2" +version = "2.5.3" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "22784dbdf76fdde8af1aeda5622b546b422b6fc585325248a2bf9f5e41e94d6c" +checksum = "8d157f1b96d14500ffdc1f10ba712e780825526c03d9a49b4d0324b0d9113ada" dependencies = [ "form_urlencoded", - "idna 0.5.0", + "idna", "percent-encoding", ] @@ -2775,7 +2761,7 @@ version = "0.2.95" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "128d1e363af62632b8eb57219c8fd7877144af57558fb2ef0368d0087bddeb2e" dependencies = [ - "cfg-if 1.0.0", + "cfg-if", "once_cell", "wasm-bindgen-macro", ] @@ -2843,18 +2829,6 @@ dependencies = [ "rustls-pki-types", ] -[[package]] -name = "wee_alloc" -version = "0.4.5" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "dbb3b5a6b2bb17cb6ad44a2e68a43e8d2722c997da10e928665c72ec6c0a0b8e" -dependencies = [ - "cfg-if 0.1.10", - "libc", - "memory_units", - "winapi", -] - [[package]] name = "whoami" version = "1.5.2" diff --git a/README.md b/README.md index 338aba4..d76bbee 100644 --- a/README.md +++ b/README.md @@ -39,6 +39,10 @@ Autoreload: https://actix.rs/docs/autoreload/ ## WebAssembly 'frontend' project needs some tools to generate the WebAssembly stuff. Everything is explained here: https://rustwasm.github.io/wasm-bindgen/examples/hello-world.html +TODO MVC Example: + https://github.com/rustwasm/wasm-bindgen/tree/main/examples/todomvc -> https://rustwasm.github.io/wasm-bindgen/exbuild/todomvc/#/ + +Javascript bundler: https://rspack.dev/ To compile run 'wasm-pack build' in 'frontend' directory To launch node run 'npm run start' in 'frontend/www' directory diff --git a/TODO.md b/TODO.md index f26af2a..a176eb8 100644 --- a/TODO.md +++ b/TODO.md @@ -1,8 +1,3 @@ -* How to log error to journalctl or elsewhere + debug log? -* Try using WASM for all the client logic (test on editing/creating a recipe) - * Understand the example here: - * https://github.com/rustwasm/wasm-bindgen/tree/main/examples/todomvc -> https://rustwasm.github.io/wasm-bindgen/exbuild/todomvc/#/ -* Add a level of severity for the message template, use error severity in "impl axum::response::IntoResponse for db::DBError" * Review the recipe model (SQL) * Describe the use cases in details. * Define the UI (mockups). @@ -16,6 +11,8 @@ .service(services::webapi::set_recipe_description) * Add support to translations into db model. +[ok] Try using WASM for all the client logic (test on editing/creating a recipe) +[ok] How to log error to journalctl or elsewhere + debug log? [ok] Clean the old code + commit [ok] Reactivate sign up/in/out [ok] Change all id to i64 diff --git a/frontend/Cargo.toml b/frontend/Cargo.toml index c555ff7..267ea63 100644 --- a/frontend/Cargo.toml +++ b/frontend/Cargo.toml @@ -33,7 +33,7 @@ console_error_panic_hook = { version = "0.1", optional = true } # `wee_alloc` is a tiny allocator for wasm that is only ~1K in code size # compared to the default allocator's ~10K. It is slower than the default # allocator, however. -wee_alloc = { version = "0.4", optional = true } +# wee_alloc = { version = "0.4", optional = true } # [dev-dependencies] # wasm-bindgen-test = "0.3" diff --git a/frontend/deploy.nu b/frontend/deploy.nu index 88e5b84..ca50923 100644 --- a/frontend/deploy.nu +++ b/frontend/deploy.nu @@ -1,3 +1,3 @@ -wasm-pack build --target web -- --features wee_alloc +wasm-pack build --target web cp pkg/frontend.js ../backend/static cp pkg/frontend_bg.wasm ../backend/static \ No newline at end of file diff --git a/frontend/package.json b/frontend/package.json index ed1f2aa..682de5c 100644 --- a/frontend/package.json +++ b/frontend/package.json @@ -1,14 +1,12 @@ { "scripts": { - "build": "webpack", - "serve": "webpack serve" + "build": "rspack build", + "serve": "rspack serve" }, "devDependencies": { + "@rspack/cli": "^1.0.14", + "@rspack/core": "^1.0.14", "@wasm-tool/wasm-pack-plugin": "1.5.0", - "html-webpack-plugin": "^5.3.2", - "text-encoding": "^0.7.0", - "webpack": "^5.75.0", - "webpack-cli": "^4.10.0", - "webpack-dev-server": "^3.11.2" + "html-webpack-plugin": "^5.6.0" } -} +} \ No newline at end of file diff --git a/frontend/webpack.config.js b/frontend/rspack.config.js similarity index 66% rename from frontend/webpack.config.js rename to frontend/rspack.config.js index 40448a4..75e9eb7 100644 --- a/frontend/webpack.config.js +++ b/frontend/rspack.config.js @@ -14,15 +14,9 @@ module.exports = { new WasmPackPlugin({ crateDirectory: path.resolve(__dirname, ".") }), - // Have this example work in Edge which doesn't ship `TextEncoder` or - // `TextDecoder` at this time. - new webpack.ProvidePlugin({ - TextDecoder: ['text-encoding', 'TextDecoder'], - TextEncoder: ['text-encoding', 'TextEncoder'] - }) ], mode: 'development', experiments: { asyncWebAssembly: true - } + } }; \ No newline at end of file diff --git a/frontend/src/handles.rs b/frontend/src/handles.rs index c665faf..55a927c 100644 --- a/frontend/src/handles.rs +++ b/frontend/src/handles.rs @@ -1,4 +1,6 @@ use wasm_bindgen::prelude::*; use web_sys::Document; -pub fn edit_recipe(doc: &Document) {} +pub fn edit_recipe(doc: &Document) { + let title_input = doc.get_element_by_id("title_field").unwrap(); +} diff --git a/frontend/src/lib.rs b/frontend/src/lib.rs index 9d8d431..ddbe956 100644 --- a/frontend/src/lib.rs +++ b/frontend/src/lib.rs @@ -4,12 +4,6 @@ mod utils; use wasm_bindgen::prelude::*; use web_sys::console; -// When the `wee_alloc` feature is enabled, use `wee_alloc` as the global -// allocator. -#[cfg(feature = "wee_alloc")] -#[global_allocator] -static ALLOC: wee_alloc::WeeAlloc = wee_alloc::WeeAlloc::INIT; - #[wasm_bindgen] extern "C" { fn alert(s: &str); @@ -27,7 +21,7 @@ pub fn main() -> Result<(), JsValue> { let window = web_sys::window().expect("no global `window` exists"); let document = window.document().expect("should have a document on window"); - //let body = document.body().expect("document should have a body"); + // let body = document.body().expect("document should have a body"); let location = window.location().pathname()?; let path: Vec<&str> = location.split('/').skip(1).collect(); @@ -46,14 +40,10 @@ pub fn main() -> Result<(), JsValue> { console_log!("recipe edit ID: {}", id); handles::edit_recipe(&document); - - let title_input = document.get_element_by_id("title_field").unwrap(); } _ => (), } - //alert(&path); - // TEST // let val = document.create_element("p")?; // val.set_inner_html("Hello from Rust!"); diff --git a/frontend/start.nu b/frontend/start.nu index eb1cffd..ede7328 100644 --- a/frontend/start.nu +++ b/frontend/start.nu @@ -1 +1,2 @@ +# See 'package.json'. npm run serve \ No newline at end of file -- 2.45.2