dioxus_cli_opt/
lib.rs

1use anyhow::Context;
2use manganis::AssetOptions;
3use manganis_core::BundledAsset;
4use rayon::iter::{IntoParallelRefIterator, ParallelIterator};
5use serde::{Deserialize, Serialize};
6use std::collections::{HashMap, HashSet};
7use std::path::{Path, PathBuf};
8use std::sync::{Arc, RwLock};
9
10mod build_info;
11mod css;
12mod file;
13mod folder;
14mod hash;
15mod image;
16mod js;
17mod json;
18
19pub use file::process_file_to;
20pub use hash::add_hash_to_asset;
21
22/// A manifest of all assets collected from dependencies
23///
24/// This will be filled in primarily by incremental compilation artifacts.
25#[derive(Debug, PartialEq, Default, Clone, Serialize, Deserialize)]
26pub struct AssetManifest {
27    /// Map of bundled asset name to the asset itself
28    assets: HashMap<PathBuf, HashSet<BundledAsset>>,
29}
30
31impl AssetManifest {
32    /// Manually add an asset to the manifest
33    pub fn register_asset(
34        &mut self,
35        asset_path: &Path,
36        options: manganis::AssetOptions,
37    ) -> anyhow::Result<BundledAsset> {
38        let output_path_str = asset_path.to_str().ok_or(anyhow::anyhow!(
39            "Failed to convert wasm bindgen output path to string"
40        ))?;
41
42        let mut bundled_asset =
43            manganis::macro_helpers::create_bundled_asset(output_path_str, options);
44        add_hash_to_asset(&mut bundled_asset);
45
46        self.assets
47            .entry(asset_path.to_path_buf())
48            .or_default()
49            .insert(bundled_asset);
50
51        Ok(bundled_asset)
52    }
53
54    /// Insert an existing bundled asset to the manifest
55    pub fn insert_asset(&mut self, asset: BundledAsset) {
56        let asset_path = asset.absolute_source_path();
57        self.assets
58            .entry(asset_path.into())
59            .or_default()
60            .insert(asset);
61    }
62
63    /// Get any assets that are tied to a specific source file
64    pub fn get_assets_for_source(&self, path: &Path) -> Option<&HashSet<BundledAsset>> {
65        self.assets.get(path)
66    }
67
68    /// Check if the manifest contains a specific asset
69    pub fn contains(&self, asset: &BundledAsset) -> bool {
70        self.assets
71            .get(&PathBuf::from(asset.absolute_source_path()))
72            .is_some_and(|assets| assets.contains(asset))
73    }
74
75    /// Iterate over all the assets in the manifest
76    pub fn assets(&self) -> impl Iterator<Item = &BundledAsset> {
77        self.assets.values().flat_map(|assets| assets.iter())
78    }
79
80    pub fn load_from_file(path: &Path) -> anyhow::Result<Self> {
81        let src = std::fs::read_to_string(path)?;
82
83        serde_json::from_str(&src)
84            .with_context(|| format!("Failed to parse asset manifest from {path:?}\n{src}"))
85    }
86}
87
88/// Optimize a list of assets in parallel
89pub fn optimize_all_assets(
90    assets_to_transfer: Vec<(PathBuf, PathBuf, AssetOptions)>,
91    on_optimization_start: impl FnMut(&Path, &Path, &AssetOptions) + Sync + Send,
92    on_optimization_end: impl FnMut(&Path, &Path, &AssetOptions) + Sync + Send,
93) -> anyhow::Result<()> {
94    let on_optimization_start = Arc::new(RwLock::new(on_optimization_start));
95    let on_optimization_end = Arc::new(RwLock::new(on_optimization_end));
96    assets_to_transfer
97        .par_iter()
98        .try_for_each(|(from, to, options)| {
99            {
100                let mut on_optimization_start = on_optimization_start.write().unwrap();
101                on_optimization_start(from, to, options);
102            }
103
104            let res = process_file_to(options, from, to);
105            if let Err(err) = res.as_ref() {
106                tracing::error!("Failed to copy asset {from:?}: {err}");
107            }
108
109            {
110                let mut on_optimization_end = on_optimization_end.write().unwrap();
111                on_optimization_end(from, to, options);
112            }
113
114            res.map(|_| ())
115        })
116}