Skip to content

Commit

Permalink
Add bazel build for wasm-ui-example
Browse files Browse the repository at this point in the history
  • Loading branch information
kolloch authored and Peter Kolloch committed Oct 4, 2024
1 parent e6371d8 commit 7d847f1
Show file tree
Hide file tree
Showing 18 changed files with 1,425 additions and 44 deletions.
2 changes: 2 additions & 0 deletions examples/bzlmod/wasm-ui/.bazelignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
/dist/
/target/
25 changes: 25 additions & 0 deletions examples/bzlmod/wasm-ui/.bazelrc
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
# Required on windows
common --enable_platform_specific_config
startup --windows_enable_symlinks
build:windows --enable_runfiles

build --experimental_enable_bzlmod

# This isn't currently the defaut in Bazel, but we enable it to test we'll be ready if/when it flips.
build --incompatible_disallow_empty_glob

# Required for cargo_build_script support before Bazel 7
build --incompatible_merge_fixed_and_default_shell_env

# Use a static value for `PATH` and does not inherit `LD_LIBRARY_PATH`. Doesn't let environment
# variables like `PATH` sneak into the build, which can cause massive cache misses when they change.
# Use `--action_env=ENV_VARIABLE` if you want to inherit specific environment variables from the
# client, but note that doing so can prevent cross-user caching if a shared cache is used.
# Docs: https://bazel.build/reference/command-line-reference#flag--incompatible_strict_action_env
build --incompatible_strict_action_env

# Speed up all builds by not checking if external repository files have been modified.
# Docs: https://github.com/bazelbuild/bazel/blob/1af61b21df99edc2fc66939cdf14449c2661f873/src/main/java/com/google/devtools/build/lib/bazel/repository/RepositoryOptions.java#L244
build --noexperimental_check_external_repository_files
fetch --noexperimental_check_external_repository_files
query --noexperimental_check_external_repository_files
1 change: 1 addition & 0 deletions examples/bzlmod/wasm-ui/.bazelversion
2 changes: 2 additions & 0 deletions examples/bzlmod/wasm-ui/.gitignore
Original file line number Diff line number Diff line change
@@ -1,2 +1,4 @@
/dist/
/target/

/bazel-*
38 changes: 38 additions & 0 deletions examples/bzlmod/wasm-ui/BUILD.bazel
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
load("@aspect_bazel_lib//lib:copy_to_directory.bzl", "copy_to_directory")
load("@crates_ui//:defs.bzl", "all_crate_deps")
load("@rules_rust//rust:defs.bzl", "rust_binary")
load("@rules_rust//wasm_bindgen:defs.bzl", "rust_wasm_bindgen")

package(default_visibility = ["//visibility:public"])

rust_binary(
name = "ui-wasm",
srcs = [
"src/app.rs",
"src/main.rs",
],
target_compatible_with = [
"@platforms//cpu:wasm32",
],
deps = all_crate_deps(normal = True),
)

rust_wasm_bindgen(
name = "ui-bindgen",
target = "web",
wasm_file = "ui-wasm",
)

copy_to_directory(
name = "ui-http-root",
srcs = [
"index.css",
"index.html",
":ui-bindgen",
],
include_srcs_patterns = [
"ui-bindgen_bg.wasm",
"ui-bindgen.js",
"index*",
],
)
34 changes: 17 additions & 17 deletions examples/bzlmod/wasm-ui/Cargo.lock

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

9 changes: 4 additions & 5 deletions examples/bzlmod/wasm-ui/Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,14 +1,13 @@
[package]
name = "trunk-template"
name = "wasm-ui"
version = "0.1.0"
edition = "2021"
description = "Template for starting a Yew project using Trunk"
description = "From template for starting a Yew project using Trunk"
readme = "README.md"
repository = "https://github.com/yewstack/yew-trunk-minimal-template"
license = "MIT OR Apache-2.0"
keywords = ["yew", "trunk"]
keywords = ["yew", "trunk", "bazel"]
categories = ["gui", "wasm", "web-programming"]

# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
[dependencies]
yew = { version="0.20", features=["csr"] }
yew = { version = "0.20", features = ["csr"] }
56 changes: 56 additions & 0 deletions examples/bzlmod/wasm-ui/MODULE.bazel
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
"""bazelbuild/rules_rust - bzlmod example"""

module(
name = "all_crate_deps_bzlmod_example",
version = "0.0.0",
)

bazel_dep(name = "platforms", version = "0.0.10")
bazel_dep(
name = "bazel_skylib",
version = "1.6.1",
)
bazel_dep(name = "aspect_bazel_lib", version = "2.9.1")
bazel_dep(
name = "rules_rust",
version = "0.0.0",
)
local_path_override(
module_name = "rules_rust",
path = "../../..",
)

rust = use_extension("@rules_rust//rust:extensions.bzl", "rust")
rust.toolchain(edition = "2021")
use_repo(
rust,
"rust_toolchains",
)

register_toolchains("@rust_toolchains//:all")

wasm_bindgen = use_extension("@rules_rust//wasm_bindgen:extensions.bzl", "wasm_bindgen")
use_repo(
wasm_bindgen,
)

