Skip to content

Commit 1550f0d

Browse files
committed
feat: implement data-target-path for sass
1 parent 73d967a commit 1550f0d

File tree

5 files changed

+30
-20
lines changed

5 files changed

+30
-20
lines changed

examples/target-path/index.html

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
<meta name="viewport" content="width=device-width, initial-scale=1"/>
66
<title>Trunk | target-path</title>
77

8-
<link data-trunk rel="scss" href="src/index.scss"/>
8+
<link data-trunk rel="scss" href="src/index.scss" data-target-path="static"/>
99
<link data-trunk rel="css" href="src/app.css" data-target-path="static"/>
1010
<link data-trunk data-no-minify rel="css" href="src/not_minified.css" data-target-path="static"/>
1111
<base data-trunk-public-url/>

site/content/assets.md

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,7 @@ This will typically look like: `<link data-trunk rel="{type}" href="{path}" ..ot
4747

4848
- `data-inline`: (optional) this attribute will inline the compiled CSS from the SASS/SCSS file into a `<style>` tag instead of using a `<link rel="stylesheet">` tag.
4949
- `data-integrity`: (optional) the `integrity` digest type for code & script resources. Defaults to plain `sha384`.
50+
- `data-target-path`: (optional) Path where the directory is placed inside the dist dir. If not present, the directory is placed in the dist root. The path must be a relative path without `..`.
5051

5152
## css
5253

@@ -55,7 +56,7 @@ This will typically look like: `<link data-trunk rel="{type}" href="{path}" ..ot
5556
- In the future, Trunk will resolve local `@imports`, will handle minification (see [trunk#7](https://github.com/trunk-rs/trunk/issues/7)), and we may even look into a pattern where any CSS found in the source tree will be bundled, which would enable a nice zero-config "component styles" pattern. See [trunk#3](https://github.com/trunk-rs/trunk/issues/3) for more details.
5657
- `data-integrity`: (optional) the `integrity` digest type for code & script resources. Defaults to plain `sha384`.
5758
- `data-no-minify`: (optional) by default, CSS files are minified in `--release` mode (unless building with `--no-minification`). Setting this attribute disables minification for that particular file. Defaults to false.
58-
- `data-target-path`: (optional) Path where the directory is placed inside the dist dir. If not present the directory is placed in the dist root. The path must be a relative path without `..`.
59+
- `data-target-path`: (optional) Path where the directory is placed inside the dist dir. If not present, the directory is placed in the dist root. The path must be a relative path without `..`.
5960

6061
## tailwind
6162

@@ -69,7 +70,7 @@ This will typically look like: `<link data-trunk rel="{type}" href="{path}" ..ot
6970
`rel="icon"`: Trunk will copy the icon image specified in the `href` attribute to the `dist` dir. This content is hashed for cache control.
7071

7172
- `data-integrity`: (optional) the `integrity` digest type for code & script resources. Defaults to plain `sha384`.
72-
- `data-target-path`: (optional) Path where the directory is placed inside the dist dir. If not present the directory is placed in the dist root. The path must be a relative path without `..`.
73+
- `data-target-path`: (optional) Path where the directory is placed inside the dist dir. If not present, the directory is placed in the dist root. The path must be a relative path without `..`.
7374

7475
## inline
7576

@@ -84,13 +85,13 @@ This will typically look like: `<link data-trunk rel="{type}" href="{path}" ..ot
8485

8586
`rel="copy-file"`: Trunk will copy the file specified in the `href` attribute to the `dist` dir. This content is copied exactly, no hashing is performed.
8687

87-
- `data-target-path`: (optional) Path where the directory is placed inside the dist dir. If not present the directory is placed in the dist root. The path must be a relative path without `..`.
88+
- `data-target-path`: (optional) Path where the directory is placed inside the dist dir. If not present, the directory is placed in the dist root. The path must be a relative path without `..`.
8889

8990
## copy-dir
9091

9192
`rel="copy-dir"`: Trunk will recursively copy the directory specified in the `href` attribute to the `dist` dir. This content is copied exactly, no hashing is performed.
9293

93-
- `data-target-path`: (optional) Path where the directory is placed inside the dist dir. If not present the directory is placed in the dist root. The path must be a relative path without `..`.
94+
- `data-target-path`: (optional) Path where the directory is placed inside the dist dir. If not present, the directory is placed in the dist root. The path must be a relative path without `..`.
9495

9596
# Script Asset Types
9697

@@ -109,7 +110,7 @@ This will typically look like: `<script data-trunk src="{path}" ..other options
109110
Trunk will copy script files found in the source HTML without content modification. This content is hashed for cache control. The `src` attribute must be included in the script pointing to the script file to be processed.
110111

111112
- `data-no-minify`: (optional) by default, scripts are minified in `--release` mode (unless building with `--no-minification`). Setting this attribute disables minification for that particular file. Defaults to false.
112-
- `data-target-path`: (optional) Path where the directory is placed inside the dist dir. If not present the directory is placed in the dist root. The path must be a relative path without `..`.
113+
- `data-target-path`: (optional) Path where the directory is placed inside the dist dir. If not present, the directory is placed in the dist root. The path must be a relative path without `..`.
113114

114115
## JS Snippets
115116

src/common.rs

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -221,3 +221,21 @@ pub async fn target_path(
221221
Ok(base.to_owned())
222222
}
223223
}
224+
225+
/// Create a file_name, including the relative base to the `dist`.
226+
///
227+
/// The function will return an error if the `target_file` is not a direct or indirect child of
228+
/// `dist`.
229+
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())
241+
}

src/pipelines/mod.rs

Lines changed: 2 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ mod rust;
1313
mod sass;
1414
mod tailwind_css;
1515

16-
use crate::common::path_exists;
16+
use crate::common::{dist_relative, path_exists};
1717
use crate::config::RtcBuild;
1818
use crate::pipelines::copy_dir::{CopyDir, CopyDirOutput};
1919
use crate::pipelines::copy_file::{CopyFile, CopyFileOutput};
@@ -288,17 +288,7 @@ impl AssetFile {
288288
};
289289

290290
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();
291+
let file_name = dist_relative(dist, &file_path)?;
302292

303293
fs::write(&file_path, bytes)
304294
.await

src/pipelines/sass.rs

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ use super::{
44
data_target_path, AssetFile, AttrWriter, Attrs, TrunkAssetPipelineOutput, ATTR_HREF,
55
ATTR_INLINE,
66
};
7-
use crate::common::target_path;
7+
use crate::common::{dist_relative, target_path};
88
use crate::{
99
common,
1010
config::RtcBuild,
@@ -140,6 +140,7 @@ impl Sass {
140140
let result_dir =
141141
target_path(&self.cfg.staging_dist, self.target_path.as_deref(), None).await?;
142142
let file_path = result_dir.join(&file_name);
143+
let file_href = dist_relative(&self.cfg.staging_dist, &file_path)?;
143144

144145
let integrity = OutputDigest::generate_from(self.integrity, css.as_bytes());
145146

@@ -152,7 +153,7 @@ impl Sass {
152153
})?;
153154

154155
// Generate a hashed reference to the new CSS file.
155-
CssRef::File(file_name, integrity)
156+
CssRef::File(file_href, integrity)
156157
};
157158

158159
tracing::debug!(path = ?rel_path, "finished compiling sass/scss");

0 commit comments

Comments
 (0)