Skip to content

Commit 80f42f9

Browse files
committed
fix: convert paths to hrefs in way that works on all platforms
1 parent f8aba29 commit 80f42f9

File tree

2 files changed

+25
-13
lines changed

2 files changed

+25
-13
lines changed

src/common.rs

Lines changed: 23 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -227,22 +227,33 @@ pub async fn target_path(
227227
/// The function will return an error if the `target_file` is not a direct or indirect child of
228228
/// `dist`.
229229
pub fn dist_relative(dist: &Path, target_file: &Path) -> Result<String> {
230-
Ok(target_file
231-
.strip_prefix(dist)
232-
.with_context(|| {
233-
format!(
234-
"unable to create a relative path of '{}' in '{}'",
235-
target_file.display(),
236-
dist.display()
237-
)
238-
})?
239-
.to_string_lossy()
240-
.to_string())
230+
let target_file = target_file.strip_prefix(dist).with_context(|| {
231+
format!(
232+
"unable to create a relative path of '{}' in '{}'",
233+
target_file.display(),
234+
dist.display()
235+
)
236+
})?;
237+
238+
Ok(path_to_href(target_file))
241239
}
242240

241+
/// Take a path, and create a relocated name it into the `target_path`, if present.
243242
pub fn apply_data_target_path(path: impl Into<String>, target_path: &Option<PathBuf>) -> String {
244243
match target_path {
245-
Some(target_path) => format!("{}/{}", target_path.display(), path.into()),
244+
Some(target_path) => path_to_href(target_path.join(path.into())),
246245
None => path.into(),
247246
}
248247
}
248+
249+
/// Take a path and turn it into an href compatible path
250+
///
251+
/// Basically, this means replacing path separator with a forward slash on Windows.
252+
pub fn path_to_href(path: impl AsRef<Path>) -> String {
253+
let path = path
254+
.as_ref()
255+
.iter()
256+
.map(|c| c.to_string_lossy())
257+
.collect::<Vec<_>>();
258+
path.join("/")
259+
}

src/pipelines/rust/mod.rs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ mod wasm_opt;
88
pub use output::RustAppOutput;
99

1010
use super::{data_target_path, Attrs, TrunkAssetPipelineOutput, ATTR_HREF, SNIPPETS_DIR};
11+
use crate::common::path_to_href;
1112
use crate::{
1213
common::{
1314
self, apply_data_target_path, check_target_not_found_err, copy_dir_recursive, path_exists,
@@ -652,7 +653,7 @@ impl RustApp {
652653
self.sri
653654
.record_file(
654655
SriType::ModulePreload,
655-
name.to_string_lossy(),
656+
path_to_href(name),
656657
SriOptions::default(),
657658
&snippet,
658659
)

0 commit comments

Comments
 (0)