Skip to content

Commit 9b8df94

Browse files
committed
feat: add data-target-path to the script asset
1 parent dbf782c commit 9b8df94

File tree

2 files changed

+15
-3
lines changed

2 files changed

+15
-3
lines changed

site/content/assets.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -109,6 +109,7 @@ This will typically look like: `<script data-trunk src="{path}" ..other options
109109
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.
110110

111111
- `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 `..`.
112113

113114
## JS Snippets
114115

src/pipelines/js.rs

Lines changed: 14 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,9 @@
11
//! JS asset pipeline.
22
3-
use super::{AssetFile, AttrWriter, Attrs, TrunkAssetPipelineOutput, ATTR_MINIFY, ATTR_SRC};
3+
use super::{
4+
data_target_path, AssetFile, AttrWriter, Attrs, TrunkAssetPipelineOutput, ATTR_MINIFY, ATTR_SRC,
5+
};
6+
use crate::common::target_path;
47
use crate::{
58
config::RtcBuild,
69
pipelines::AssetFileType,
@@ -28,6 +31,8 @@ pub struct Js {
2831
module: bool,
2932
/// Whether to minify or not
3033
minify: bool,
34+
/// Optional target path inside the dist dir.
35+
target_path: Option<PathBuf>,
3136
}
3237

3338
impl Js {
@@ -48,6 +53,7 @@ impl Js {
4853
let integrity = IntegrityType::from_attrs(&attrs, &cfg)?;
4954
let module = attrs.get("type").map(|s| s.as_str()) == Some("module");
5055
let minify = attrs.get(ATTR_MINIFY).is_none();
56+
let target_path = data_target_path(&attrs)?;
5157

5258
Ok(Self {
5359
id,
@@ -57,6 +63,7 @@ impl Js {
5763
attrs,
5864
integrity,
5965
minify,
66+
target_path,
6067
})
6168
}
6269

@@ -72,10 +79,14 @@ impl Js {
7279
let rel_path = crate::common::strip_prefix(&self.asset.path);
7380
tracing::debug!(path = ?rel_path, "copying & hashing js");
7481
let minify = self.cfg.release && self.minify && !self.cfg.no_minification;
82+
83+
let result_dir =
84+
target_path(&self.cfg.staging_dist, self.target_path.as_deref(), None).await?;
85+
7586
let file = self
7687
.asset
7788
.copy(
78-
&self.cfg.staging_dist,
89+
&result_dir,
7990
self.cfg.filehash,
8091
minify,
8192
if self.module {
@@ -87,7 +98,7 @@ impl Js {
8798
.await?;
8899
tracing::debug!(path = ?rel_path, "finished copying & hashing js");
89100

90-
let result_file = self.cfg.staging_dist.join(&file);
101+
let result_file = result_dir.join(&file);
91102
let integrity = OutputDigest::generate(self.integrity, || std::fs::read(&result_file))
92103
.with_context(|| {
93104
format!(

0 commit comments

Comments
 (0)