register_toolchains(
"@rules_rust//wasm_bindgen:default_wasm_bindgen_toolchain",
"@rules_rust//rust/private/dummy_cc_toolchain:dummy_cc_wasm32_toolchain",
)

crate = use_extension(
"@rules_rust//crate_universe:extension.bzl",
"crate",
)
crate.from_cargo(
name = "crates_serve",
cargo_lockfile = "//serve:Cargo.lock",
manifests = ["//serve:Cargo.toml"],
)
crate.from_cargo(
name = "crates_ui",
cargo_lockfile = "//:Cargo.lock",
manifests = ["//:Cargo.toml"],
)
use_repo(crate, "crates_serve", "crates_ui")
30 changes: 16 additions & 14 deletions examples/bzlmod/wasm-ui/README.md
Original file line number Diff line number Diff line change
@@ -1,10 +1,21 @@
# Yew Trunk Template
# Yew Template - with Bazel/Trunk

This is a fairly minimal template for a Yew app that's built with [Trunk].
This was generated according to the getting started instructions of [yew](https://yew.rs/)
and then enriched with Bazel build rules.

## Usage
## Bazel

For a more thorough explanation of Trunk and its features, please head over to the [repository][trunk].
Build and serve the UI with:

```bash
bazel run //serve
```

This also builds a small rust server application to serve the files.

## Usage (Trunk)

For a more thorough explanation of Trunk and its features, please head over to the [Trunk](https://trunkrs.dev/).

### Installation

Expand All @@ -18,7 +29,7 @@ If you don't already have it, install it with the following command:
rustup target add wasm32-unknown-unknown
```

Now that we have our basics covered, it's time to install the star of the show: [Trunk].
Now that we have our basics covered, it's time to install the star of the show: [Trunk](https://trunkrs.dev/).
Simply run the following command to install it:

```bash
Expand Down Expand Up @@ -63,12 +74,3 @@ Update the `name`, `version`, `description` and `repository` fields in the [Carg
The [index.html](index.html) file also contains a `<title>` tag that needs updating.

Finally, you should update this very `README` file to be about your app.

### License

The template ships with both the Apache and MIT license.
If you don't want to have your app dual licensed, just remove one (or both) of the files and update the `license` field in `Cargo.toml`.

There are two empty spaces in the MIT license you need to fill out: `` and `Peter Kolloch <peter.kolloch@nexxiot.com>`.

[trunk]: https://github.com/thedodd/trunk
1 change: 1 addition & 0 deletions examples/bzlmod/wasm-ui/WORKSPACE.bazel
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
# Intentionally blank; using bzlmod
1 change: 1 addition & 0 deletions examples/bzlmod/wasm-ui/WORKSPACE.bzlmod
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
# Intentionally blank; enable strict mode for bzlmod
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
@charset "UTF-8";
html,
body {
height: 100%;
Expand All @@ -8,7 +9,6 @@ body {
align-items: center;
display: flex;
justify-content: center;

background: linear-gradient(to bottom right, #444444, #009a5b);
font-size: 1.5rem;
}
Expand All @@ -25,7 +25,6 @@ main {

.heart:after {
content: "❤️";

font-size: 1.75em;
}

Expand Down
30 changes: 24 additions & 6 deletions examples/bzlmod/wasm-ui/index.html
Original file line number Diff line number Diff line change
@@ -1,8 +1,26 @@
<!DOCTYPE html>
<!doctype html>
<html>
<head>
<meta charset="utf-8" />
<title>Trunk Template</title>
<link data-trunk rel="sass" href="index.scss" />
</head>
<head>
<meta charset="utf-8" />
<title>Yew build with bazel</title>
<link
rel="icon"
href="data:image/svg+xml,<svg xmlns=%22http://www.w3.org/2000/svg%22 viewBox=%220 0 100 100%22><text y=%22.9em%22 font-size=%2290%22>🚀</text></svg>"
/>
<link rel="stylesheet" href="/index.css" />
<link
rel="preload"
href="/ui-bindgen_bg.wasm"
as="fetch"
type="application/wasm"
crossorigin=""
/>
<link rel="modulepreload" href="/ui-bindgen.js" />
</head>
<body>
<script type="module">
import init from "/ui-bindgen.js";
init("/ui-bindgen_bg.wasm");
</script>
</body>
</html>
24 changes: 24 additions & 0 deletions examples/bzlmod/wasm-ui/serve/BUILD.bazel
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
load("@crates_serve//:defs.bzl", "all_crate_deps")
load("@rules_rust//rust:defs.bzl", "rust_binary")
load(":binary_runner.bzl", "binary_runner")

package(default_visibility = ["//visibility:public"])

binary_runner(
name = "serve",
binary = ":serve_bin",
data = [
"//:ui-http-root",
],
env = {
"HTTP_ROOT": "$(rootpath //:ui-http-root)",
},
)

rust_binary(
name = "serve_bin",
srcs = [
"src/main.rs",
],
deps = all_crate_deps(normal = True),
)
Loading

0 comments on commit 7d847f1

Please sign in to comment.