fix: use a different temp path to not confuse image opt (#3620)
This commit is contained in:
1 parent
13dcd80fbf
commit
08f2180bbd
3 files changed
+25
-18
No files matched your search
@@ -1,6 +1,6 @@
|
||||
use anyhow::Context;
|
||||
use manganis_core::{AssetOptions, CssAssetOptions, ImageAssetOptions, JsAssetOptions};
|
||||
use std::{ffi::OsStr, path::Path};
|
||||
use std::path::Path;
|
||||
|
||||
use crate::css::process_scss;
|
||||
|
||||
@@ -40,56 +40,59 @@ pub(crate) fn process_file_to_with_options(
|
||||
// Processing can be slow. Write to a temporary file first and then rename it to the final output path. If everything
|
||||
// goes well. Without this, the user could quit in the middle of processing and the file will look complete to the
|
||||
// caching system even though it is empty.
|
||||
let mut partial_ext = output_path.extension().unwrap_or_default().to_os_string();
|
||||
partial_ext.push(OsStr::new(".partial"));
|
||||
let temp_output_path = output_path.with_extension(&partial_ext);
|
||||
let temp_output_path = &temp_output_path;
|
||||
let temp_path = output_path.with_file_name(format!(
|
||||
"partial.{}",
|
||||
output_path
|
||||
.file_name()
|
||||
.unwrap_or_default()
|
||||
.to_string_lossy()
|
||||
));
|
||||
|
||||
match options {
|
||||
AssetOptions::Unknown => match source.extension().map(|e| e.to_string_lossy()).as_deref() {
|
||||
Some("css") => {
|
||||
process_css(&CssAssetOptions::new(), source, temp_output_path)?;
|
||||
process_css(&CssAssetOptions::new(), source, &temp_path)?;
|
||||
}
|
||||
Some("scss" | "sass") => {
|
||||
process_scss(&CssAssetOptions::new(), source, temp_output_path)?;
|
||||
process_scss(&CssAssetOptions::new(), source, &temp_path)?;
|
||||
}
|
||||
Some("js") => {
|
||||
process_js(&JsAssetOptions::new(), source, temp_output_path, !in_folder)?;
|
||||
process_js(&JsAssetOptions::new(), source, &temp_path, !in_folder)?;
|
||||
}
|
||||
Some("json") => {
|
||||
process_json(source, temp_output_path)?;
|
||||
process_json(source, &temp_path)?;
|
||||
}
|
||||
Some("jpg" | "jpeg" | "png" | "webp" | "avif") => {
|
||||
process_image(&ImageAssetOptions::new(), source, temp_output_path)?;
|
||||
process_image(&ImageAssetOptions::new(), source, &temp_path)?;
|
||||
}
|
||||
Some(_) | None => {
|
||||
if source.is_dir() {
|
||||
process_folder(source, temp_output_path)?;
|
||||
process_folder(source, &temp_path)?;
|
||||
} else {
|
||||
let source_file = std::fs::File::open(source)?;
|
||||
let mut reader = std::io::BufReader::new(source_file);
|
||||
let output_file = std::fs::File::create(temp_output_path)?;
|
||||
let output_file = std::fs::File::create(&temp_path)?;
|
||||
let mut writer = std::io::BufWriter::new(output_file);
|
||||
std::io::copy(&mut reader, &mut writer).with_context(|| {
|
||||
format!(
|
||||
"Failed to write file to output location: {}",
|
||||
temp_output_path.display()
|
||||
temp_path.display()
|
||||
)
|
||||
})?;
|
||||
}
|
||||
}
|
||||
},
|
||||
AssetOptions::Css(options) => {
|
||||
process_css(options, source, temp_output_path)?;
|
||||
process_css(options, source, &temp_path)?;
|
||||
}
|
||||
AssetOptions::Js(options) => {
|
||||
process_js(options, source, temp_output_path, !in_folder)?;
|
||||
process_js(options, source, &temp_path, !in_folder)?;
|
||||
}
|
||||
AssetOptions::Image(options) => {
|
||||
process_image(options, source, temp_output_path)?;
|
||||
process_image(options, source, &temp_path)?;
|
||||
}
|
||||
AssetOptions::Folder(_) => {
|
||||
process_folder(source, temp_output_path)?;
|
||||
process_folder(source, &temp_path)?;
|
||||
}
|
||||
_ => {
|
||||
tracing::warn!("Unknown asset options: {:?}", options);
|
||||
@@ -97,7 +100,7 @@ pub(crate) fn process_file_to_with_options(
|
||||
}
|
||||
|
||||
// If everything was successful, rename the temp file to the final output path
|
||||
std::fs::rename(temp_output_path, output_path)?;
|
||||
std::fs::rename(temp_path, output_path)?;
|
||||
|
||||
Ok(())
|
||||
}
|
||||
Binary file not shown.
|
After Width: | Height: | Size: 20 KiB |
@@ -3,6 +3,7 @@
|
||||
use dioxus::prelude::*;
|
||||
|
||||
const MONACO_FOLDER: Asset = asset!("/monaco-editor-0.52.2/package/min/vs");
|
||||
const SOME_IMAGE: Asset = asset!("/images/toasts.png", ImageAssetOptions::new().with_avif());
|
||||
|
||||
fn main() {
|
||||
dioxus::launch(App);
|
||||
@@ -30,5 +31,8 @@ fn App() -> Element {
|
||||
src: "{MONACO_FOLDER}/loader.js",
|
||||
"onload": script
|
||||
}
|
||||
img {
|
||||
src: "{SOME_IMAGE}"
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user