Skip to content

Commit 315bb3a

Browse files
committed
feat: add data-target-path to the copy-file action
1 parent 6594336 commit 315bb3a

18 files changed

+363
-23
lines changed

.github/workflows/ci.yaml

+3-2
Original file line numberDiff line numberDiff line change
@@ -74,7 +74,7 @@ jobs:
7474
strategy:
7575
fail-fast: false
7676
matrix:
77-
os: [ubuntu-latest, macos-latest, windows-latest]
77+
os: [ ubuntu-latest, macos-latest, windows-latest ]
7878
include:
7979
- os: ubuntu-latest
8080
binPath: target/debug/trunk
@@ -119,13 +119,14 @@ jobs:
119119
fail-fast: false
120120
max-parallel: 32
121121
matrix:
122-
os: [ubuntu-latest, macos-latest, windows-latest]
122+
os: [ ubuntu-latest, macos-latest, windows-latest ]
123123
example:
124124
- cdylib
125125
- initializer
126126
- no-rust
127127
- proxy
128128
- seed
129+
- target-path
129130
- vanilla
130131
- webworker
131132
- webworker-gloo

examples/target-path/Cargo.lock

+154
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

examples/target-path/Cargo.toml

+10
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
[package]
2+
name = "vanilla-example"
3+
version = "0.1.0"
4+
authors = ["Jens Reimann <[email protected]>"]
5+
edition = "2021"
6+
7+
[dependencies]
8+
console_error_panic_hook = "0.1"
9+
wasm-bindgen = "0.2"
10+
web-sys = { version = "0.3", features = ["Window", "Document", "HtmlElement", "Node", "Text"] }

examples/target-path/README.md

+7
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
Trunk | target-dirs
2+
=========================
3+
An example application demonstrating create a more complex target directory structure in the dist folder, based on
4+
the vanilla example.
5+
6+
Once you've installed Trunk, simply execute `trunk serve --open` from this example's directory, and you should see the
7+
web application rendered in your browser.

examples/target-path/Trunk.toml

+3
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
[build]
2+
target = "index.html"
3+
dist = "dist"
Loading

examples/target-path/index.html

+45
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
<!DOCTYPE html>
2+
<html>
3+
<head>
4+
<meta charset="utf-8"/>
5+
<meta name="viewport" content="width=device-width, initial-scale=1"/>
6+
<title>Trunk | target-path</title>
7+
8+
<link data-trunk rel="scss" href="src/index.scss"/>
9+
<link data-trunk rel="css" href="src/app.css"/>
10+
<link data-trunk data-no-minify rel="css" href="src/not_minified.css"/>
11+
<base data-trunk-public-url/>
12+
</head>
13+
<body>
14+
15+
<link data-trunk rel="rust" href="Cargo.toml" data-wasm-opt="z" data-bin="vanilla-example"/>
16+
17+
<link data-trunk rel="copy-dir" href="assets" data-target-path="static"/>
18+
<link data-trunk rel="copy-file" href="more-assets/rustacean-flat-happy.svg" data-target-path="static"/>
19+
20+
<script data-trunk src="src/script.js"></script>
21+
<script data-trunk src="src/script.mjs" type="module"></script>
22+
23+
<table>
24+
<tr>
25+
<th>Test</th>
26+
<th>Outcome</th>
27+
<th>Expected</th>
28+
</tr>
29+
30+
<tr>
31+
<td><code>copy-dir</code></td>
32+
<td><img src="static/rustacean-flat-happy.png" width="128"/></td>
33+
<td>Should see PNG image</td>
34+
</tr>
35+
36+
<tr>
37+
<td><code>copy-file</code></td>
38+
<td><img src="static/rustacean-flat-happy.svg" width="128"/></td>
39+
<td>Should see SVG image</td>
40+
</tr>
41+
</table>
42+
43+
</body>
44+
<script>testFromJavaScript();</script>
45+
</html>
Loading

examples/target-path/src/app.css

+1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
body {}

examples/target-path/src/index.scss

+9
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
@charset "utf-8";
2+
3+
html {
4+
body {
5+
font-size: 20pt;
6+
color: #111;
7+
font-family: sans-serif;
8+
}
9+
}

examples/target-path/src/main.rs

+26
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
#![recursion_limit = "1024"]
2+
3+
use console_error_panic_hook::set_once as set_panic_hook;
4+
use wasm_bindgen::prelude::*;
5+
use web_sys::window;
6+
7+
fn start_app() {
8+
let document = window()
9+
.and_then(|win| win.document())
10+
.expect("Could not access document");
11+
let body = document.body().expect("Could not access document.body");
12+
let text_node = document.create_text_node("Hello, world from Vanilla Rust!");
13+
body.append_child(text_node.as_ref())
14+
.expect("Failed to append text");
15+
}
16+
17+
#[wasm_bindgen(inline_js = "export function snippetTest() { console.log('Hello from JS FFI!'); }")]
18+
extern "C" {
19+
fn snippetTest();
20+
}
21+
22+
fn main() {
23+
set_panic_hook();
24+
snippetTest();
25+
start_app();
26+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
.should_not_be_minified:checked:hover {
2+
--empty-prop: ;
3+
background-color: black;
4+
}

examples/target-path/src/script.js

+3
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
function testFromJavaScript() {
2+
console.log("Hello from JavaScript");
3+
}

examples/target-path/src/script.mjs

+5
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
function testFromJavaScriptModule() {
2+
console.log("Hello from JavaScript Module");
3+
}
4+
5+
testFromJavaScriptModule();

site/content/assets.md

+2
Original file line numberDiff line numberDiff line change
@@ -82,6 +82,8 @@ This will typically look like: `<link data-trunk rel="{type}" href="{path}" ..ot
8282

8383
`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.
8484

85+
- `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 `..`.
86+
8587
## copy-dir
8688

8789
`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.

src/common.rs

+27-1
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ use std::ffi::OsStr;
88
use std::fmt::Debug;
99
use std::fs::Metadata;
1010
use std::io::ErrorKind;
11-
use std::path::{Path, PathBuf};
11+
use std::path::{Component, Path, PathBuf};
1212
use std::process::Stdio;
1313
use tokio::fs;
1414
use tokio::process::Command;
@@ -195,3 +195,29 @@ pub fn check_target_not_found_err(err: anyhow::Error, target: &str) -> anyhow::E
195195
_ => err,
196196
}
197197
}
198+
199+
/// Create a target path from a base and an optional relative prefix.
200+
///
201+
/// This is intended for cases where a subdirectory for a target base (like `dist`) is being
202+
/// composed. The target directory will also be created.
203+
pub async fn target_path(
204+
base: &Path,
205+
target_path: Option<&Path>,
206+
default: Option<&OsStr>,
207+
) -> Result<PathBuf> {
208+
if let Some(path) = target_path {
209+
if path.is_absolute() || path.components().any(|c| matches!(c, Component::ParentDir)) {
210+
bail!(
211+
"Invalid data-target-path '{}'. Must be a relative path without '..'.",
212+
path.display()
213+
);
214+
}
215+
let dir_out = base.join(path);
216+
tokio::fs::create_dir_all(&dir_out).await?;
217+
Ok(dir_out)
218+
} else if let Some(default) = default {
219+
Ok(base.join(default))
220+
} else {
221+
Ok(base.to_owned())
222+
}
223+
}

0 commit comments

Comments
 (0)