Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: add data-target-path to the copy-file action #724

Merged
merged 1 commit into from
Feb 26, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 3 additions & 2 deletions .github/workflows/ci.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,7 @@ jobs:
strategy:
fail-fast: false
matrix:
os: [ubuntu-latest, macos-latest, windows-latest]
os: [ ubuntu-latest, macos-latest, windows-latest ]
include:
- os: ubuntu-latest
binPath: target/debug/trunk
Expand Down Expand Up @@ -119,13 +119,14 @@ jobs:
fail-fast: false
max-parallel: 32
matrix:
os: [ubuntu-latest, macos-latest, windows-latest]
os: [ ubuntu-latest, macos-latest, windows-latest ]
example:
- cdylib
- initializer
- no-rust
- proxy
- seed
- target-path
- vanilla
- webworker
- webworker-gloo
Expand Down
154 changes: 154 additions & 0 deletions examples/target-path/Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

10 changes: 10 additions & 0 deletions examples/target-path/Cargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
[package]
name = "vanilla-example"
version = "0.1.0"
authors = ["Jens Reimann <[email protected]>"]
edition = "2021"

[dependencies]
console_error_panic_hook = "0.1"
wasm-bindgen = "0.2"
web-sys = { version = "0.3", features = ["Window", "Document", "HtmlElement", "Node", "Text"] }
7 changes: 7 additions & 0 deletions examples/target-path/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
Trunk | target-dirs
=========================
An example application demonstrating create a more complex target directory structure in the dist folder, based on
the vanilla example.

Once you've installed Trunk, simply execute `trunk serve --open` from this example's directory, and you should see the
web application rendered in your browser.
3 changes: 3 additions & 0 deletions examples/target-path/Trunk.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
[build]
target = "index.html"
dist = "dist"
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
45 changes: 45 additions & 0 deletions examples/target-path/index.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8"/>
<meta name="viewport" content="width=device-width, initial-scale=1"/>
<title>Trunk | target-path</title>

<link data-trunk rel="scss" href="src/index.scss"/>
<link data-trunk rel="css" href="src/app.css"/>
<link data-trunk data-no-minify rel="css" href="src/not_minified.css"/>
<base data-trunk-public-url/>
</head>
<body>

<link data-trunk rel="rust" href="Cargo.toml" data-wasm-opt="z" data-bin="vanilla-example"/>

<link data-trunk rel="copy-dir" href="assets" data-target-path="static"/>
<link data-trunk rel="copy-file" href="more-assets/rustacean-flat-happy.svg" data-target-path="static"/>

<script data-trunk src="src/script.js"></script>
<script data-trunk src="src/script.mjs" type="module"></script>

<table>
<tr>
<th>Test</th>
<th>Outcome</th>
<th>Expected</th>
</tr>

<tr>
<td><code>copy-dir</code></td>
<td><img src="static/rustacean-flat-happy.png" width="128"/></td>
<td>Should see PNG image</td>
</tr>

<tr>
<td><code>copy-file</code></td>
<td><img src="static/rustacean-flat-happy.svg" width="128"/></td>
<td>Should see SVG image</td>
</tr>
</table>

</body>
<script>testFromJavaScript();</script>
</html>
33 changes: 33 additions & 0 deletions examples/target-path/more-assets/rustacean-flat-happy.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
1 change: 1 addition & 0 deletions examples/target-path/src/app.css
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
body {}
9 changes: 9 additions & 0 deletions examples/target-path/src/index.scss
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
@charset "utf-8";

html {
body {
font-size: 20pt;
color: #111;
font-family: sans-serif;
}
}
26 changes: 26 additions & 0 deletions examples/target-path/src/main.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
#![recursion_limit = "1024"]

use console_error_panic_hook::set_once as set_panic_hook;
use wasm_bindgen::prelude::*;
use web_sys::window;

fn start_app() {
let document = window()
.and_then(|win| win.document())
.expect("Could not access document");
let body = document.body().expect("Could not access document.body");
let text_node = document.create_text_node("Hello, world from Vanilla Rust!");
body.append_child(text_node.as_ref())
.expect("Failed to append text");
}

#[wasm_bindgen(inline_js = "export function snippetTest() { console.log('Hello from JS FFI!'); }")]
extern "C" {
fn snippetTest();
}

fn main() {
set_panic_hook();
snippetTest();
start_app();
}
4 changes: 4 additions & 0 deletions examples/target-path/src/not_minified.css
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
.should_not_be_minified:checked:hover {
--empty-prop: ;
background-color: black;
}
3 changes: 3 additions & 0 deletions examples/target-path/src/script.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
function testFromJavaScript() {
console.log("Hello from JavaScript");
}
5 changes: 5 additions & 0 deletions examples/target-path/src/script.mjs
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
function testFromJavaScriptModule() {
console.log("Hello from JavaScript Module");
}

testFromJavaScriptModule();
2 changes: 2 additions & 0 deletions site/content/assets.md
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,8 @@ This will typically look like: `<link data-trunk rel="{type}" href="{path}" ..ot

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

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

## copy-dir

✅ `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.
Expand Down
28 changes: 27 additions & 1 deletion src/common.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ use std::ffi::OsStr;
use std::fmt::Debug;
use std::fs::Metadata;
use std::io::ErrorKind;
use std::path::{Path, PathBuf};
use std::path::{Component, Path, PathBuf};
use std::process::Stdio;
use tokio::fs;
use tokio::process::Command;
Expand Down Expand Up @@ -195,3 +195,29 @@ pub fn check_target_not_found_err(err: anyhow::Error, target: &str) -> anyhow::E
_ => err,
}
}

/// Create a target path from a base and an optional relative prefix.
///
/// This is intended for cases where a subdirectory for a target base (like `dist`) is being
/// composed. The target directory will also be created.
pub async fn target_path(
base: &Path,
target_path: Option<&Path>,
default: Option<&OsStr>,
) -> Result<PathBuf> {
if let Some(path) = target_path {
if path.is_absolute() || path.components().any(|c| matches!(c, Component::ParentDir)) {
bail!(
"Invalid data-target-path '{}'. Must be a relative path without '..'.",
path.display()
);
}
let dir_out = base.join(path);
tokio::fs::create_dir_all(&dir_out).await?;
Ok(dir_out)
} else if let Some(default) = default {
Ok(base.join(default))
} else {
Ok(base.to_owned())
}
}
Loading