Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: use a different temp path to not confuse image opt #3620

Merged
merged 1 commit into from
Jan 22, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
39 changes: 21 additions & 18 deletions packages/cli-opt/src/file.rs
Original file line number Diff line number Diff line change
@@ -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;

Expand Down Expand Up @@ -40,64 +40,67 @@ 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);
}
}

// 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(())
}
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
4 changes: 4 additions & 0 deletions packages/playwright-tests/cli-optimization/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down Expand Up @@ -30,5 +31,8 @@ fn App() -> Element {
src: "{MONACO_FOLDER}/loader.js",
"onload": script
}
img {
src: "{SOME_IMAGE}"
}
}
}
Loading