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::{BTreeMap, 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: BTreeMap<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    /// Get the first asset that matches the given source path
69    pub fn get_first_asset_for_source(&self, path: &Path) -> Option<&BundledAsset> {
70        self.assets
71            .get(path)
72            .and_then(|assets| assets.iter().next())
73    }
74
75    /// Check if the manifest contains a specific asset
76    pub fn contains(&self, asset: &BundledAsset) -> bool {
77        self.assets
78            .get(&PathBuf::from(asset.absolute_source_path()))
79            .is_some_and(|assets| assets.contains(asset))
80    }
81
82    /// Iterate over all the assets with unique output paths in the manifest. This will not include
83    /// assets that have different source paths, but the same file contents.
84    pub fn unique_assets(&self) -> impl Iterator<Item = &BundledAsset> {
85        let mut seen = HashSet::new();
86        self.assets
87            .values()
88            .flat_map(|assets| assets.iter())
89            .filter(move |asset| seen.insert(asset.bundled_path()))
90    }
91
92    pub fn load_from_file(path: &Path) -> anyhow::Result<Self> {
93        let src = std::fs::read_to_string(path)?;
94
95        serde_json::from_str(&src)
96            .with_context(|| format!("Failed to parse asset manifest from {path:?}\n{src}"))
97    }
98}
99
100/// Optimize a list of assets in parallel
101pub fn optimize_all_assets(
102    assets_to_transfer: Vec<(PathBuf, PathBuf, AssetOptions)>,
103    on_optimization_start: impl FnMut(&Path, &Path, &AssetOptions) + Sync + Send,
104    on_optimization_end: impl FnMut(&Path, &Path, &AssetOptions) + Sync + Send,
105) -> anyhow::Result<()> {
106    let on_optimization_start = Arc::new(RwLock::new(on_optimization_start));
107    let on_optimization_end = Arc::new(RwLock::new(on_optimization_end));
108    assets_to_transfer
109        .par_iter()
110        .try_for_each(|(from, to, options)| {
111            {
112                let mut on_optimization_start = on_optimization_start.write().unwrap();
113                on_optimization_start(from, to, options);
114            }
115
116            let res = process_file_to(options, from, to);
117            if let Err(err) = res.as_ref() {
118                tracing::error!("Failed to copy asset {from:?}: {err}");
119            }
120
121            {
122                let mut on_optimization_end = on_optimization_end.write().unwrap();
123                on_optimization_end(from, to, options);
124            }
125
126            res.map(|_| ())
127        })
128}