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 anyhow::Context;
|
||||||
use manganis_core::{AssetOptions, CssAssetOptions, ImageAssetOptions, JsAssetOptions};
|
use manganis_core::{AssetOptions, CssAssetOptions, ImageAssetOptions, JsAssetOptions};
|
||||||
use std::{ffi::OsStr, path::Path};
|
use std::path::Path;
|
||||||
|
|
||||||
use crate::css::process_scss;
|
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
|
// 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
|
// 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.
|
// caching system even though it is empty.
|
||||||
let mut partial_ext = output_path.extension().unwrap_or_default().to_os_string();
|
let temp_path = output_path.with_file_name(format!(
|
||||||
partial_ext.push(OsStr::new(".partial"));
|
"partial.{}",
|
||||||
let temp_output_path = output_path.with_extension(&partial_ext);
|
output_path
|
||||||
let temp_output_path = &temp_output_path;
|
.file_name()
|
||||||
|
.unwrap_or_default()
|
||||||
|
.to_string_lossy()
|
||||||
|
));
|
||||||
|
|
||||||
match options {
|
match options {
|
||||||
AssetOptions::Unknown => match source.extension().map(|e| e.to_string_lossy()).as_deref() {
|
AssetOptions::Unknown => match source.extension().map(|e| e.to_string_lossy()).as_deref() {
|
||||||
Some("css") => {
|
Some("css") => {
|
||||||
process_css(&CssAssetOptions::new(), source, temp_output_path)?;
|
process_css(&CssAssetOptions::new(), source, &temp_path)?;
|
||||||
}
|
}
|
||||||
Some("scss" | "sass") => {
|
Some("scss" | "sass") => {
|
||||||
process_scss(&CssAssetOptions::new(), source, temp_output_path)?;
|
process_scss(&CssAssetOptions::new(), source, &temp_path)?;
|
||||||
}
|
}
|
||||||
Some("js") => {
|
Some("js") => {
|
||||||
process_js(&JsAssetOptions::new(), source, temp_output_path, !in_folder)?;
|
process_js(&JsAssetOptions::new(), source, &temp_path, !in_folder)?;
|
||||||
}
|
}
|
||||||
Some("json") => {
|
Some("json") => {
|
||||||
process_json(source, temp_output_path)?;
|
process_json(source, &temp_path)?;
|
||||||
}
|
}
|
||||||
Some("jpg" | "jpeg" | "png" | "webp" | "avif") => {
|
Some("jpg" | "jpeg" | "png" | "webp" | "avif") => {
|
||||||
process_image(&ImageAssetOptions::new(), source, temp_output_path)?;
|
process_image(&ImageAssetOptions::new(), source, &temp_path)?;
|
||||||
}
|
}
|
||||||
Some(_) | None => {
|
Some(_) | None => {
|
||||||
if source.is_dir() {
|
if source.is_dir() {
|
||||||
process_folder(source, temp_output_path)?;
|
process_folder(source, &temp_path)?;
|
||||||
} else {
|
} else {
|
||||||
let source_file = std::fs::File::open(source)?;
|
let source_file = std::fs::File::open(source)?;
|
||||||
let mut reader = std::io::BufReader::new(source_file);
|
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);
|
let mut writer = std::io::BufWriter::new(output_file);
|
||||||
std::io::copy(&mut reader, &mut writer).with_context(|| {
|
std::io::copy(&mut reader, &mut writer).with_context(|| {
|
||||||
format!(
|
format!(
|
||||||
"Failed to write file to output location: {}",
|
"Failed to write file to output location: {}",
|
||||||
temp_output_path.display()
|
temp_path.display()
|
||||||
)
|
)
|
||||||
})?;
|
})?;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
AssetOptions::Css(options) => {
|
AssetOptions::Css(options) => {
|
||||||
process_css(options, source, temp_output_path)?;
|
process_css(options, source, &temp_path)?;
|
||||||
}
|
}
|
||||||
AssetOptions::Js(options) => {
|
AssetOptions::Js(options) => {
|
||||||
process_js(options, source, temp_output_path, !in_folder)?;
|
process_js(options, source, &temp_path, !in_folder)?;
|
||||||
}
|
}
|
||||||
AssetOptions::Image(options) => {
|
AssetOptions::Image(options) => {
|
||||||
process_image(options, source, temp_output_path)?;
|
process_image(options, source, &temp_path)?;
|
||||||
}
|
}
|
||||||
AssetOptions::Folder(_) => {
|
AssetOptions::Folder(_) => {
|
||||||
process_folder(source, temp_output_path)?;
|
process_folder(source, &temp_path)?;
|
||||||
}
|
}
|
||||||
_ => {
|
_ => {
|
||||||
tracing::warn!("Unknown asset options: {:?}", options);
|
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
|
// 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(())
|
Ok(())
|
||||||
}
|
}
|
||||||
Binary file not shown.
|
After Width: | Height: | Size: 20 KiB |
@@ -3,6 +3,7 @@
|
|||||||
use dioxus::prelude::*;
|
use dioxus::prelude::*;
|
||||||
|
|
||||||
const MONACO_FOLDER: Asset = asset!("/monaco-editor-0.52.2/package/min/vs");
|
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() {
|
fn main() {
|
||||||
dioxus::launch(App);
|
dioxus::launch(App);
|
||||||
@@ -30,5 +31,8 @@ fn App() -> Element {
|
|||||||
src: "{MONACO_FOLDER}/loader.js",
|
src: "{MONACO_FOLDER}/loader.js",
|
||||||
"onload": script
|
"onload": script
|
||||||
}
|
}
|
||||||
|
img {
|
||||||
|
src: "{SOME_IMAGE}"
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
Reference in new issue
Block a user