Skip to content

Commit

Permalink
fix: handle projects which have both a bin and lib (cdylib) target
Browse files Browse the repository at this point in the history
In a previous change `cdylib` targets have been added to the list of
allowed artifacts. However, projects which are both a binary as well as
a library, the compilation failed now, since both target artifacts (bin
and lib) got added to the target output. It was not possible to select
by name.

This change adds the `data-target-name` attribute to specifically select
a target "by name", without having to use the `--bin` argument in cargo.

Closes: #630
  • Loading branch information
ctron committed Dec 18, 2023
1 parent 3f2c9e3 commit 09e7da6
Show file tree
Hide file tree
Showing 11 changed files with 304 additions and 21 deletions.
6 changes: 3 additions & 3 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -55,9 +55,9 @@ oxipng = "9"
parking_lot = "0.12"
remove_dir_all = "0.8"
reqwest = { version = "0.11", default-features = false, features = [
"rustls-tls-native-roots",
"stream",
"trust-dns",
"rustls-tls-native-roots",
"stream",
"trust-dns",
] }
sha2 = "0.10"
seahash = "4"
Expand Down
167 changes: 167 additions & 0 deletions examples/cdylib/Cargo.lock

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

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

[lib]
crate-type = ["cdylib", "rlib"]

[dependencies]
console_error_panic_hook = "0.1"
wasm-bindgen = "0.2"
wasm-bindgen-futures = "0.4"
web-sys = { version = "0.3", features = ["Window", "Document", "HtmlElement", "Node", "Text"] }
8 changes: 8 additions & 0 deletions examples/cdylib/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
Trunk | cdylib | web-sys
=========================
An example application demonstrating building a vanilla Rust (no frameworks) WASM web application using web-sys as a
library.

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.

![Rendered cdylib example](example-vanilla.png)
17 changes: 17 additions & 0 deletions examples/cdylib/index.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8"/>
<meta name="viewport" content="width=device-width, initial-scale=1"/>
<title>Trunk | cdylib | web-sys</title>

<link data-trunk rel="scss" href="src/index.scss"/>
<link data-trunk rel="css" href="src/app.css"/>
<base data-trunk-public-url/>
</head>
<body>
<link data-trunk rel="rust" href="Cargo.toml" />
<script data-trunk src="src/script.js"></script>
</body>
<script>testFromJavaScript();</script>
</html>
1 change: 1 addition & 0 deletions examples/cdylib/src/app.css
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
body {}
9 changes: 9 additions & 0 deletions examples/cdylib/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;
}
}
27 changes: 27 additions & 0 deletions examples/cdylib/src/lib.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
#![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 cdylib 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();
}

#[wasm_bindgen(start)]
pub async fn run() {
set_panic_hook();
snippetTest();
start_app();
}
3 changes: 3 additions & 0 deletions examples/cdylib/src/script.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
function testFromJavaScript() {
console.log("Hello from JavaScript");
}
3 changes: 2 additions & 1 deletion site/content/assets.md
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,8 @@ This will typically look like: `<link data-trunk rel="{type}" href="{path}" ..ot
`rel="rust"`: Trunk will compile the specified Cargo project as WASM and load it. This is optional. If not specified, Trunk will look for a `Cargo.toml` in the parent directory of the source HTML file.

- `href`: (optional) the path to the `Cargo.toml` of the Rust project. If a directory is specified, then Trunk will look for the `Cargo.toml` in the given directory. If no value is specified, then Trunk will look for a `Cargo.toml` in the parent directory of the source HTML file.
- `data-bin`: (optional) the name of the binary to compile and load. If the Cargo project has multiple binaries, this value will be required for proper functionality.
- `data-target-name`: (optional) the name of the target artifact to load. If the Cargo project has multiple targets (binaries and library), this value can be used to select which one should be used by trunk.
- `data-bin`: (optional) the name of the binary to compile and load. If the Cargo project has multiple binaries, this value can be used to specify that a specific binary should be compiled (using `--bin`) and used by trunk. This implicitly includes `data-target-name`.
- `data-type`: (optional) specifies how the binary should be loaded into the project. Can be set to `main` or `worker`. `main` is the default. There can only be one `main` link. For workers a wasm-bindgen javascript wrapper and the wasm file (with `_bg.wasm` suffix) is created, named after the binary name (if provided) or project name. See one of the webworker examples on how to load them.
- `data-cargo-features`: (optional) Space or comma separated list of cargo features to activate.
- `data-cargo-no-default-features`: (optional) Disables the default Cargo features.
Expand Down
Loading

0 comments on commit 09e7da6

Please sign in to comment.