Skip to content

Commit 07cc1ed

Browse files
committed
fix: ensure that the target-path is applied to hrefs
1 parent ef90794 commit 07cc1ed

File tree

6 files changed

+46
-9
lines changed

6 files changed

+46
-9
lines changed

src/pipelines/copy_file.rs

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -69,7 +69,13 @@ impl CopyFile {
6969

7070
let _ = self
7171
.asset
72-
.copy(&dir_out, false, false, AssetFileType::Other)
72+
.copy(
73+
&self.cfg.staging_dist,
74+
&dir_out,
75+
false,
76+
false,
77+
AssetFileType::Other,
78+
)
7379
.await?;
7480
tracing::debug!(path = ?rel_path, "finished copying file");
7581

src/pipelines/css.rs

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -86,11 +86,17 @@ impl Css {
8686

8787
let file = self
8888
.asset
89-
.copy(&result_path, self.cfg.filehash, minify, AssetFileType::Css)
89+
.copy(
90+
&self.cfg.staging_dist,
91+
&result_path,
92+
self.cfg.filehash,
93+
minify,
94+
AssetFileType::Css,
95+
)
9096
.await?;
9197
tracing::debug!(path = ?rel_path, "finished copying & hashing css");
9298

93-
let result_file = result_path.join(&file);
99+
let result_file = self.cfg.staging_dist.join(&file);
94100
let integrity = OutputDigest::generate(self.integrity, || std::fs::read(&result_file))
95101
.with_context(|| {
96102
format!(

src/pipelines/icon.rs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -79,14 +79,15 @@ impl Icon {
7979
let file = self
8080
.asset
8181
.copy(
82+
&self.cfg.staging_dist,
8283
&result_dir,
8384
self.cfg.filehash,
8485
self.cfg.release && !self.cfg.no_minification,
8586
AssetFileType::Icon(image_type),
8687
)
8788
.await?;
8889

89-
let result_file = result_dir.join(&file);
90+
let result_file = self.cfg.staging_dist.join(&file);
9091
let integrity = OutputDigest::generate(self.integrity, || std::fs::read(&result_file))
9192
.with_context(|| {
9293
format!(

src/pipelines/js.rs

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -86,6 +86,7 @@ impl Js {
8686
let file = self
8787
.asset
8888
.copy(
89+
&self.cfg.staging_dist,
8990
&result_dir,
9091
self.cfg.filehash,
9192
minify,
@@ -96,9 +97,9 @@ impl Js {
9697
},
9798
)
9899
.await?;
99-
tracing::debug!(path = ?rel_path, "finished copying & hashing js");
100+
tracing::debug!(path = ?rel_path, file = ?file, "finished copying & hashing js");
100101

101-
let result_file = result_dir.join(&file);
102+
let result_file = self.cfg.staging_dist.join(&file);
102103
let integrity = OutputDigest::generate(self.integrity, || std::fs::read(&result_file))
103104
.with_context(|| {
104105
format!(

src/pipelines/mod.rs

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -243,10 +243,11 @@ impl AssetFile {
243243
/// Copy this asset to the target dir. If hashing is enabled, create a hash from the file
244244
/// contents and include it as hex string in the destination file name.
245245
///
246-
/// The base file name (stripped path, without any parent folders) is returned if the operation
246+
/// The base file name (stripped path, relative to the base dist dir) is returned if the operation
247247
/// was successful.
248248
pub async fn copy(
249249
&self,
250+
dist: &Path,
250251
to_dir: &Path,
251252
with_hash: bool,
252253
minify: bool,
@@ -287,6 +288,17 @@ impl AssetFile {
287288
};
288289

289290
let file_path = to_dir.join(&file_name);
291+
let file_name = file_path
292+
.strip_prefix(dist)
293+
.with_context(|| {
294+
format!(
295+
"unable to create a relative path of '{}' in '{}'",
296+
file_path.display(),
297+
dist.display()
298+
)
299+
})?
300+
.to_string_lossy()
301+
.to_string();
290302

291303
fs::write(&file_path, bytes)
292304
.await

src/pipelines/sass.rs

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,10 @@
11
//! Sass/Scss asset pipeline.
22
3-
use super::{AssetFile, AttrWriter, Attrs, TrunkAssetPipelineOutput, ATTR_HREF, ATTR_INLINE};
3+
use super::{
4+
data_target_path, AssetFile, AttrWriter, Attrs, TrunkAssetPipelineOutput, ATTR_HREF,
5+
ATTR_INLINE,
6+
};
7+
use crate::common::target_path;
48
use crate::{
59
common,
610
config::RtcBuild,
@@ -28,6 +32,8 @@ pub struct Sass {
2832
other_attrs: Attrs,
2933
/// The required integrity setting
3034
integrity: IntegrityType,
35+
/// Optional target path inside the dist dir.
36+
target_path: Option<PathBuf>,
3137
}
3238

3339
impl Sass {
@@ -50,6 +56,7 @@ impl Sass {
5056
let use_inline = attrs.get(ATTR_INLINE).is_some();
5157

5258
let integrity = IntegrityType::from_attrs(&attrs, &cfg)?;
59+
let target_path = data_target_path(&attrs)?;
5360

5461
Ok(Self {
5562
id,
@@ -58,6 +65,7 @@ impl Sass {
5865
use_inline,
5966
other_attrs: attrs,
6067
integrity,
68+
target_path,
6169
})
6270
}
6371

@@ -128,7 +136,10 @@ impl Sass {
128136
.filehash
129137
.then(|| format!("{}-{:x}.css", &self.asset.file_stem.to_string_lossy(), hash))
130138
.unwrap_or(temp_target_file_name);
131-
let file_path = self.cfg.staging_dist.join(&file_name);
139+
140+
let result_dir =
141+
target_path(&self.cfg.staging_dist, self.target_path.as_deref(), None).await?;
142+
let file_path = result_dir.join(&file_name);
132143

133144
let integrity = OutputDigest::generate_from(self.integrity, css.as_bytes());
134145

0 commit comments

Comments
 (0)