forked from trunk-rs/trunk
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add data-type attribute to rust links
This allows web workers to be included in `index.html`. Since this makes the rust-worker attribute unnecessary it has been removed. Also adds an example showing basic usage and updates the documentation.
- Loading branch information
1 parent
d46ba85
commit f911eda
Showing
11 changed files
with
345 additions
and
83 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
[package] | ||
name = "webworker-example" | ||
version = "0.1.0" | ||
edition = "2021" | ||
|
||
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html | ||
|
||
[dependencies] | ||
console_error_panic_hook = "0.1" | ||
web-sys = { version = "0.3", features = ["Window", "Location", "Document", "HtmlElement", "Node", "Text", "Worker", "DedicatedWorkerGlobalScope"] } | ||
wasm-bindgen = "=0.2.74" | ||
wee_alloc = "0.4" | ||
js-sys = "0.3.55" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | Web worker</title> | ||
|
||
<base data-trunk-public-url /> | ||
</head> | ||
|
||
<body> | ||
<link data-trunk rel="rust" href="Cargo.toml" data-wasm-opt="z" data-bin="app" data-type="main" /> | ||
<link data-trunk rel="rust" href="Cargo.toml" data-wasm-opt="z" data-bin="worker" data-type="worker" /> | ||
</body> | ||
|
||
</html> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
#![recursion_limit = "1024"] | ||
|
||
#[global_allocator] | ||
static ALLOC: wee_alloc::WeeAlloc = wee_alloc::WeeAlloc::INIT; | ||
|
||
use console_error_panic_hook::set_once as set_panic_hook; | ||
use web_sys::{window, Worker}; | ||
|
||
fn worker_new(file_name: &str) -> Worker { | ||
let origin = window().unwrap().location().origin().unwrap(); | ||
let url = format!("{}/{}", origin, file_name); | ||
|
||
Worker::new(&url).expect("failed to spawn worker") | ||
} | ||
|
||
fn main() { | ||
set_panic_hook(); | ||
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 Rust!"); | ||
body.append_child(text_node.as_ref()).expect("Failed to append text"); | ||
|
||
let worker = worker_new("worker.js"); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
fn main() {} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.