dioxus_cli_opt/
file.rs

1use anyhow::Context;
2use manganis::{CssModuleAssetOptions, FolderAssetOptions};
3use manganis_core::{AssetOptions, CssAssetOptions, ImageAssetOptions, JsAssetOptions};
4use std::path::Path;
5
6use crate::css::{process_css_module, process_scss};
7
8use super::{
9    css::process_css, folder::process_folder, image::process_image, js::process_js,
10    json::process_json,
11};
12
13/// Process a specific file asset with the given options reading from the source and writing to the output path
14pub fn process_file_to(
15    options: &AssetOptions,
16    source: &Path,
17    output_path: &Path,
18) -> anyhow::Result<()> {
19    process_file_to_with_options(options, source, output_path, false)
20}
21
22/// Process a specific file asset with additional options
23pub(crate) fn process_file_to_with_options(
24    options: &AssetOptions,
25    source: &Path,
26    output_path: &Path,
27    in_folder: bool,
28) -> anyhow::Result<()> {
29    // If the file already exists, then we must have a file with the same hash
30    // already. The hash has the file contents and options, so if we find a file
31    // with the same hash, we probably already created this file in the past
32    if output_path.exists() {
33        return Ok(());
34    }
35    if let Some(parent) = output_path.parent() {
36        if !parent.exists() {
37            std::fs::create_dir_all(parent).context("Failed to create directory")?;
38        }
39    }
40
41    // Processing can be slow. Write to a temporary file first and then rename it to the final output path. If everything
42    // goes well. Without this, the user could quit in the middle of processing and the file will look complete to the
43    // caching system even though it is empty.
44    let temp_path = output_path.with_file_name(format!(
45        "partial.{}",
46        output_path
47            .file_name()
48            .unwrap_or_default()
49            .to_string_lossy()
50    ));
51    let resolved_options = resolve_asset_options(source, options);
52
53    match &resolved_options {
54        ResolvedAssetType::Css(options) => {
55            process_css(options, source, &temp_path)?;
56        }
57        ResolvedAssetType::CssModule(options) => {
58            process_css_module(options, source, output_path, &temp_path)?;
59        }
60        ResolvedAssetType::Scss(options) => {
61            process_scss(options, source, &temp_path)?;
62        }
63        ResolvedAssetType::Js(options) => {
64            process_js(options, source, &temp_path, !in_folder)?;
65        }
66        ResolvedAssetType::Image(options) => {
67            process_image(options, source, &temp_path)?;
68        }
69        ResolvedAssetType::Json => {
70            process_json(source, &temp_path)?;
71        }
72        ResolvedAssetType::Folder(_) => {
73            process_folder(source, &temp_path)?;
74        }
75        ResolvedAssetType::File => {
76            let source_file = std::fs::File::open(source)?;
77            let mut reader = std::io::BufReader::new(source_file);
78            let output_file = std::fs::File::create(&temp_path)?;
79            let mut writer = std::io::BufWriter::new(output_file);
80            std::io::copy(&mut reader, &mut writer).with_context(|| {
81                format!(
82                    "Failed to write file to output location: {}",
83                    temp_path.display()
84                )
85            })?;
86        }
87    }
88
89    // If everything was successful, rename the temp file to the final output path
90    std::fs::rename(temp_path, output_path).context("Failed to rename output file")?;
91
92    Ok(())
93}
94
95pub(crate) enum ResolvedAssetType {
96    /// An image asset
97    Image(ImageAssetOptions),
98    /// A css asset
99    Css(CssAssetOptions),
100    /// A css module asset
101    CssModule(CssModuleAssetOptions),
102    /// A SCSS asset
103    Scss(CssAssetOptions),
104    /// A javascript asset
105    Js(JsAssetOptions),
106    /// A json asset
107    Json,
108    /// A folder asset
109    Folder(FolderAssetOptions),
110    /// A generic file
111    File,
112}
113
114pub(crate) fn resolve_asset_options(source: &Path, options: &AssetOptions) -> ResolvedAssetType {
115    match options {
116        AssetOptions::Image(image) => ResolvedAssetType::Image(*image),
117        AssetOptions::Css(css) => ResolvedAssetType::Css(*css),
118        AssetOptions::CssModule(css) => ResolvedAssetType::CssModule(*css),
119        AssetOptions::Js(js) => ResolvedAssetType::Js(*js),
120        AssetOptions::Folder(folder) => ResolvedAssetType::Folder(*folder),
121        AssetOptions::Unknown => resolve_unknown_asset_options(source),
122        _ => {
123            tracing::warn!("Unknown asset options... you may need to update the Dioxus CLI. Defaulting to a generic file: {:?}", options);
124            resolve_unknown_asset_options(source)
125        }
126    }
127}
128
129fn resolve_unknown_asset_options(source: &Path) -> ResolvedAssetType {
130    match source.extension().map(|e| e.to_string_lossy()).as_deref() {
131        Some("scss" | "sass") => ResolvedAssetType::Scss(CssAssetOptions::new()),
132        Some("css") => ResolvedAssetType::Css(CssAssetOptions::new()),
133        Some("js") => ResolvedAssetType::Js(JsAssetOptions::new()),
134        Some("json") => ResolvedAssetType::Json,
135        Some("jpg" | "jpeg" | "png" | "webp" | "avif") => {
136            ResolvedAssetType::Image(ImageAssetOptions::new())
137        }
138        _ if source.is_dir() => ResolvedAssetType::Folder(FolderAssetOptions::new()),
139        _ => ResolvedAssetType::File,
140    }
141}