Skip to content

Commit 106863c

Browse files
committed
feat: add data-target-path to the css step
1 parent 51098c9 commit 106863c

File tree

3 files changed

+17
-8
lines changed

3 files changed

+17
-8
lines changed

examples/target-path/index.html

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
<title>Trunk | target-path</title>
77

88
<link data-trunk rel="scss" href="src/index.scss"/>
9-
<link data-trunk rel="css" href="src/app.css"/>
9+
<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"/>
1111
<base data-trunk-public-url/>
1212
</head>

site/content/assets.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,7 @@ This will typically look like: `<link data-trunk rel="{type}" href="{path}" ..ot
5555
- 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.
5656
- `data-integrity`: (optional) the `integrity` digest type for code & script resources. Defaults to plain `sha384`.
5757
- `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 `..`.
5859

5960
## tailwind
6061

src/pipelines/css.rs

Lines changed: 15 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
//! CSS asset pipeline.
22
33
use super::{AssetFile, AttrWriter, Attrs, TrunkAssetPipelineOutput, ATTR_HREF, ATTR_MINIFY};
4+
use crate::common::target_path;
45
use crate::{
56
config::RtcBuild,
67
pipelines::AssetFileType,
@@ -26,6 +27,8 @@ pub struct Css {
2627
integrity: IntegrityType,
2728
/// Whether to minify or not
2829
minify: bool,
30+
/// Optional target path inside the dist dir.
31+
target_path: Option<PathBuf>,
2932
}
3033

3134
impl Css {
@@ -49,13 +52,19 @@ impl Css {
4952

5053
let minify = attrs.get(ATTR_MINIFY).is_none();
5154

55+
let target_path = attrs
56+
.get("data-target-path")
57+
.map(|val| val.parse())
58+
.transpose()?;
59+
5260
Ok(Self {
5361
id,
5462
cfg,
5563
asset,
5664
attrs,
5765
integrity,
5866
minify,
67+
target_path,
5968
})
6069
}
6170

@@ -71,18 +80,17 @@ impl Css {
7180
let rel_path = crate::common::strip_prefix(&self.asset.path);
7281
tracing::debug!(path = ?rel_path, "copying & hashing css");
7382
let minify = self.cfg.release && self.minify && !self.cfg.no_minification;
83+
84+
let result_path =
85+
target_path(&self.cfg.staging_dist, self.target_path.as_deref(), None).await?;
86+
7487
let file = self
7588
.asset
76-
.copy(
77-
&self.cfg.staging_dist,
78-
self.cfg.filehash,
79-
minify,
80-
AssetFileType::Css,
81-
)
89+
.copy(&result_path, self.cfg.filehash, minify, AssetFileType::Css)
8290
.await?;
8391
tracing::debug!(path = ?rel_path, "finished copying & hashing css");
8492

85-
let result_file = self.cfg.staging_dist.join(&file);
93+
let result_file = result_path.join(&file);
8694
let integrity = OutputDigest::generate(self.integrity, || std::fs::read(&result_file))
8795
.with_context(|| {
8896
format!(

0 commit comments

Comments
 (0)