Skip to content

Commit

Permalink
feat: add support for trunk
Browse files Browse the repository at this point in the history
[Trunk](https://trunkrs.dev) is a tool that allow you to build web apps
using Rust and webassembly, including compiling scss, and distributing
other assets.

Adds `craneLib.buildTrunkPackage`, a function that builds a Trunk
project returning a directory with the resulting dist files for that
project
  • Loading branch information
lelgenio committed Apr 28, 2023
1 parent 54b63c8 commit 287f5fd
Show file tree
Hide file tree
Showing 32 changed files with 3,172 additions and 0 deletions.
3 changes: 3 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,9 @@ and this project adheres to [Semantic Versioning](http://semver.org/spec/v2.0.0.

## Unreleased

### Added
* Added support for the [Trunk](https://trunkrs.dev) wasm app build tool

### [0.12.1] - 2023-04-10

### Changed
Expand Down
2 changes: 2 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ A [Nix] library for building [cargo] projects.
* [cargo-audit] for vulnerability scanning
* [cargo-nextest] a next-generation test runner
* [cargo-tarpaulin] for code coverage
* [trunk] for wasm frontend apps

## Getting Started

Expand Down Expand Up @@ -72,3 +73,4 @@ conditions.
[Nix]: https://nixos.org/
[rustfmt]: https://github.com/rust-lang/rustfmt
[Semantic Versioning]: http://semver.org/spec/v2.0.0.html
[Trunk]: https://trunkrs.dev/
4 changes: 4 additions & 0 deletions checks/default.nix
Original file line number Diff line number Diff line change
Expand Up @@ -414,6 +414,10 @@ in
smokeWorkspace = self.smoke [ "print" ] self.workspace;
smokeWorkspaceRoot = self.smoke [ "print" ] self.workspaceRoot;

trunk = callPackage ./trunk.nix {
inherit myLib;
};

vendorCargoDeps =
let
src = ./workspace;
Expand Down
35 changes: 35 additions & 0 deletions checks/trunk.nix
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
{ pkgs
, myLib
, runCommand
}:

let
wasmToolchain = pkgs.rust-bin.stable.latest.minimal.override {
targets = [ "wasm32-unknown-unknown" ];
};

myLibWasm = myLib.overrideToolchain wasmToolchain;

defaultArgs = {
src = ./trunk;
doCheck = false;
};

# default build
cargoArtifacts = myLibWasm.buildDepsOnly (defaultArgs // {
cargoExtraArgs = "--target=wasm32-unknown-unknown";
});
trunkSimple = myLibWasm.buildTrunkPackage (defaultArgs // {
inherit cargoArtifacts;
pname = "trunk-simple";
});

trunkSimpleNoArtifacts = myLibWasm.buildTrunkPackage (defaultArgs // {
pname = "trunk-simple-no-artifacts";
});
in
runCommand "trunkTests" { } ''
test -f ${trunkSimple}/*.wasm
test -f ${trunkSimpleNoArtifacts}/*.wasm
mkdir -p $out
''
156 changes: 156 additions & 0 deletions checks/trunk/Cargo.lock

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

8 changes: 8 additions & 0 deletions checks/trunk/Cargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
[package]
name = "trunk"
version = "0.1.0"
edition = "2021"

[dependencies]
console_log = "1.0.0"
log = "0.4.17"
7 changes: 7 additions & 0 deletions checks/trunk/index.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8" />
<title>Trunk App</title>
</head>
</html>
4 changes: 4 additions & 0 deletions checks/trunk/src/main.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
fn main() {
console_log::init().unwrap();
log::debug!("Hi, this can run on the browser");
}
19 changes: 19 additions & 0 deletions docs/API.md
Original file line number Diff line number Diff line change
Expand Up @@ -226,6 +226,25 @@ The following hooks are automatically added as native build inputs:
* `jq`
* `removeReferencesToVendoredSourcesHook`

### `craneLib.buildTrunkPackage`
`buildTrunkPackage :: set -> drv`

Create a derivation which will build a distributable directory for a WASM application.

Except where noted below, all derivation attributes are delegated to
`mkCargoDerivation`, and can be used to influence its behavior.
* `cargoArtifactsWasm`: A path (or derivation) which contains an existing cargo
`target` directory, which will be reused at the start of the derivation.
Useful for caching incremental cargo builds.
- This can be prepared via `buildDepsOnly`
- Alternatively, any cargo-based derivation which was built with
`doInstallCargoArtifacts = true` will work as well
- For this to be usefull it must have the dependencies
built for the `wasm32-unknown-unknown` target
* `indexPath` A path to the index.html of your trunk project
* `trunkExtraArgs` pass additional arguments to `trunk`, empty by default
* `trunkExtraBuildArgs` pass additional arguments to `trunk build`, empty by default

### `craneLib.cargoAudit`
`cargoAudit :: set -> drv`

Expand Down
2 changes: 2 additions & 0 deletions docs/SUMMARY.md
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,8 @@
* [Cross compiling](./examples/cross-rust-overlay.md)
* [Cross compiling with musl](./examples/cross-musl.md)
* [Cross compiling to windows](./examples/cross-windows.md)
* [Building Trunk projects](./examples/trunk.md)
* [Workspace with Trunk](./examples/trunk-workspace.md)
* [Source filtering](./source-filtering.md)
* [Local development](./local_development.md)
* [Custom cargo commands](./custom_cargo_commands.md)
Expand Down
20 changes: 20 additions & 0 deletions docs/examples/trunk-workspace.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
[Trunk](https://trunkrs.dev) is a tool that allow you to build web apps using Rust and webassembly, including compiling scss, and distributing other assets.
It can be used in conjunction with any of Rust's web frameworks for the development of full stack web applications.

In this example we have a workspace with three members:

* client: a Yew application compiled using Trunk
* server: a Axum server built using Cargo
* shared: a library that contains types to be imported in both the client and server

Quick-start a Trunk+Server project with

```sh
nix flake init -t github:ipetkov/crane#trunk-workspace
```

Alternatively, copy and paste the following `flake.nix` and modify it to build your workspace's packages:

```nix
{{#include ../../examples/trunk-workspace/flake.nix}}
```
19 changes: 19 additions & 0 deletions docs/examples/trunk.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
[Trunk](https://trunkrs.dev) is a tool that allow you to build web apps using Rust and webassembly, including compiling scss, and distributing other assets.

Being a more specialized tool, it comes with some constraints that must be noted when using it in combination with crane:

* Your Toolchain must have the `wasm32-unknown-unknown` target installed (See: [Custom toolchain](../custom-toolchain.md))
* For `craneLib.buildDepsOnly` to work you will need to set the build target (See: [API Reference](../API.md#libbuilddepsonly))
* `craneLib.filterCargoSources` will remove html, css, your assets folder, so you need to modify the source filtering function (See: [Source filtering](../source-filtering.md))

Quick-start a Trunk project with

```sh
nix flake init -t github:ipetkov/crane#trunk
```

Alternatively, copy and paste the following `flake.nix`:

```nix
{{#include ../../examples/trunk/flake.nix}}
```
6 changes: 6 additions & 0 deletions docs/getting-started.md
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,12 @@ nix flake init -t github:ipetkov/crane#cross-rust-overlay

# For statically linked binaries using musl
nix flake init -t github:ipetkov/crane#cross-musl

# If you are building a WASM webapp with trunk
nix flake init -t github:ipetkov/crane#trunk

# If you are building a workspace with trunk member
nix flake init -t github:ipetkov/crane#trunk-workspace
```

For an even more lean, no frills set up, create a `flake.nix` file with the
Expand Down
1 change: 1 addition & 0 deletions examples/trunk-workspace/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
client/dist
Loading

0 comments on commit 287f5fd

Please sign in to comment.