Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Make the wasm-bindgen flag --target configurable #20

Closed
wants to merge 3 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions .github/workflows/ci.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,10 @@ name: CI
on:
push:
branches:
- master
- main
pull_request:
branches:
- master
- main
jobs:
lint:
name: lint
Expand Down
2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ authors = [
"Anthony Dodd <[email protected]>",
"Jens Reimann <[email protected]>"
]
repository = "https://github.com/thedodd/trunk"
repository = "https://github.com/trunk-dev/trunk"
readme = "README.md"
categories = ["command-line-utilities", "wasm", "web-programming"]
keywords = ["wasm", "bundler", "web", "build-tool", "compiler"]
Expand Down
8 changes: 4 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
# Trunk

[![Build Status](https://github.com/thedodd/trunk/actions/workflows/ci.yaml/badge.svg?branch=trunk)](https://github.com/thedodd/trunk/actions)
[![Build Status](https://github.com/trunk-dev/trunk/actions/workflows/ci.yaml/badge.svg?branch=trunk)](https://github.com/trunk-dev/trunk/actions)
[![](https://img.shields.io/crates/v/trunk.svg?color=brightgreen&style=flat-square)](https://crates.io/crates/trunk)
![](https://img.shields.io/badge/license-MIT%2FApache--2.0-blue?style=flat-square)
[![Discord Chat](https://img.shields.io/discord/793890238267260958?logo=discord&style=flat-square)](https://discord.gg/JEPdBujTDr)
[![](https://img.shields.io/crates/d/trunk?label=downloads%20%28crates.io%29&style=flat-square)](https://crates.io/crates/trunk)
[![](https://img.shields.io/github/downloads/thedodd/trunk/total?label=downloads%20%28GH%29&style=flat-square)](https://github.com/thedodd/trunk/releases)
[![](https://img.shields.io/github/downloads/trunk-dev/trunk/total?label=downloads%20%28GH%29&style=flat-square)](https://github.com/trunk-dev/trunk/releases)
![](https://img.shields.io/homebrew/installs/dy/trunk?color=brightgreen&label=downloads%20%28brew%29&style=flat-square)

**Build, bundle & ship your Rust WASM application to the web.**
Expand All @@ -23,9 +23,9 @@ Trunk is a WASM web application bundler for Rust. Trunk uses a simple, optional-
Head on over to the [Trunk website](https://trunkrs.dev), everything you need is there. A few quick links:

- [Install](https://trunkrs.dev/#install)
- Download a released binary: https://github.com/thedodd/trunk/releases
- Download a released binary: https://github.com/trunk-dev/trunk/releases
- `cargo binstall trunk`
- `cargo install --git https://github.com/thedodd/trunk trunk` (most recent from git)
- `cargo install --git https://github.com/trunk-dev/trunk trunk` (most recent from git)
- `cargo install --path . trunk` (Most recent from local directory)
- [App Setup](https://trunkrs.dev//#app-setup)
- [Assets](https://trunkrs.dev/assets/)
Expand Down
5 changes: 3 additions & 2 deletions examples/yew-tailwindcss/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -22,9 +22,10 @@ impl Component for App {
}

fn view(&self) -> Html {
let link_classes = "block px-4 py-2 hover:bg-black hover:text-white rounded border-black border";
let link_classes =
"block px-4 py-2 hover:bg-black hover:text-white rounded border-black border";
let links = [
("Trunk", "https://github.com/thedodd/trunk"),
("Trunk", "https://github.com/trunk-dev/trunk"),
("Yew", "https://yew.rs/"),
("Tailwind", "https://tailwindcss.com"),
];
Expand Down
2 changes: 1 addition & 1 deletion examples/yew/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ impl Component for App {
navend=html!{
<>
<ybc::NavbarItem>
<ybc::ButtonAnchor classes=classes!("is-black", "is-outlined") rel=String::from("noopener noreferrer") target=String::from("_blank") href="https://github.com/thedodd/trunk">
<ybc::ButtonAnchor classes=classes!("is-black", "is-outlined") rel=String::from("noopener noreferrer") target=String::from("_blank") href="https://github.com/trunk-dev/trunk">
{"Trunk"}
</ybc::ButtonAnchor>
</ybc::NavbarItem>
Expand Down
2 changes: 1 addition & 1 deletion site/config.toml
Original file line number Diff line number Diff line change
Expand Up @@ -15,5 +15,5 @@ favicon = "rustacean-flat-happy.svg"
juice_logo_name = "Trunk"
juice_logo_path = "rustacean-flat-happy.svg"
juice_extra_menu = [
{ title = "Github", link = "https://github.com/thedodd/trunk"}
{ title = "Github", link = "https://github.com/trunk-dev/trunk"}
]
6 changes: 3 additions & 3 deletions site/content/_index.md
Original file line number Diff line number Diff line change
Expand Up @@ -29,12 +29,12 @@ cargo binstall trunk

### GitHub release download

Fetch and unpack a released binary from the [release page](https://github.com/thedodd/trunk/releases).
Fetch and unpack a released binary from the [release page](https://github.com/trunk-dev/trunk/releases).

For example (be sure to check for the most recent version):

```shell
wget -qO- https://github.com/thedodd/trunk/releases/download/0.17.10/trunk-x86_64-unknown-linux-gnu.tar.gz | tar -xzf-
wget -qO- https://github.com/trunk-dev/trunk/releases/download/0.17.10/trunk-x86_64-unknown-linux-gnu.tar.gz | tar -xzf-
```

### NixOS
Expand Down Expand Up @@ -124,7 +124,7 @@ That's not all! Trunk has even more useful features. Head on over to the followi

# Contributing

Anyone and everyone is welcome to contribute! Please review the [CONTRIBUTING.md](https://github.com/thedodd/trunk/blob/master/CONTRIBUTING.md) document for more details. The best way to get started is to find an open issue, and then start hacking on implementing it. Letting other folks know that you are working on it, and sharing progress is a great approach. Open pull requests early and often, and please use GitHub's draft pull request feature.
Anyone and everyone is welcome to contribute! Please review the [CONTRIBUTING.md](https://github.com/trunk-dev/trunk/blob/master/CONTRIBUTING.md) document for more details. The best way to get started is to find an open issue, and then start hacking on implementing it. Letting other folks know that you are working on it, and sharing progress is a great approach. Open pull requests early and often, and please use GitHub's draft pull request feature.

# License

Expand Down
1 change: 1 addition & 0 deletions site/content/assets.md
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ This will typically look like: `<link data-trunk rel="{type}" href="{path}" ..ot
- `data-reference-types`: (optional) instruct `wasm-bindgen` to enable [reference types](https://rustwasm.github.io/docs/wasm-bindgen/reference/reference-types.html).
- `data-weak-refs`: (optional) instruct `wasm-bindgen` to enable [weak references](https://rustwasm.github.io/docs/wasm-bindgen/reference/weak-references.html).
- `data-typescript`: (optional) instruct `wasm-bindgen` to output Typescript bindings. Defaults to false.
- `data-bindgen-target`: (optional) specifies the value of the `wasm-bindgen` flag [`--target`](https://rustwasm.github.io/wasm-bindgen/reference/deployment.html) (see link for possible values). Defaults to `web`. The main use-case is to switch to `no-modules` with `data-type="worker"` for backwards [compatibility](https://caniuse.com/mdn-api_worker_worker_ecmascript_modules) but with some [disadvantages](https://rustwasm.github.io/wasm-bindgen/examples/without-a-bundler.html?highlight=no-modules#using-the-older---target-no-modules).
- `data-loader-shim`: (optional) instruct `trunk` to create a loader shim for web workers. Defaults to false.
- `data-cross-origin`: (optional) the `crossorigin` setting when loading the code & script resources. Defaults to plain `anonymous`.
- `data-integrity`: (optional) the `integrity` digest type for code & script resources. Defaults to plain `sha384`.
Expand Down
2 changes: 1 addition & 1 deletion site/content/configuration.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ weight = 2
Trunk supports a layered config system. At the base, a config file can encapsulate project specific defaults, paths, ports and other config. Environment variables can be used to overwrite config file values. Lastly, CLI arguments / options take final precedence.

# Trunk.toml
Trunk supports an optional `Trunk.toml` config file. An example config file is included [in the Trunk repo](https://github.com/thedodd/trunk/blob/master/Trunk.toml), and shows all available config options along with their default values. By default, Trunk will look for a `Trunk.toml` config file in the current working directory. Trunk supports the global `--config` option to specify an alternative location for the file.
Trunk supports an optional `Trunk.toml` config file. An example config file is included [in the Trunk repo](https://github.com/trunk-dev/trunk/blob/master/Trunk.toml), and shows all available config options along with their default values. By default, Trunk will look for a `Trunk.toml` config file in the current working directory. Trunk supports the global `--config` option to specify an alternative location for the file.

Note that any relative paths declared in a `Trunk.toml` file will be treated as being relative to the `Trunk.toml` file itself.

Expand Down
2 changes: 1 addition & 1 deletion site/templates/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ <h3 class="title-text">
<footer>
<p>
<small class="subtext text-center">
<a href="https://github.com/thedodd/trunk">Trunk Maintainers</a> © 2023
<a href="https://github.com/trunk-dev/trunk">Trunk Maintainers</a> © 2023
</small>
</p>
<p class="subtext text-center">
Expand Down
70 changes: 52 additions & 18 deletions src/pipelines/rust/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,8 @@ pub struct RustApp {
/// An optional optimization setting that enables wasm-opt. Can be nothing, `0` (default), `1`,
/// `2`, `3`, `4`, `s or `z`. Using `0` disables wasm-opt completely.
wasm_opt: WasmOptLevel,
/// The value of the `--target` flag for wasm-bindgen.
wasm_bindgen_target: WasmBindgenTarget,
/// Name for the module. Is binary name if given, otherwise it is the name of the cargo
/// project.
name: String,
Expand Down Expand Up @@ -97,6 +99,30 @@ impl FromStr for RustAppType {
}
}

/// Determines the value of `--target` flag for wasm-bindgen. For more details see
/// [here](https://rustwasm.github.io/wasm-bindgen/reference/deployment.html).
#[derive(Debug, Clone)]
pub struct WasmBindgenTarget(String);

impl FromStr for WasmBindgenTarget {
type Err = anyhow::Error;

fn from_str(s: &str) -> Result<Self, Self::Err> {
ensure!(
s == "bundler" || s == "web" || s == "no-modules" || s == "nodejs" || s == "deno",
r#"unknown `data-bindgen-target="{}"` value for <link data-trunk rel="rust" .../> attr; please ensure the value is lowercase and is a supported type"#,
s
);
Ok(Self(String::from(s)))
}
}

impl Default for WasmBindgenTarget {
fn default() -> Self {
Self(String::from("web"))
}
}

impl RustApp {
pub const TYPE_RUST_APP: &'static str = "rust";

Expand Down Expand Up @@ -128,9 +154,9 @@ impl RustApp {
let no_demangle = attrs.contains_key("data-no-demangle");
let app_type = attrs
.get("data-type")
.map(|s| s.as_str())
.unwrap_or("main")
.parse()?;
.map(|s| s.parse())
.transpose()?
.unwrap_or(RustAppType::Main);
let reference_types = attrs.contains_key("data-reference-types");
let weak_refs = attrs.contains_key("data-weak-refs");
let wasm_opt = attrs
Expand All @@ -144,6 +170,11 @@ impl RustApp {
WasmOptLevel::Off
}
});
let wasm_bindgen_target = attrs
.get("data-bindgen-target")
.map(|s| s.parse())
.transpose()?
.unwrap_or_default();
let cross_origin = attrs
.get("data-cross-origin")
.map(|val| CrossOrigin::from_str(val))
Expand All @@ -160,10 +191,10 @@ impl RustApp {
let name = bin.clone().unwrap_or_else(|| manifest.package.name.clone());

let data_features = attrs.get("data-cargo-features").map(|val| val.to_string());
let data_all_features = attrs.get("data-cargo-all-features").is_some();
let data_no_default_features = attrs.get("data-cargo-no-default-features").is_some();
let data_all_features = attrs.contains_key("data-cargo-all-features");
let data_no_default_features = attrs.contains_key("data-cargo-no-default-features");

let loader_shim = attrs.get("data-loader-shim").is_some();
let loader_shim = attrs.contains_key("data-loader-shim");
if loader_shim {
ensure!(
app_type == RustAppType::Worker,
Expand Down Expand Up @@ -210,6 +241,7 @@ impl RustApp {
reference_types,
weak_refs,
wasm_opt,
wasm_bindgen_target,
app_type,
name,
loader_shim,
Expand Down Expand Up @@ -253,6 +285,7 @@ impl RustApp {
weak_refs: false,
wasm_opt: WasmOptLevel::Off,
app_type: RustAppType::Main,
wasm_bindgen_target: WasmBindgenTarget::default(),
name,
loader_shim: false,
cross_origin: Default::default(),
Expand Down Expand Up @@ -466,12 +499,9 @@ impl RustApp {
let arg_out_path = format!("--out-dir={}", bindgen_out);
let arg_out_name = format!("--out-name={}", &hashed_name);
let target_wasm = wasm.to_string_lossy().to_string();
let target_type = match self.app_type {
RustAppType::Main => "--target=web",
RustAppType::Worker => "--target=no-modules",
};
let target_type = format!("--target={}", self.wasm_bindgen_target.0);

let mut args = vec![target_type, &arg_out_path, &arg_out_name, &target_wasm];
let mut args: Vec<&str> = vec![&target_type, &arg_out_path, &arg_out_name, &target_wasm];
if self.keep_debug {
args.push("--keep-debug");
}
Expand Down Expand Up @@ -541,14 +571,18 @@ impl RustApp {
.await
.context("error creating loader shim script")?;

let shim = match self.wasm_bindgen_target.0.as_ref() {
"web" => format!("import init from './{hashed_js_name}';await init();"),
"no-modules" => format!(
r#"importScripts("./{hashed_js_name}");wasm_bindgen("./{hashed_wasm_name}");"#,
),
_ => bail!(
"Loader shim can only be created for data-bindgen-target \"web\" or \
\"no-modules\"!"
),
};
loader_f
.write_all(
format!(
r#"importScripts("./{}");wasm_bindgen("./{}");"#,
hashed_js_name, hashed_wasm_name
)
.as_bytes(),
)
.write_all(shim.as_bytes())
.await
.context("error writing loader shim script")?;
loader_f
Expand Down