From dfcc795f25b5efec058dc85de06d279af735068b Mon Sep 17 00:00:00 2001 From: hulxv Date: Mon, 30 Sep 2024 00:46:59 +0300 Subject: [PATCH] fix(bundler): return an error when targets not found --- crates/metassr-build/src/client/mod.rs | 2 +- crates/metassr-build/src/server/mod.rs | 11 +- .../metassr-build/src/server/renderer/head.rs | 5 +- crates/metassr-bundler/src/lib.rs | 65 ++++++-- crates/metassr-bundler/tests/home.js | 20 +++ crates/metassr-bundler/tests/package.json | 13 ++ crates/metassr-bundler/tests/yarn.lock | 145 ++++++++++++++++++ 7 files changed, 240 insertions(+), 21 deletions(-) create mode 100644 crates/metassr-bundler/tests/home.js create mode 100644 crates/metassr-bundler/tests/package.json create mode 100644 crates/metassr-bundler/tests/yarn.lock diff --git a/crates/metassr-build/src/client/mod.rs b/crates/metassr-build/src/client/mod.rs index 482db39..7fecef5 100644 --- a/crates/metassr-build/src/client/mod.rs +++ b/crates/metassr-build/src/client/mod.rs @@ -72,7 +72,7 @@ impl Build for ClientBuilder { }) .collect::>(); - let bundler = WebBundler::new(&targets, &self.dist_path); + let bundler = WebBundler::new(&targets, &self.dist_path)?; if let Err(e) = bundler.exec() { return Err(anyhow!("Bundling failed: {e}")); } diff --git a/crates/metassr-build/src/server/mod.rs b/crates/metassr-build/src/server/mod.rs index 77b4cf7..3d1346e 100644 --- a/crates/metassr-build/src/server/mod.rs +++ b/crates/metassr-build/src/server/mod.rs @@ -78,12 +78,13 @@ impl Build for ServerSideBuilder { Err(e) => return Err(anyhow!("Couldn't generate targets: {e}")), }; - if let Err(e) = WebBundler::new( - &targets.ready_for_bundling(&self.dist_path), + let bundling_targets = targets.ready_for_bundling(&self.dist_path); + let bundler = WebBundler::new( + &bundling_targets, &self.dist_path, - ) - .exec() - { + )?; + + if let Err(e) = bundler.exec() { return Err(anyhow!("Bundling failed: {e}")); } diff --git a/crates/metassr-build/src/server/renderer/head.rs b/crates/metassr-build/src/server/renderer/head.rs index 63f5e9f..7dd3533 100644 --- a/crates/metassr-build/src/server/renderer/head.rs +++ b/crates/metassr-build/src/server/renderer/head.rs @@ -48,7 +48,10 @@ impl HeadRenderer { } fn bundle(&mut self) -> Result<()> { - if let Err(e) = WebBundler::new(&self.bundling_target()?, &self.cache_dir.path()).exec() + let bundling_targets = self.bundling_target()?; + let bundler = WebBundler::new(&bundling_targets, self.cache_dir.path())?; + + if let Err(e) = bundler.exec() { return Err(anyhow!("Cannot bundling head: {e}")); } diff --git a/crates/metassr-bundler/src/lib.rs b/crates/metassr-bundler/src/lib.rs index 8241a61..8340f9b 100644 --- a/crates/metassr-bundler/src/lib.rs +++ b/crates/metassr-bundler/src/lib.rs @@ -57,19 +57,33 @@ impl<'a> WebBundler<'a> { /// - `dist_path`: The path to the directory where the bundled output should be saved. /// /// Returns a `WebBundler` struct. - pub fn new(targets: &'a HashMap, dist_path: &'a S) -> Self + pub fn new(targets: &'a HashMap, dist_path: &'a S) -> Result where S: AsRef + ?Sized, { + let mut non_found_files = vec![]; let targets: HashMap = targets .iter() - .map(|(k, v)| (k.into(), Path::new(v))) + .map(|(k, path)| { + let path = Path::new(path); + if !path.exists() { + non_found_files.push(path.to_str().unwrap()); + } + (k.into(), path) + }) .collect(); - Self { + if non_found_files.len() > 0 { + return Err(anyhow!( + "[bundler] Non Exist files found: {:?}", + non_found_files + )); + } + + Ok(Self { targets, dist_path: Path::new(dist_path), - } + }) } /// Executes the bundling process by invoking the `web_bundling` function from `bundle.js` via MetaCall. @@ -147,19 +161,42 @@ impl<'a> WebBundler<'a> { #[cfg(test)] mod tests { + use super::*; use metacall::switch; + + fn clean() { + let dist = Path::new("test/dist"); + if dist.exists() { + std::fs::remove_dir_all(dist).unwrap(); + } + } + #[test] - fn it_works() { + fn bundling_works() { + clean(); let _metacall = switch::initialize().unwrap(); - WebBundler::new( - &HashMap::from([( - "pages/homes.tsx".to_owned(), - "../../tests/web-app/src/pages/home.tsx".to_owned(), - )]), - "../../tests/web-app/dist", - ) - .exec() - .unwrap() + let targets = HashMap::from([("pages/home".to_owned(), "./tests/home.js".to_owned())]); + + match WebBundler::new(&targets, "tests/dist") { + Ok(bundler) => { + assert!(bundler.exec().is_ok()); + assert!(Path::new("tests/dist/pages/home.js").exists()); + } + Err(err) => { + panic!("BUNDLING TEST FAILED: {err:?}",) + } + } + clean(); + } + + #[test] + fn invalid_target_fails() { + clean(); + let _metacall = switch::initialize().unwrap(); + let targets = HashMap::from([("invalid_path.tsx".to_owned(), "invalid_path".to_owned())]); + + let bundler = WebBundler::new(&targets, "tests/dist"); + assert!(bundler.is_err()); } } diff --git a/crates/metassr-bundler/tests/home.js b/crates/metassr-bundler/tests/home.js new file mode 100644 index 0000000..546f108 --- /dev/null +++ b/crates/metassr-bundler/tests/home.js @@ -0,0 +1,20 @@ +import React, { useState, ReactNode } from 'react'; + +export default function Home() { + let [counter, setCounter] = useState(0); + + return ( +
+
This is a simple home page contains a counter
+ +

{counter}

+ + +
+ ) + +} + + diff --git a/crates/metassr-bundler/tests/package.json b/crates/metassr-bundler/tests/package.json new file mode 100644 index 0000000..ab8f036 --- /dev/null +++ b/crates/metassr-bundler/tests/package.json @@ -0,0 +1,13 @@ +{ + "name": "tests", + "version": "1.0.0", + "main": "index.js", + "license": "MIT", + "dependencies": { + "react": "^18.3.1", + "react-dom": "^18.3.1" + }, + "devDependencies": { + "@rspack/core": "^1.0.7" + } +} diff --git a/crates/metassr-bundler/tests/yarn.lock b/crates/metassr-bundler/tests/yarn.lock new file mode 100644 index 0000000..9622543 --- /dev/null +++ b/crates/metassr-bundler/tests/yarn.lock @@ -0,0 +1,145 @@ +# THIS IS AN AUTOGENERATED FILE. DO NOT EDIT THIS FILE DIRECTLY. +# yarn lockfile v1 + + +"@module-federation/runtime-tools@0.5.1": + version "0.5.1" + resolved "https://registry.yarnpkg.com/@module-federation/runtime-tools/-/runtime-tools-0.5.1.tgz#1b1f93837159a6bf0c0ba78730d589a5a8f74aa3" + integrity sha512-nfBedkoZ3/SWyO0hnmaxuz0R0iGPSikHZOAZ0N/dVSQaIzlffUo35B5nlC2wgWIc0JdMZfkwkjZRrnuuDIJbzg== + dependencies: + "@module-federation/runtime" "0.5.1" + "@module-federation/webpack-bundler-runtime" "0.5.1" + +"@module-federation/runtime@0.5.1": + version "0.5.1" + resolved "https://registry.yarnpkg.com/@module-federation/runtime/-/runtime-0.5.1.tgz#b548a75e2068952ff66ad717cbf73fc921edd5d7" + integrity sha512-xgiMUWwGLWDrvZc9JibuEbXIbhXg6z2oUkemogSvQ4LKvrl/n0kbqP1Blk669mXzyWbqtSp6PpvNdwaE1aN5xQ== + dependencies: + "@module-federation/sdk" "0.5.1" + +"@module-federation/sdk@0.5.1": + version "0.5.1" + resolved "https://registry.yarnpkg.com/@module-federation/sdk/-/sdk-0.5.1.tgz#6c0a4053c23fa84db7aae7e4736496c541de7191" + integrity sha512-exvchtjNURJJkpqjQ3/opdbfeT2wPKvrbnGnyRkrwW5o3FH1LaST1tkiNviT6OXTexGaVc2DahbdniQHVtQ7pA== + +"@module-federation/webpack-bundler-runtime@0.5.1": + version "0.5.1" + resolved "https://registry.yarnpkg.com/@module-federation/webpack-bundler-runtime/-/webpack-bundler-runtime-0.5.1.tgz#ef626af0d57e3568c474d66d7d3797366e09cafd" + integrity sha512-mMhRFH0k2VjwHt3Jol9JkUsmI/4XlrAoBG3E0o7HoyoPYv1UFOWyqAflfANcUPgbYpvqmyLzDcO+3IT36LXnrA== + dependencies: + "@module-federation/runtime" "0.5.1" + "@module-federation/sdk" "0.5.1" + +"@rspack/binding-darwin-arm64@1.0.7": + version "1.0.7" + resolved "https://registry.yarnpkg.com/@rspack/binding-darwin-arm64/-/binding-darwin-arm64-1.0.7.tgz#22b2c1aad32f65eee6806cf95db2e62ced215ee7" + integrity sha512-VnkgTM2OJzWTJxiWxLeUpRGumDp0XAsjU9/DL1PBjw1W+1exyGc2QST8q+mxsgDPDl9u1rjJa6UN4oboBbl47Q== + +"@rspack/binding-darwin-x64@1.0.7": + version "1.0.7" + resolved "https://registry.yarnpkg.com/@rspack/binding-darwin-x64/-/binding-darwin-x64-1.0.7.tgz#c4185bba4e2ed324063e19219217f5f13ff5b35d" + integrity sha512-43v660eofqzRVtTVddl28K5550E1gCeWIc8WRUAxJoL4QTzoo8M3iGnU8CquKG6phat5Iug8mJ0OIUfqHGK8YQ== + +"@rspack/binding-linux-arm64-gnu@1.0.7": + version "1.0.7" + resolved "https://registry.yarnpkg.com/@rspack/binding-linux-arm64-gnu/-/binding-linux-arm64-gnu-1.0.7.tgz#909747e44f0ffc6426cb0b67ab4cb7f540d60368" + integrity sha512-FHS1cU5MzXijVKQ7xW2Rpp0wvtN0BQYbouT3Yx6DNrdtE3P4i/XHnol8zdHkpHeSCOP/p0bnyO+Q/BbXXr4XSw== + +"@rspack/binding-linux-arm64-musl@1.0.7": + version "1.0.7" + resolved "https://registry.yarnpkg.com/@rspack/binding-linux-arm64-musl/-/binding-linux-arm64-musl-1.0.7.tgz#26c60907e2ff9997b243d6cadfad14b49f08dba7" + integrity sha512-WT+h3fpEWY+60UqiTcwTq6jq6NFhZdZW+Onb3QHQ9F9myWOemM5z5GF8rxWKTn6PHOMz01o/cF4ikMQRfC/sEA== + +"@rspack/binding-linux-x64-gnu@1.0.7": + version "1.0.7" + resolved "https://registry.yarnpkg.com/@rspack/binding-linux-x64-gnu/-/binding-linux-x64-gnu-1.0.7.tgz#ad477d407310ca3d8566206cbc81a51f0658af68" + integrity sha512-l2ORrXY+dG7n/yog5tGwk3NZLLWh/jz88+74IQkVBX1wRViJt1cJZE9wDqoCLhT6dgCreIUZ1s5UghuAR6ymYQ== + +"@rspack/binding-linux-x64-musl@1.0.7": + version "1.0.7" + resolved "https://registry.yarnpkg.com/@rspack/binding-linux-x64-musl/-/binding-linux-x64-musl-1.0.7.tgz#e25d52f87f17207dd0fb2383905981d775e1d702" + integrity sha512-k1qtanXpQiGk/h2UfVDxLqzkS/LHj5i4AlMTV6q/CIBUjTOrwO4yFWCCC8JLw4vK6rT/YK/Ib4nwy1XRyVY+dQ== + +"@rspack/binding-win32-arm64-msvc@1.0.7": + version "1.0.7" + resolved "https://registry.yarnpkg.com/@rspack/binding-win32-arm64-msvc/-/binding-win32-arm64-msvc-1.0.7.tgz#1a55c1a50e2cf49d40fd4f21c0a8574be2de3ef9" + integrity sha512-WqdaSOo6uGy1c4AkmvcJTtjJT9F7/c5dNTUCTXWAFzh9V1k1X5tpPCxFFB/PpWov59esywkV2ZRIgDypBf3PLg== + +"@rspack/binding-win32-ia32-msvc@1.0.7": + version "1.0.7" + resolved "https://registry.yarnpkg.com/@rspack/binding-win32-ia32-msvc/-/binding-win32-ia32-msvc-1.0.7.tgz#643d5f0705b74934823ab5a56733915e52336889" + integrity sha512-H9/U63KyIVlmmb34pRsX45Q0sdSqST22+za67dwEZicx5DjswGHQlkcdWZPmvsquACUG/ZyJf+tOzR3tm/sE5Q== + +"@rspack/binding-win32-x64-msvc@1.0.7": + version "1.0.7" + resolved "https://registry.yarnpkg.com/@rspack/binding-win32-x64-msvc/-/binding-win32-x64-msvc-1.0.7.tgz#f26ce9f308d94b7c54cc107c0820273c6ece3528" + integrity sha512-T6E00hKhgfXLkWmkmzyxYl/jKWot0PE2U4/ciGlBVQtDyjc50WPWnRREyPAEv7ozgAqou+ugd5KN+O6SFz4NbQ== + +"@rspack/binding@1.0.7": + version "1.0.7" + resolved "https://registry.yarnpkg.com/@rspack/binding/-/binding-1.0.7.tgz#355cf2e5eabe60648a58da9bc6c5eecdbb11608c" + integrity sha512-lSjxstfgtesIj1A0pk1W99iTIyDyfv/HXveyV3x+C+62pv0i0jj9tJUDmFM1gGwitKzm1LV9DgW/sOuzz3NtNw== + optionalDependencies: + "@rspack/binding-darwin-arm64" "1.0.7" + "@rspack/binding-darwin-x64" "1.0.7" + "@rspack/binding-linux-arm64-gnu" "1.0.7" + "@rspack/binding-linux-arm64-musl" "1.0.7" + "@rspack/binding-linux-x64-gnu" "1.0.7" + "@rspack/binding-linux-x64-musl" "1.0.7" + "@rspack/binding-win32-arm64-msvc" "1.0.7" + "@rspack/binding-win32-ia32-msvc" "1.0.7" + "@rspack/binding-win32-x64-msvc" "1.0.7" + +"@rspack/core@^1.0.7": + version "1.0.7" + resolved "https://registry.yarnpkg.com/@rspack/core/-/core-1.0.7.tgz#7e9905841f9e9904678c3282f1f062ae1e98669f" + integrity sha512-L3O0GDKasMmLtDkZrbfJI8OFJ5hmlLannoJ2hDR3LMq8tC/q0jIXTL7YIo7rStsudecCkcD7CH/Smm00itZSzg== + dependencies: + "@module-federation/runtime-tools" "0.5.1" + "@rspack/binding" "1.0.7" + "@rspack/lite-tapable" "1.0.0" + caniuse-lite "^1.0.30001616" + +"@rspack/lite-tapable@1.0.0": + version "1.0.0" + resolved "https://registry.yarnpkg.com/@rspack/lite-tapable/-/lite-tapable-1.0.0.tgz#160883693462f164d0e6ede22f32614673ef6429" + integrity sha512-7MZf4lburSUZoEenwazwUDKHhqyfnLCGnQ/tKcUtztfmVzfjZfRn/EaiT0AKkYGnL2U8AGsw89oUeVyvaOLVCw== + +caniuse-lite@^1.0.30001616: + version "1.0.30001664" + resolved "https://registry.yarnpkg.com/caniuse-lite/-/caniuse-lite-1.0.30001664.tgz#d588d75c9682d3301956b05a3749652a80677df4" + integrity sha512-AmE7k4dXiNKQipgn7a2xg558IRqPN3jMQY/rOsbxDhrd0tyChwbITBfiwtnqz8bi2M5mIWbxAYBvk7W7QBUS2g== + +"js-tokens@^3.0.0 || ^4.0.0": + version "4.0.0" + resolved "https://registry.yarnpkg.com/js-tokens/-/js-tokens-4.0.0.tgz#19203fb59991df98e3a287050d4647cdeaf32499" + integrity sha512-RdJUflcE3cUzKiMqQgsCu06FPu9UdIJO0beYbPhHN4k6apgJtifcoCtT9bcxOpYBtpD2kCM6Sbzg4CausW/PKQ== + +loose-envify@^1.1.0: + version "1.4.0" + resolved "https://registry.yarnpkg.com/loose-envify/-/loose-envify-1.4.0.tgz#71ee51fa7be4caec1a63839f7e682d8132d30caf" + integrity sha512-lyuxPGr/Wfhrlem2CL/UcnUc1zcqKAImBDzukY7Y5F/yQiNdko6+fRLevlw1HgMySw7f611UIY408EtxRSoK3Q== + dependencies: + js-tokens "^3.0.0 || ^4.0.0" + +react-dom@^18.3.1: + version "18.3.1" + resolved "https://registry.yarnpkg.com/react-dom/-/react-dom-18.3.1.tgz#c2265d79511b57d479b3dd3fdfa51536494c5cb4" + integrity sha512-5m4nQKp+rZRb09LNH59GM4BxTh9251/ylbKIbpe7TpGxfJ+9kv6BLkLBXIjjspbgbnIBNqlI23tRnTWT0snUIw== + dependencies: + loose-envify "^1.1.0" + scheduler "^0.23.2" + +react@^18.3.1: + version "18.3.1" + resolved "https://registry.yarnpkg.com/react/-/react-18.3.1.tgz#49ab892009c53933625bd16b2533fc754cab2891" + integrity sha512-wS+hAgJShR0KhEvPJArfuPVN1+Hz1t0Y6n5jLrGQbkb4urgPE/0Rve+1kMB1v/oWgHgm4WIcV+i7F2pTVj+2iQ== + dependencies: + loose-envify "^1.1.0" + +scheduler@^0.23.2: + version "0.23.2" + resolved "https://registry.yarnpkg.com/scheduler/-/scheduler-0.23.2.tgz#414ba64a3b282892e944cf2108ecc078d115cdc3" + integrity sha512-UOShsPwz7NrMUqhR6t0hWjFduvOzbtv7toDH1/hIrfRNIDBnnBWd0CwJTGvTpngVlmwGCdP9/Zl/tVrDqcuYzQ== + dependencies: + loose-envify "^1.1.0"