Skip to content

Commit 2cc747b

Browse files
authored
Merge pull request #30 from metacall/fix/bundler-targets-not-found
fix(bundler): return an error when targets not found
2 parents 836f05c + dfcc795 commit 2cc747b

File tree

7 files changed

+240
-21
lines changed

7 files changed

+240
-21
lines changed

crates/metassr-build/src/client/mod.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -72,7 +72,7 @@ impl Build for ClientBuilder {
7272
})
7373
.collect::<HashMap<String, String>>();
7474

75-
let bundler = WebBundler::new(&targets, &self.dist_path);
75+
let bundler = WebBundler::new(&targets, &self.dist_path)?;
7676
if let Err(e) = bundler.exec() {
7777
return Err(anyhow!("Bundling failed: {e}"));
7878
}

crates/metassr-build/src/server/mod.rs

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -78,12 +78,13 @@ impl Build for ServerSideBuilder {
7878
Err(e) => return Err(anyhow!("Couldn't generate targets: {e}")),
7979
};
8080

81-
if let Err(e) = WebBundler::new(
82-
&targets.ready_for_bundling(&self.dist_path),
81+
let bundling_targets = targets.ready_for_bundling(&self.dist_path);
82+
let bundler = WebBundler::new(
83+
&bundling_targets,
8384
&self.dist_path,
84-
)
85-
.exec()
86-
{
85+
)?;
86+
87+
if let Err(e) = bundler.exec() {
8788
return Err(anyhow!("Bundling failed: {e}"));
8889
}
8990

crates/metassr-build/src/server/renderer/head.rs

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,10 @@ impl HeadRenderer {
4848
}
4949

5050
fn bundle(&mut self) -> Result<()> {
51-
if let Err(e) = WebBundler::new(&self.bundling_target()?, &self.cache_dir.path()).exec()
51+
let bundling_targets = self.bundling_target()?;
52+
let bundler = WebBundler::new(&bundling_targets, self.cache_dir.path())?;
53+
54+
if let Err(e) = bundler.exec()
5255
{
5356
return Err(anyhow!("Cannot bundling head: {e}"));
5457
}

crates/metassr-bundler/src/lib.rs

Lines changed: 51 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -57,19 +57,33 @@ impl<'a> WebBundler<'a> {
5757
/// - `dist_path`: The path to the directory where the bundled output should be saved.
5858
///
5959
/// Returns a `WebBundler` struct.
60-
pub fn new<S>(targets: &'a HashMap<String, String>, dist_path: &'a S) -> Self
60+
pub fn new<S>(targets: &'a HashMap<String, String>, dist_path: &'a S) -> Result<Self>
6161
where
6262
S: AsRef<OsStr> + ?Sized,
6363
{
64+
let mut non_found_files = vec![];
6465
let targets: HashMap<String, &Path> = targets
6566
.iter()
66-
.map(|(k, v)| (k.into(), Path::new(v)))
67+
.map(|(k, path)| {
68+
let path = Path::new(path);
69+
if !path.exists() {
70+
non_found_files.push(path.to_str().unwrap());
71+
}
72+
(k.into(), path)
73+
})
6774
.collect();
6875

69-
Self {
76+
if non_found_files.len() > 0 {
77+
return Err(anyhow!(
78+
"[bundler] Non Exist files found: {:?}",
79+
non_found_files
80+
));
81+
}
82+
83+
Ok(Self {
7084
targets,
7185
dist_path: Path::new(dist_path),
72-
}
86+
})
7387
}
7488

7589
/// Executes the bundling process by invoking the `web_bundling` function from `bundle.js` via MetaCall.
@@ -147,19 +161,42 @@ impl<'a> WebBundler<'a> {
147161

148162
#[cfg(test)]
149163
mod tests {
164+
150165
use super::*;
151166
use metacall::switch;
167+
168+
fn clean() {
169+
let dist = Path::new("test/dist");
170+
if dist.exists() {
171+
std::fs::remove_dir_all(dist).unwrap();
172+
}
173+
}
174+
152175
#[test]
153-
fn it_works() {
176+
fn bundling_works() {
177+
clean();
154178
let _metacall = switch::initialize().unwrap();
155-
WebBundler::new(
156-
&HashMap::from([(
157-
"pages/homes.tsx".to_owned(),
158-
"../../tests/web-app/src/pages/home.tsx".to_owned(),
159-
)]),
160-
"../../tests/web-app/dist",
161-
)
162-
.exec()
163-
.unwrap()
179+
let targets = HashMap::from([("pages/home".to_owned(), "./tests/home.js".to_owned())]);
180+
181+
match WebBundler::new(&targets, "tests/dist") {
182+
Ok(bundler) => {
183+
assert!(bundler.exec().is_ok());
184+
assert!(Path::new("tests/dist/pages/home.js").exists());
185+
}
186+
Err(err) => {
187+
panic!("BUNDLING TEST FAILED: {err:?}",)
188+
}
189+
}
190+
clean();
191+
}
192+
193+
#[test]
194+
fn invalid_target_fails() {
195+
clean();
196+
let _metacall = switch::initialize().unwrap();
197+
let targets = HashMap::from([("invalid_path.tsx".to_owned(), "invalid_path".to_owned())]);
198+
199+
let bundler = WebBundler::new(&targets, "tests/dist");
200+
assert!(bundler.is_err());
164201
}
165202
}

crates/metassr-bundler/tests/home.js

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
import React, { useState, ReactNode } from 'react';
2+
3+
export default function Home() {
4+
let [counter, setCounter] = useState(0);
5+
6+
return (
7+
<div>
8+
<div className="text-4xl font-bold">This is a simple home page contains a counter</div>
9+
10+
<h1 className="text-4xl font-bold">{counter}</h1>
11+
<button onClick={() => { setCounter(counter + 1); }}>
12+
Click me :D
13+
</button>
14+
15+
</div>
16+
)
17+
18+
}
19+
20+
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
{
2+
"name": "tests",
3+
"version": "1.0.0",
4+
"main": "index.js",
5+
"license": "MIT",
6+
"dependencies": {
7+
"react": "^18.3.1",
8+
"react-dom": "^18.3.1"
9+
},
10+
"devDependencies": {
11+
"@rspack/core": "^1.0.7"
12+
}
13+
}
Lines changed: 145 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,145 @@
1+
# THIS IS AN AUTOGENERATED FILE. DO NOT EDIT THIS FILE DIRECTLY.
2+
# yarn lockfile v1
3+
4+
5+
"@module-federation/[email protected]":
6+
version "0.5.1"
7+
resolved "https://registry.yarnpkg.com/@module-federation/runtime-tools/-/runtime-tools-0.5.1.tgz#1b1f93837159a6bf0c0ba78730d589a5a8f74aa3"
8+
integrity sha512-nfBedkoZ3/SWyO0hnmaxuz0R0iGPSikHZOAZ0N/dVSQaIzlffUo35B5nlC2wgWIc0JdMZfkwkjZRrnuuDIJbzg==
9+
dependencies:
10+
"@module-federation/runtime" "0.5.1"
11+
"@module-federation/webpack-bundler-runtime" "0.5.1"
12+
13+
"@module-federation/[email protected]":
14+
version "0.5.1"
15+
resolved "https://registry.yarnpkg.com/@module-federation/runtime/-/runtime-0.5.1.tgz#b548a75e2068952ff66ad717cbf73fc921edd5d7"
16+
integrity sha512-xgiMUWwGLWDrvZc9JibuEbXIbhXg6z2oUkemogSvQ4LKvrl/n0kbqP1Blk669mXzyWbqtSp6PpvNdwaE1aN5xQ==
17+
dependencies:
18+
"@module-federation/sdk" "0.5.1"
19+
20+
"@module-federation/[email protected]":
21+
version "0.5.1"
22+
resolved "https://registry.yarnpkg.com/@module-federation/sdk/-/sdk-0.5.1.tgz#6c0a4053c23fa84db7aae7e4736496c541de7191"
23+
integrity sha512-exvchtjNURJJkpqjQ3/opdbfeT2wPKvrbnGnyRkrwW5o3FH1LaST1tkiNviT6OXTexGaVc2DahbdniQHVtQ7pA==
24+
25+
"@module-federation/[email protected]":
26+
version "0.5.1"
27+
resolved "https://registry.yarnpkg.com/@module-federation/webpack-bundler-runtime/-/webpack-bundler-runtime-0.5.1.tgz#ef626af0d57e3568c474d66d7d3797366e09cafd"
28+
integrity sha512-mMhRFH0k2VjwHt3Jol9JkUsmI/4XlrAoBG3E0o7HoyoPYv1UFOWyqAflfANcUPgbYpvqmyLzDcO+3IT36LXnrA==
29+
dependencies:
30+
"@module-federation/runtime" "0.5.1"
31+
"@module-federation/sdk" "0.5.1"
32+
33+
34+
version "1.0.7"
35+
resolved "https://registry.yarnpkg.com/@rspack/binding-darwin-arm64/-/binding-darwin-arm64-1.0.7.tgz#22b2c1aad32f65eee6806cf95db2e62ced215ee7"
36+
integrity sha512-VnkgTM2OJzWTJxiWxLeUpRGumDp0XAsjU9/DL1PBjw1W+1exyGc2QST8q+mxsgDPDl9u1rjJa6UN4oboBbl47Q==
37+
38+
39+
version "1.0.7"
40+
resolved "https://registry.yarnpkg.com/@rspack/binding-darwin-x64/-/binding-darwin-x64-1.0.7.tgz#c4185bba4e2ed324063e19219217f5f13ff5b35d"
41+
integrity sha512-43v660eofqzRVtTVddl28K5550E1gCeWIc8WRUAxJoL4QTzoo8M3iGnU8CquKG6phat5Iug8mJ0OIUfqHGK8YQ==
42+
43+
44+
version "1.0.7"
45+
resolved "https://registry.yarnpkg.com/@rspack/binding-linux-arm64-gnu/-/binding-linux-arm64-gnu-1.0.7.tgz#909747e44f0ffc6426cb0b67ab4cb7f540d60368"
46+
integrity sha512-FHS1cU5MzXijVKQ7xW2Rpp0wvtN0BQYbouT3Yx6DNrdtE3P4i/XHnol8zdHkpHeSCOP/p0bnyO+Q/BbXXr4XSw==
47+
48+
49+
version "1.0.7"
50+
resolved "https://registry.yarnpkg.com/@rspack/binding-linux-arm64-musl/-/binding-linux-arm64-musl-1.0.7.tgz#26c60907e2ff9997b243d6cadfad14b49f08dba7"
51+
integrity sha512-WT+h3fpEWY+60UqiTcwTq6jq6NFhZdZW+Onb3QHQ9F9myWOemM5z5GF8rxWKTn6PHOMz01o/cF4ikMQRfC/sEA==
52+
53+
54+
version "1.0.7"
55+
resolved "https://registry.yarnpkg.com/@rspack/binding-linux-x64-gnu/-/binding-linux-x64-gnu-1.0.7.tgz#ad477d407310ca3d8566206cbc81a51f0658af68"
56+
integrity sha512-l2ORrXY+dG7n/yog5tGwk3NZLLWh/jz88+74IQkVBX1wRViJt1cJZE9wDqoCLhT6dgCreIUZ1s5UghuAR6ymYQ==
57+
58+
59+
version "1.0.7"
60+
resolved "https://registry.yarnpkg.com/@rspack/binding-linux-x64-musl/-/binding-linux-x64-musl-1.0.7.tgz#e25d52f87f17207dd0fb2383905981d775e1d702"
61+
integrity sha512-k1qtanXpQiGk/h2UfVDxLqzkS/LHj5i4AlMTV6q/CIBUjTOrwO4yFWCCC8JLw4vK6rT/YK/Ib4nwy1XRyVY+dQ==
62+
63+
64+
version "1.0.7"
65+
resolved "https://registry.yarnpkg.com/@rspack/binding-win32-arm64-msvc/-/binding-win32-arm64-msvc-1.0.7.tgz#1a55c1a50e2cf49d40fd4f21c0a8574be2de3ef9"
66+
integrity sha512-WqdaSOo6uGy1c4AkmvcJTtjJT9F7/c5dNTUCTXWAFzh9V1k1X5tpPCxFFB/PpWov59esywkV2ZRIgDypBf3PLg==
67+
68+
69+
version "1.0.7"
70+
resolved "https://registry.yarnpkg.com/@rspack/binding-win32-ia32-msvc/-/binding-win32-ia32-msvc-1.0.7.tgz#643d5f0705b74934823ab5a56733915e52336889"
71+
integrity sha512-H9/U63KyIVlmmb34pRsX45Q0sdSqST22+za67dwEZicx5DjswGHQlkcdWZPmvsquACUG/ZyJf+tOzR3tm/sE5Q==
72+
73+
74+
version "1.0.7"
75+
resolved "https://registry.yarnpkg.com/@rspack/binding-win32-x64-msvc/-/binding-win32-x64-msvc-1.0.7.tgz#f26ce9f308d94b7c54cc107c0820273c6ece3528"
76+
integrity sha512-T6E00hKhgfXLkWmkmzyxYl/jKWot0PE2U4/ciGlBVQtDyjc50WPWnRREyPAEv7ozgAqou+ugd5KN+O6SFz4NbQ==
77+
78+
79+
version "1.0.7"
80+
resolved "https://registry.yarnpkg.com/@rspack/binding/-/binding-1.0.7.tgz#355cf2e5eabe60648a58da9bc6c5eecdbb11608c"
81+
integrity sha512-lSjxstfgtesIj1A0pk1W99iTIyDyfv/HXveyV3x+C+62pv0i0jj9tJUDmFM1gGwitKzm1LV9DgW/sOuzz3NtNw==
82+
optionalDependencies:
83+
"@rspack/binding-darwin-arm64" "1.0.7"
84+
"@rspack/binding-darwin-x64" "1.0.7"
85+
"@rspack/binding-linux-arm64-gnu" "1.0.7"
86+
"@rspack/binding-linux-arm64-musl" "1.0.7"
87+
"@rspack/binding-linux-x64-gnu" "1.0.7"
88+
"@rspack/binding-linux-x64-musl" "1.0.7"
89+
"@rspack/binding-win32-arm64-msvc" "1.0.7"
90+
"@rspack/binding-win32-ia32-msvc" "1.0.7"
91+
"@rspack/binding-win32-x64-msvc" "1.0.7"
92+
93+
"@rspack/core@^1.0.7":
94+
version "1.0.7"
95+
resolved "https://registry.yarnpkg.com/@rspack/core/-/core-1.0.7.tgz#7e9905841f9e9904678c3282f1f062ae1e98669f"
96+
integrity sha512-L3O0GDKasMmLtDkZrbfJI8OFJ5hmlLannoJ2hDR3LMq8tC/q0jIXTL7YIo7rStsudecCkcD7CH/Smm00itZSzg==
97+
dependencies:
98+
"@module-federation/runtime-tools" "0.5.1"
99+
"@rspack/binding" "1.0.7"
100+
"@rspack/lite-tapable" "1.0.0"
101+
caniuse-lite "^1.0.30001616"
102+
103+
104+
version "1.0.0"
105+
resolved "https://registry.yarnpkg.com/@rspack/lite-tapable/-/lite-tapable-1.0.0.tgz#160883693462f164d0e6ede22f32614673ef6429"
106+
integrity sha512-7MZf4lburSUZoEenwazwUDKHhqyfnLCGnQ/tKcUtztfmVzfjZfRn/EaiT0AKkYGnL2U8AGsw89oUeVyvaOLVCw==
107+
108+
caniuse-lite@^1.0.30001616:
109+
version "1.0.30001664"
110+
resolved "https://registry.yarnpkg.com/caniuse-lite/-/caniuse-lite-1.0.30001664.tgz#d588d75c9682d3301956b05a3749652a80677df4"
111+
integrity sha512-AmE7k4dXiNKQipgn7a2xg558IRqPN3jMQY/rOsbxDhrd0tyChwbITBfiwtnqz8bi2M5mIWbxAYBvk7W7QBUS2g==
112+
113+
"js-tokens@^3.0.0 || ^4.0.0":
114+
version "4.0.0"
115+
resolved "https://registry.yarnpkg.com/js-tokens/-/js-tokens-4.0.0.tgz#19203fb59991df98e3a287050d4647cdeaf32499"
116+
integrity sha512-RdJUflcE3cUzKiMqQgsCu06FPu9UdIJO0beYbPhHN4k6apgJtifcoCtT9bcxOpYBtpD2kCM6Sbzg4CausW/PKQ==
117+
118+
loose-envify@^1.1.0:
119+
version "1.4.0"
120+
resolved "https://registry.yarnpkg.com/loose-envify/-/loose-envify-1.4.0.tgz#71ee51fa7be4caec1a63839f7e682d8132d30caf"
121+
integrity sha512-lyuxPGr/Wfhrlem2CL/UcnUc1zcqKAImBDzukY7Y5F/yQiNdko6+fRLevlw1HgMySw7f611UIY408EtxRSoK3Q==
122+
dependencies:
123+
js-tokens "^3.0.0 || ^4.0.0"
124+
125+
react-dom@^18.3.1:
126+
version "18.3.1"
127+
resolved "https://registry.yarnpkg.com/react-dom/-/react-dom-18.3.1.tgz#c2265d79511b57d479b3dd3fdfa51536494c5cb4"
128+
integrity sha512-5m4nQKp+rZRb09LNH59GM4BxTh9251/ylbKIbpe7TpGxfJ+9kv6BLkLBXIjjspbgbnIBNqlI23tRnTWT0snUIw==
129+
dependencies:
130+
loose-envify "^1.1.0"
131+
scheduler "^0.23.2"
132+
133+
react@^18.3.1:
134+
version "18.3.1"
135+
resolved "https://registry.yarnpkg.com/react/-/react-18.3.1.tgz#49ab892009c53933625bd16b2533fc754cab2891"
136+
integrity sha512-wS+hAgJShR0KhEvPJArfuPVN1+Hz1t0Y6n5jLrGQbkb4urgPE/0Rve+1kMB1v/oWgHgm4WIcV+i7F2pTVj+2iQ==
137+
dependencies:
138+
loose-envify "^1.1.0"
139+
140+
scheduler@^0.23.2:
141+
version "0.23.2"
142+
resolved "https://registry.yarnpkg.com/scheduler/-/scheduler-0.23.2.tgz#414ba64a3b282892e944cf2108ecc078d115cdc3"
143+
integrity sha512-UOShsPwz7NrMUqhR6t0hWjFduvOzbtv7toDH1/hIrfRNIDBnnBWd0CwJTGvTpngVlmwGCdP9/Zl/tVrDqcuYzQ==
144+
dependencies:
145+
loose-envify "^1.1.0"

0 commit comments

Comments
 (0)