Skip to content

Commit a90a264

Browse files
committed
feat: implement data-target-path for rust
1 parent ca2c48c commit a90a264

File tree

5 files changed

+40
-19
lines changed

5 files changed

+40
-19
lines changed

examples/target-path/index.html

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@
1212
</head>
1313
<body>
1414

15-
<link data-trunk rel="rust" href="Cargo.toml" data-wasm-opt="z" data-bin="vanilla-example"/>
15+
<link data-trunk rel="rust" href="Cargo.toml" data-wasm-opt="z" data-bin="vanilla-example" data-target-path="static"/>
1616

1717
<link data-trunk rel="copy-dir" href="assets" data-target-path="static"/>
1818
<link data-trunk rel="copy-file" href="more-assets/rustacean-flat-happy.svg" data-target-path="static"/>
@@ -31,13 +31,13 @@
3131

3232
<tr>
3333
<td><code>copy-dir</code></td>
34-
<td><img src="static/rustacean-flat-happy.png" width="128"/></td>
34+
<td><img src="static/rustacean-flat-happy.png" width="128" alt="Ferris the Crab"/></td>
3535
<td>Should see PNG image</td>
3636
</tr>
3737

3838
<tr>
3939
<td><code>copy-file</code></td>
40-
<td><img src="static/rustacean-flat-happy.svg" width="128"/></td>
40+
<td><img src="static/rustacean-flat-happy.svg" width="128" alt="Ferris the Crab"/></td>
4141
<td>Should see SVG image</td>
4242
</tr>
4343
</table>

src/common.rs

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -239,3 +239,10 @@ pub fn dist_relative(dist: &Path, target_file: &Path) -> Result<String> {
239239
.to_string_lossy()
240240
.to_string())
241241
}
242+
243+
pub fn apply_data_target_path(path: impl Into<String>, target_path: &Option<PathBuf>) -> String {
244+
match target_path {
245+
Some(target_path) => format!("{}/{}", target_path.display(), path.into()),
246+
None => path.into(),
247+
}
248+
}

