Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(bundler): return an error when targets not found #30

Merged
merged 1 commit into from
Sep 29, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion crates/metassr-build/src/client/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,7 @@ impl Build for ClientBuilder {
})
.collect::<HashMap<String, String>>();

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}"));
}
Expand Down
11 changes: 6 additions & 5 deletions crates/metassr-build/src/server/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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}"));
}

Expand Down
5 changes: 4 additions & 1 deletion crates/metassr-build/src/server/renderer/head.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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}"));
}
Expand Down
65 changes: 51 additions & 14 deletions crates/metassr-bundler/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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<S>(targets: &'a HashMap<String, String>, dist_path: &'a S) -> Self
pub fn new<S>(targets: &'a HashMap<String, String>, dist_path: &'a S) -> Result<Self>
where
S: AsRef<OsStr> + ?Sized,
{
let mut non_found_files = vec![];
let targets: HashMap<String, &Path> = 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.
Expand Down Expand Up @@ -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());
}
}
20 changes: 20 additions & 0 deletions crates/metassr-bundler/tests/home.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
import React, { useState, ReactNode } from 'react';

export default function Home() {
let [counter, setCounter] = useState(0);

return (
<div>
<div className="text-4xl font-bold">This is a simple home page contains a counter</div>

<h1 className="text-4xl font-bold">{counter}</h1>
<button onClick={() => { setCounter(counter + 1); }}>
Click me :D
</button>

</div>
)

}


13 changes: 13 additions & 0 deletions crates/metassr-bundler/tests/package.json
Original file line number Diff line number Diff line change
@@ -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"
}
}
145 changes: 145 additions & 0 deletions crates/metassr-bundler/tests/yarn.lock
Original file line number Diff line number Diff line change
@@ -0,0 +1,145 @@
# THIS IS AN AUTOGENERATED FILE. DO NOT EDIT THIS FILE DIRECTLY.
# yarn lockfile v1


"@module-federation/[email protected]":
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/[email protected]":
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/[email protected]":
version "0.5.1"
resolved "https://registry.yarnpkg.com/@module-federation/sdk/-/sdk-0.5.1.tgz#6c0a4053c23fa84db7aae7e4736496c541de7191"
integrity sha512-exvchtjNURJJkpqjQ3/opdbfeT2wPKvrbnGnyRkrwW5o3FH1LaST1tkiNviT6OXTexGaVc2DahbdniQHVtQ7pA==

"@module-federation/[email protected]":
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/[email protected]":
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/[email protected]":
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/[email protected]":
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/[email protected]":
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/[email protected]":
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/[email protected]":
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/[email protected]":
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/[email protected]":
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/[email protected]":
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/[email protected]":
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/[email protected]":
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"
Loading