Skip to content

Commit

Permalink
feat(examples): add an example for using hooks
Browse files Browse the repository at this point in the history
  • Loading branch information
ctron committed May 24, 2024
1 parent 4af4d87 commit 0fcf271
Show file tree
Hide file tree
Showing 14 changed files with 417 additions and 1 deletion.
1 change: 1 addition & 0 deletions .github/workflows/ci.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -145,6 +145,7 @@ jobs:
os: [ ubuntu-latest, macos-latest, windows-latest ]
example:
- cdylib
- hooks
- initializer
- leptos
- no-rust
Expand Down
2 changes: 1 addition & 1 deletion Cargo.lock

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

2 changes: 2 additions & 0 deletions examples/hooks/.cargo/config.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
[alias]
xtask = "run --manifest-path ./xtask/Cargo.toml --"
154 changes: 154 additions & 0 deletions examples/hooks/Cargo.lock

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

23 changes: 23 additions & 0 deletions examples/hooks/Cargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
[package]
name = "hooks-examples"
version = "0.1.0"
authors = ["Jens Reimann <ctron@dentrassi.de>"]
edition = "2021"

[dependencies]
console_error_panic_hook = "0.1"
wasm-bindgen = "0.2"
web-sys = { version = "0.3", features = [
"console",
"Document",
"HtmlElement",
"Node",
"Text",
"Window",
] }


[features]
default = []
a = []

24 changes: 24 additions & 0 deletions examples/hooks/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
Trunk | Hooks
=========================
An example using different hooks and ideas to run code during the build.

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 and some output on the browser's console.

## Hook

There is one `xtask` based build hook. Check out [`cargo-xtask`](https://github.com/matklad/cargo-xtask) to get better
understanding of the pattern, as it's not an actual command you install.

The `xtask` is triggered by Trunk's hook system and has access to the `TRUNK_*` environment variables during the hook
execution.

# `build.rs`

You can also use a simple [`build.rs` build script](https://doc.rust-lang.org/cargo/reference/build-scripts.html). This
will be executed during the build of the main WASM application. It will be run on the host system though (not using
`wasm32-unknown-unknown`).

It does not have access to the Trunk env-vars, but does have access to all kind of `cargo` information, like the
features enabled during compilation. If you run this example with `trunk --features a`, you will see a different output
on the console.
12 changes: 12 additions & 0 deletions examples/hooks/Trunk.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
[watch]
ignore = [
"xtask/target"
]

# This uses the idea of cargo xtask, writing code in Rust rather than an OS dependent shell.
#
# Of course, you can also just use bash.
[[hooks]]
stage = "build"
command = "cargo"
command_arguments = ["xtask"]
13 changes: 13 additions & 0 deletions examples/hooks/build.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
fn main() {
let function = if std::env::var_os("CARGO_FEATURE_A").is_some() {
r#"function itDepends() {
console.log("Feature A is active");
}"#
} else {
r#"function itDepends() {
console.log("Feature A is not active");
}"#
};

std::fs::write("dist/.stage/buildGenerated.js", function).expect("must write");
}
27 changes: 27 additions & 0 deletions examples/hooks/index.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8"/>
<meta name="viewport" content="width=device-width, initial-scale=1"/>
<title>Trunk | Hooks | web-sys</title>

<link data-trunk rel="scss" href="src/index.scss"/>
<base data-trunk-public-url/>
</head>
<body>
<link data-trunk rel="rust" href="Cargo.toml" data-wasm-opt="z"/>

<!-- generated by xtask -->
<script src="generated.js"></script>
<script>
generatedFunction();
</script>

<!-- generated by build.rs -->
<script src="buildGenerated.js"></script>
<script>
itDepends();
</script>
</body>

</html>
9 changes: 9 additions & 0 deletions examples/hooks/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;
}
}
17 changes: 17 additions & 0 deletions examples/hooks/src/main.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
use console_error_panic_hook::set_once as set_panic_hook;
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");
}

fn main() {
set_panic_hook();
start_app();
}
Loading

0 comments on commit 0fcf271

Please sign in to comment.