src/pipelines/css.rs

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -52,9 +52,7 @@ impl Css {
5252
let asset = AssetFile::new(&html_dir, path).await?;
5353

5454
let integrity = IntegrityType::from_attrs(&attrs, &cfg)?;
55-
5655
let minify = attrs.get(ATTR_MINIFY).is_none();
57-
5856
let target_path = data_target_path(&attrs)?;
5957

6058
Ok(Self {

src/pipelines/rust/mod.rs

Lines changed: 29 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -7,11 +7,14 @@ mod wasm_opt;
77

88
pub use output::RustAppOutput;
99

10-
use super::{Attrs, TrunkAssetPipelineOutput, ATTR_HREF, SNIPPETS_DIR};
11-
use crate::pipelines::rust::sri::{SriBuilder, SriOptions, SriType};
10+
use super::{data_target_path, Attrs, TrunkAssetPipelineOutput, ATTR_HREF, SNIPPETS_DIR};
1211
use crate::{
13-
common::{self, check_target_not_found_err, copy_dir_recursive, path_exists},
12+
common::{
13+
self, apply_data_target_path, check_target_not_found_err, copy_dir_recursive, path_exists,
14+
target_path,
15+
},
1416
config::{CargoMetadata, CrossOrigin, Features, RtcBuild},
17+
pipelines::rust::sri::{SriBuilder, SriOptions, SriType},
1518
processing::{
1619
integrity::{IntegrityType, OutputDigest},
1720
minify::minify_js,
@@ -46,7 +49,7 @@ pub struct RustApp {
4649
cfg: Arc<RtcBuild>,
4750
/// The configuration of the features passed to cargo.
4851
cargo_features: Features,
49-
/// Is this module main or a worker.
52+
/// Is this module main or a worker?
5053
app_type: RustAppType,
5154
/// All metadata associated with the target Cargo project.
5255
manifest: CargoMetadata,
@@ -57,6 +60,8 @@ pub struct RustApp {
5760
bin: Option<String>,
5861
/// An optional filter for finding the target artifact.
5962
target_name: Option<String>,
63+
/// Optional target path inside the dist dir.
64+
target_path: Option<PathBuf>,
6065
/// An option to instruct wasm-bindgen to preserve debug info in the final WASM output, even
6166
/// for `--release` mode.
6267
keep_debug: bool,
@@ -231,6 +236,8 @@ impl RustApp {
231236
}
232237
});
233238

239+
let target_path = data_target_path(&attrs)?;
240+
234241
// done
235242

236243
Ok(Self {
@@ -256,6 +263,7 @@ impl RustApp {
256263
import_bindings,
257264
import_bindings_name,
258265
initializer,
266+
target_path,
259267
})
260268
}
261269

@@ -302,6 +310,7 @@ impl RustApp {
302310
import_bindings: true,
303311
import_bindings_name: None,
304312
initializer: None,
313+
target_path: None,
305314
}))
306315
}
307316

@@ -330,7 +339,7 @@ impl RustApp {
330339
// evaluate wasm integrity after all processing
331340
self.final_digest(&mut output)
332341
.await
333-
.context("finalizing digest")?;
342+
.with_context(|| format!("finalizing digest for '{}'", output.wasm_output))?;
334343

335344
// now the build is complete
336345
tracing::debug!("rust build complete");
@@ -513,6 +522,10 @@ impl RustApp {
513522
args.push("--no-typescript");
514523
}
515524

525+
// the final base
526+
let target_path =
527+
target_path(&self.cfg.staging_dist, self.target_path.as_deref(), None).await?;
528+
516529
// Invoke wasm-bindgen.
517530
tracing::debug!("calling wasm-bindgen for {}", self.name);
518531
common::run_command(wasm_bindgen_name, &wasm_bindgen, &args)
@@ -522,22 +535,25 @@ impl RustApp {
522535
// Copy the generated WASM & JS loader to the dist dir.
523536
tracing::debug!("copying generated wasm-bindgen artifacts");
524537
let hashed_name = self.hashed_wasm_base(wasm_path).await?;
525-
let hashed_wasm_name = format!("{hashed_name}_bg.wasm");
538+
let hashed_wasm_name =
539+
apply_data_target_path(format!("{hashed_name}_bg.wasm"), &self.target_path);
526540

527541
let js_name = format!("{}.js", self.name);
528-
let hashed_js_name = format!("{}.js", hashed_name);
542+
let hashed_js_name =
543+
apply_data_target_path(format!("{}.js", hashed_name), &self.target_path);
529544
let ts_name = format!("{}.d.ts", self.name);
530-
let hashed_ts_name = format!("{}.d.ts", hashed_name);
545+
let hashed_ts_name =
546+
apply_data_target_path(format!("{}.d.ts", hashed_name), &self.target_path);
531547

532548
let js_loader_path = bindgen_out.join(&js_name);
533549
let js_loader_path_dist = self.cfg.staging_dist.join(&hashed_js_name);
534550
let wasm_name = format!("{}_bg.wasm", self.name);
535551
let wasm_path = bindgen_out.join(&wasm_name);
536552
let wasm_path_dist = self.cfg.staging_dist.join(&hashed_wasm_name);
537553

538-
let hashed_loader_name = self
539-
.loader_shim
540-
.then(|| format!("{}_loader.js", hashed_name));
554+
let hashed_loader_name = self.loader_shim.then(|| {
555+
apply_data_target_path(format!("{}_loader.js", hashed_name), &self.target_path)
556+
});
541557
let loader_shim_path = hashed_loader_name
542558
.as_ref()
543559
.map(|m| self.cfg.staging_dist.join(m));
@@ -610,7 +626,7 @@ impl RustApp {
610626
// Check for any snippets, and copy them over.
611627
let snippets_dir_src = bindgen_out.join(SNIPPETS_DIR);
612628
let snippets = if path_exists(&snippets_dir_src).await? {
613-
let snippets_dir_dest = self.cfg.staging_dist.join(SNIPPETS_DIR);
629+
let snippets_dir_dest = target_path.join(SNIPPETS_DIR);
614630
tracing::debug!(
615631
"recursively copying from '{snippets_dir_src}' to '{}'",
616632
snippets_dir_dest.display()
@@ -757,7 +773,7 @@ impl RustApp {
757773
return false;
758774
}
759775

760-
// if we have a --bin argument
776+
// if we have the --bin argument
761777
if let Some(bin) = &self.bin {
762778
// it must match
763779
if bin != &art.target.name {

src/pipelines/rust/sri.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -71,7 +71,7 @@ impl SriBuilder {
7171
let name = name.into();
7272
let digest = OutputDigest::generate_async(self.r#type, source).await?;
7373
tracing::debug!(
74-
"Recording SRI record - type: {:?}. name: {name}, value: {digest:?}",
74+
"recording SRI record - type: {:?}. name: {name}, value: {digest:?}",
7575
self.r#type,
7676
);
7777
self.result

0 commit comments

Comments
 (0)