Skip to content

Commit 0fcf271

Browse files
committed
feat(examples): add an example for using hooks
1 parent 4af4d87 commit 0fcf271

File tree

14 files changed

+417
-1
lines changed

14 files changed

+417
-1
lines changed

.github/workflows/ci.yaml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -145,6 +145,7 @@ jobs:
145145
os: [ ubuntu-latest, macos-latest, windows-latest ]
146146
example:
147147
- cdylib
148+
- hooks
148149
- initializer
149150
- leptos
150151
- no-rust

Cargo.lock

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

examples/hooks/.cargo/config.toml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
[alias]
2+
xtask = "run --manifest-path ./xtask/Cargo.toml --"

examples/hooks/Cargo.lock

Lines changed: 154 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

examples/hooks/Cargo.toml

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
[package]
2+
name = "hooks-examples"
3+
version = "0.1.0"
4+
authors = ["Jens Reimann <ctron@dentrassi.de>"]
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 = [
11+
"console",
12+
"Document",
13+
"HtmlElement",
14+
"Node",
15+
"Text",
16+
"Window",
17+
] }
18+
19+
20+
[features]
21+
default = []
22+
a = []
23+

examples/hooks/README.md

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
Trunk | Hooks
2+
=========================
3+
An example using different hooks and ideas to run code during the build.
4+
5+
Once you've installed Trunk, simply execute `trunk serve --open` from this example's directory, and you should see the
6+
web application rendered in your browser and some output on the browser's console.
7+
8+
## Hook
9+
10+
There is one `xtask` based build hook. Check out [`cargo-xtask`](https://github.com/matklad/cargo-xtask) to get better
11+
understanding of the pattern, as it's not an actual command you install.
12+
13+
The `xtask` is triggered by Trunk's hook system and has access to the `TRUNK_*` environment variables during the hook
14+
execution.
15+
16+
# `build.rs`
17+
18+
You can also use a simple [`build.rs` build script](https://doc.rust-lang.org/cargo/reference/build-scripts.html). This
19+
will be executed during the build of the main WASM application. It will be run on the host system though (not using
20+
`wasm32-unknown-unknown`).
21+
22+
It does not have access to the Trunk env-vars, but does have access to all kind of `cargo` information, like the
23+
features enabled during compilation. If you run this example with `trunk --features a`, you will see a different output
24+
on the console.

examples/hooks/Trunk.toml

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
[watch]
2+
ignore = [
3+
"xtask/target"
4+
]
5+
6+
# This uses the idea of cargo xtask, writing code in Rust rather than an OS dependent shell.
7+
#
8+
# Of course, you can also just use bash.
9+
[[hooks]]
10+
stage = "build"
11+
command = "cargo"
12+
command_arguments = ["xtask"]

examples/hooks/build.rs

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
fn main() {
2+
let function = if std::env::var_os("CARGO_FEATURE_A").is_some() {
3+
r#"function itDepends() {
4+
console.log("Feature A is active");
5+
}"#
6+
} else {
7+
r#"function itDepends() {
8+
console.log("Feature A is not active");
9+
}"#
10+
};
11+
12+
std::fs::write("dist/.stage/buildGenerated.js", function).expect("must write");
13+
}

examples/hooks/index.html

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
<!DOCTYPE html>
2+
<html lang="en">
3+
<head>
4+
<meta charset="utf-8"/>
5+
<meta name="viewport" content="width=device-width, initial-scale=1"/>
6+
<title>Trunk | Hooks | web-sys</title>
7+
8+
<link data-trunk rel="scss" href="src/index.scss"/>
9+
<base data-trunk-public-url/>
10+
</head>
11+
<body>
12+
<link data-trunk rel="rust" href="Cargo.toml" data-wasm-opt="z"/>
13+
14+
<!-- generated by xtask -->
15+
<script src="generated.js"></script>
16+
<script>
17+
generatedFunction();
18+
</script>
19+
20+
<!-- generated by build.rs -->
21+
<script src="buildGenerated.js"></script>
22+
<script>
23+
itDepends();
24+
</script>
25+
</body>
26+
27+
</html>

examples/hooks/src/index.scss

Lines changed: 9 additions & 0 deletions
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+
}

0 commit comments

Comments
 (0)