vite_static_shared/
parsing.rs1use std::{borrow::Cow, fs::File, io::Read as _, path::Path};
9
10use anyhow::Context as _;
11use anyhow::Result;
12use base64::{Engine as _, prelude::BASE64_STANDARD};
13
14use crate::{ManifestChunk, ViteManifest};
15
16pub fn parse_manifest(vite_dist: &Path) -> anyhow::Result<ViteManifest<'static>> {
29 let file = File::open(vite_dist.join(".vite").join("manifest.json"))
30 .context("failed to open vite manifest")?;
31
32 let mut manifest: ViteManifest =
33 serde_json::from_reader(file).context("failed to parse vite manifest")?;
34
35 for chunk in manifest.clone().values() {
38 for css in chunk.css.as_ref() {
39 manifest.insert(
40 css.clone(),
41 ManifestChunk {
42 key: css.clone(),
43 file: css.clone(),
44 ..Default::default()
45 },
46 );
47 }
48 }
49
50 for (key, chunk) in &mut manifest {
51 chunk.key.clone_from(key);
52 }
53
54 Ok(manifest)
55}
56
57pub fn read_manifest_chunk(vite_dist: &Path, chunk: &mut ManifestChunk) -> Result<()> {
69 let mut file = File::open(vite_dist.join(chunk.file.as_ref())).with_context(|| {
70 format!(
71 r#"failed to open vite chunk at "{path}""#,
72 path = chunk.file
73 )
74 })?;
75
76 let mut contents = Vec::new();
77
78 file.read_to_end(&mut contents).with_context(|| {
79 format!(
80 r#"failed to read vite chunk at "{path}""#,
81 path = chunk.file
82 )
83 })?;
84
85 chunk.hash = Cow::Owned({
86 let hash = blake3::hash(&contents);
87 BASE64_STANDARD.encode(hash.as_bytes())
88 });
89
90 chunk.contents = Cow::Owned(contents);
91
92 chunk.mime_type = mime_guess::from_path(chunk.file.as_ref())
93 .first_or_octet_stream()
94 .to_string()
95 .into();
96
97 Ok(())
98}