Skip to content

Commit d810047

Browse files
authored
feat: improve manifest data collection (#785)
1 parent 19c73f7 commit d810047

File tree

1 file changed

+59
-5
lines changed

1 file changed

+59
-5
lines changed

crates/tuono_lib/src/manifest.rs

Lines changed: 59 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,8 @@ fn clean_route_path(path: String) -> String {
5454
let path = path
5555
.replace("../src/routes", "")
5656
.replace(".tsx", "")
57+
.replace(".mdx", "")
58+
.replace(".md", "")
5759
.replace(".jsx", "");
5860

5961
if path == "/index" {
@@ -121,15 +123,31 @@ impl From<ViteManifest> for Manifest {
121123
}
122124

123125
// Add __layout imports
124-
for (key, layout_bundle) in manifest {
125-
let route = clean_route_path(key);
126+
for (key, layout_bundle) in &manifest {
127+
let route = clean_route_path(key.clone());
126128
if route.contains("__layout") {
127129
let path_included_in_layout = route.replace("__layout", "");
128130

131+
let mut layout_css_files: Vec<String> = Vec::new();
132+
let mut layout_js_files: Vec<String> = Vec::new();
133+
134+
for import in &layout_bundle.imports {
135+
if import == "client-main.tsx" {
136+
continue;
137+
}
138+
139+
if let Some(import_bundle) = manifest.get(import) {
140+
layout_js_files.push(import_bundle.file.clone());
141+
layout_css_files.extend(import_bundle.css.clone());
142+
}
143+
}
144+
129145
for (key, route_bundles) in &mut bundles {
130146
if key.starts_with(path_included_in_layout.as_str()) {
131147
route_bundles.js_files.push(layout_bundle.file.clone());
132148
route_bundles.css_files.extend(layout_bundle.css.clone());
149+
route_bundles.js_files.extend(layout_js_files.clone());
150+
route_bundles.css_files.extend(layout_css_files.clone());
133151
}
134152
}
135153
}
@@ -249,12 +267,23 @@ mod tests {
249267
"src": "../src/routes/about.tsx",
250268
"isDynamicEntry": true,
251269
"imports": [
252-
"client-main.tsx"
270+
"client-main.tsx",
271+
"_FileWithCssOnly.js"
253272
],
254273
"css": [
255274
"assets/about-DUhMJ_Ze.css"
256275
]
257276
},
277+
"_FileWithCssOnly.js": {
278+
"file": "assets/FileWithCssOnly.js",
279+
"name": "FileWithCssOnly",
280+
"imports": [
281+
"client-main.tsx"
282+
],
283+
"css": [
284+
"assets/FileWithCssOnly.css"
285+
]
286+
},
258287
"../src/routes/catch_all/[...slug].tsx": {
259288
"file": "assets/_...slug_-CpJyPnPj.js",
260289
"name": "_...slug_",
@@ -350,6 +379,26 @@ mod tests {
350379
}
351380
}"#;
352381

382+
#[test]
383+
fn it_correctly_cleans_the_route_path() {
384+
let cleaned_path = clean_route_path("../src/routes/index.tsx".to_string());
385+
assert_eq!(cleaned_path, "/");
386+
387+
let cleaned_path =
388+
clean_route_path("../src/routes/pokemons/[pokemon]/index.tsx".to_string());
389+
assert_eq!(cleaned_path, "/pokemons/[pokemon]");
390+
391+
let cleaned_path = clean_route_path("../src/routes/pokemons/__layout.tsx".to_string());
392+
assert_eq!(cleaned_path, "/pokemons/__layout");
393+
394+
let cleaned_path =
395+
clean_route_path("../src/routes/pokemons/[pokemon]/[type].mdx".to_string());
396+
assert_eq!(cleaned_path, "/pokemons/[pokemon]/[type]");
397+
398+
let cleaned_path = clean_route_path("../src/routes/about.md".to_string());
399+
assert_eq!(cleaned_path, "/about");
400+
}
401+
353402
#[test]
354403
fn correctly_parse_the_manifest_json() {
355404
let parsed_manifest = serde_json::from_str::<ViteManifest>(MANIFEST_EXAMPLE).unwrap();
@@ -456,12 +505,17 @@ mod tests {
456505
route.css_files,
457506
vec![
458507
"assets/about-DUhMJ_Ze.css",
459-
"assets/client-main-BS7N-NIa.css"
508+
"assets/client-main-BS7N-NIa.css",
509+
"assets/FileWithCssOnly.css"
460510
]
461511
);
462512
assert_eq!(
463513
route.js_files,
464-
vec!["assets/about-C3UqHfGb.js", "assets/client-main-DOdr9gvl.js"]
514+
vec![
515+
"assets/about-C3UqHfGb.js",
516+
"assets/client-main-DOdr9gvl.js",
517+
"assets/FileWithCssOnly.js"
518+
]
465519
);
466520
}
467521
}

0 commit comments

Comments
 (0)