Skip to content

Commit

Permalink
extract starter project
Browse files Browse the repository at this point in the history
  • Loading branch information
tylerbarker committed Dec 23, 2024
0 parents commit 2c1405c
Show file tree
Hide file tree
Showing 42 changed files with 969 additions and 0 deletions.
29 changes: 29 additions & 0 deletions .dockerignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
# flyctl launch added from .gitignore
# Local
**/.DS_Store
**/*.local
**/*.log*

# Dist
**/node_modules
**/dist
**/.vinxi
**/.output
**/.vercel
**/.netlify
**/.wrangler

# IDE
**/.zed
**/.vscode/*
!**/.vscode/extensions.json
**/.idea

# flyctl launch added from acrate/.gitignore
acrate/target
acrate/**/**/*.rs.bk
acrate/**/Cargo.lock
acrate/**/bin
acrate/**/pkg
acrate/**/wasm-pack.log
fly.toml
50 changes: 50 additions & 0 deletions .github/workflows/main.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
name: bun-workflow

on:
push:
branches: [main]
pull_request:
branches: [main]

permissions:
contents: read

jobs:
bun_build:
name: Build + test with Bun v${{matrix.bun_version}}, wasm-bindgen v${{matrix.wasm_bindgen_version}}
strategy:
matrix:
bun_version: ["1.1.40"]
wasm_bindgen_version: ["0.2.99"]
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v4
- uses: oven-sh/setup-bun@v2
with:
bun-version: ${{matrix.bun_version}}

- name: Setup Rust
uses: dtolnay/rust-toolchain@stable
with:
targets: wasm32-unknown-unknown

- name: Install wasm-bindgen-cli + wasm-pack
run: |
cargo install wasm-bindgen-cli --version ${{matrix.wasm_bindgen_version}}
curl https://rustwasm.github.io/wasm-pack/installer/init.sh -sSf | sh
- name: Cargo + wasm-pack build
working-directory: ./acrate
run: |
cargo build --target wasm32-unknown-unknown
wasm-pack build --target web
- name: Cargo + wasm-pack tests
working-directory: ./acrate
run: |
cargo test
wasm-pack test --headless --chrome
- run: bun install
- run: bun run build
- run: bun test
19 changes: 19 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
# Local
.DS_Store
*.local
*.log*

# Dist
node_modules
dist/
.vinxi
.output
.vercel
.netlify
.wrangler

# IDE
.zed
.vscode/*
!.vscode/extensions.json
.idea
1 change: 1 addition & 0 deletions .tool-versions
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
bun 1.1.40
42 changes: 42 additions & 0 deletions Dockerfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
ARG BUN_VERSION=1.1.40
FROM oven/bun:${BUN_VERSION} AS builder

# Install OS deps
RUN apt-get update -y && apt-get install -y ca-certificates curl build-essential

# Install Rust
RUN curl --proto '=https' --tlsv1.2 -sSf https://sh.rustup.rs | sh -s -- --default-toolchain stable --profile minimal --target x86_64-unknown-linux-gnu -y
ENV PATH="/root/.cargo/bin:${PATH}"

# Install wasm-pack
RUN cargo install wasm-pack

WORKDIR /app

# Copy necessary files only
COPY acrate ./acrate
COPY app ./app
COPY public ./public
COPY package.json bun.lockb ./
COPY app.config.ts tsconfig.json ./
COPY tailwind.config.cjs postcss.config.cjs ./
COPY wasm-build.ts ./

# Install deps and build the application
RUN bun install
RUN bun run build

# Production stage
FROM oven/bun:${BUN_VERSION}-slim

WORKDIR /app

# Copy only the necessary built files from builder
COPY --from=builder /app/.output/server ./server
COPY --from=builder /app/.output/public ./public

# Expose the port your app runs on (adjust if needed)
EXPOSE 3000

# Command to run the application
CMD ["bun", "run", "server/index.mjs"]
22 changes: 22 additions & 0 deletions LICENSE.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
# MIT License

Copyright (c) 2024 Tyler Barker

Permission is hereby granted, free of charge, to any person obtaining
a copy of this software and associated documentation files (the
"Software"), to deal in the Software without restriction, including
without limitation the rights to use, copy, modify, merge, publish,
distribute, sublicense, and/or sell copies of the Software, and to
permit persons to whom the Software is furnished to do so, subject to
the following conditions:

The above copyright notice and this permission notice shall be
included in all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
46 changes: 46 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
# Tanstack + Bun + Rust WASM Starter 🐰

An extremely shiny React app starter featuring:

- TanStack [Start](https://tanstack.com/start) + [Router](https://tanstack.com/router) + [Query](https://tanstack.com/query)
- Package management, TS scripting with [Bun](https://bun.sh)
- Deployed to a [Bun](https://bun.sh) server
- Integrated WASM library with [Rust](https://www.rust-lang.org) and [wasm-bindgen](https://wasm-bindgen.netlify.app)
- Styling with [TailwindCSS](https://tailwindcss.com)
- Formatting + linting with [Biome](https://biomejs.dev)
- Project dependencies managed by [asdf](https://asdf-vm.com)
- Dockerized deployment (I recommend [fly.io](https://fly.io))

## Requirements

To use this template as intended, you'll need all the following installed:

- [asdf](https://asdf-vm.com) (+ run `asdf plugin add bun`)
- [Bun](https://bun.sh) (via asdf, run `asdf install`)
- Biome.js [IDE integration](https://biomejs.dev/guides/editors/first-party-extensions)
- Fly.io account & [flyctl](https://fly.io/docs/flyctl/install) (or your preferred hosting provider)
- chromedriver (`brew install --cask chromedriver --no-quarantine`)
- Rust (via [rustup](https://rustup.rs))
- [wasm-bindgen-cli](https://wasm-bindgen.netlify.app/reference/cli.html?highlight=cli#installation) + [wasm-pack](https://rustwasm.github.io/wasm-pack/)

All of the above assumes macOS. Where I've not included links to installation, please refer to a search engine for the relevant instructions for your machine.

## New Template Steps

1. Rename root directory, replace all uses of "tanstack-bun-wasm-starter" name.
2. Rename WASM crate directory - replace all uses of "acrate" name.
3. Replace "yourdomain.com" with your actual domain in robots.txt and sitemap.xml (BYO DNS config, see [fly certs](https://fly.io/docs/flyctl/certs).
4. Run `bun run setup`
5. If all's well run `bun run dev`
6. Happy coding ✌️

## Deployment

I deploy this to Fly.io, so these instructions will be specific. I've intentionally not included the autogenerated `fly.toml` file in this repository so you can generate it yourself:

1. Create an account with Fly.io
2. [Install flyctl](https://fly.io/docs/flyctl/install) and authenticate.
3. Run `flyctl launch` from the root directory and follow the prompts.
4. Deploy with `flyctl deploy`.

Note that `flyctl launch` will also generate a GitHub Action for deployment. Be sure to note down the `FLY_API_TOKEN` and save it to your repo action secrets if you'd like to automate deployments this way.
8 changes: 8 additions & 0 deletions acrate/.cargo/config.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
[build]
target = "wasm32-unknown-unknown"

[test]
target = "wasm32-unknown-unknown"

[target.wasm32-unknown-unknown]
runner = 'wasm-bindgen-test-runner'
6 changes: 6 additions & 0 deletions acrate/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
/target
**/*.rs.bk
Cargo.lock
bin/
pkg/
wasm-pack.log
31 changes: 31 additions & 0 deletions acrate/Cargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
[package]
name = "acrate"
description = "A crate for generating WebAssembly functions for the browser."
version = "0.0.0"
authors = ["Tyler Barker <[email protected]>"]
edition = "2021"
license = "MIT"

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

[features]
default = ["console_error_panic_hook"]

[dependencies]
wasm-bindgen = "0.2.99"

# The `console_error_panic_hook` crate provides better debugging of panics by
# logging them with `console.error`. This is great for development, but requires
# all the `std::fmt` and `std::panicking` infrastructure, so isn't great for
# code size when deploying.
console_error_panic_hook = { version = "0.1.7", optional = true }
web-sys = { version = "0.3.70", features = ["AudioBuffer", "AudioContext"] }
js-sys = "0.3.70"

[dev-dependencies]
wasm-bindgen-test = "0.3.49"

[profile.release]
# Tell `rustc` to optimize for small code size.
opt-level = "s"
84 changes: 84 additions & 0 deletions acrate/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,84 @@
<div align="center">

<h1><code>wasm-pack-template</code></h1>

<strong>A template for kick starting a Rust and WebAssembly project using <a href="https://github.com/rustwasm/wasm-pack">wasm-pack</a>.</strong>

<p>
<a href="https://travis-ci.org/rustwasm/wasm-pack-template"><img src="https://img.shields.io/travis/rustwasm/wasm-pack-template.svg?style=flat-square" alt="Build Status" /></a>
</p>

<h3>
<a href="https://rustwasm.github.io/docs/wasm-pack/tutorials/npm-browser-packages/index.html">Tutorial</a>
<span> | </span>
<a href="https://discordapp.com/channels/442252698964721669/443151097398296587">Chat</a>
</h3>

<sub>Built with 🦀🕸 by <a href="https://rustwasm.github.io/">The Rust and WebAssembly Working Group</a></sub>
</div>

## About

[**📚 Read this template tutorial! 📚**][template-docs]

This template is designed for compiling Rust libraries into WebAssembly and
publishing the resulting package to NPM.

Be sure to check out [other `wasm-pack` tutorials online][tutorials] for other
templates and usages of `wasm-pack`.

[tutorials]: https://rustwasm.github.io/docs/wasm-pack/tutorials/index.html
[template-docs]: https://rustwasm.github.io/docs/wasm-pack/tutorials/npm-browser-packages/index.html

## 🚴 Usage

### 🐑 Use `cargo generate` to Clone this Template

[Learn more about `cargo generate` here.](https://github.com/ashleygwilliams/cargo-generate)

```
cargo generate --git https://github.com/rustwasm/wasm-pack-template.git --name my-project
cd my-project
```

### 🛠️ Build with `wasm-pack build`

```
wasm-pack build
```

### 🔬 Test in Headless Browsers with `wasm-pack test`

```
wasm-pack test --headless --firefox
```

### 🎁 Publish to NPM with `wasm-pack publish`

```
wasm-pack publish
```

## 🔋 Batteries Included

* [`wasm-bindgen`](https://github.com/rustwasm/wasm-bindgen) for communicating
between WebAssembly and JavaScript.
* [`console_error_panic_hook`](https://github.com/rustwasm/console_error_panic_hook)
for logging panic messages to the developer console.
* `LICENSE-APACHE` and `LICENSE-MIT`: most Rust projects are licensed this way, so these are included for you

## License

Licensed under either of

* Apache License, Version 2.0, ([LICENSE-APACHE](LICENSE-APACHE) or http://www.apache.org/licenses/LICENSE-2.0)
* MIT license ([LICENSE-MIT](LICENSE-MIT) or http://opensource.org/licenses/MIT)

at your option.

### Contribution

Unless you explicitly state otherwise, any contribution intentionally
submitted for inclusion in the work by you, as defined in the Apache-2.0
license, shall be dual licensed as above, without any additional terms or
conditions.
33 changes: 33 additions & 0 deletions acrate/src/lib.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
mod utils;

use wasm_bindgen::prelude::*;

#[wasm_bindgen]
extern "C" {
// Use `js_namespace` here to bind `console.log(..)` instead of just
// `log(..)`
#[wasm_bindgen(js_namespace = console)]
fn log(s: &str);

// The `console.log` is quite polymorphic, so we can bind it with multiple
// signatures. Note that we need to use `js_name` to ensure we always call
// `log` in JS.
#[wasm_bindgen(js_namespace = console, js_name = log)]
fn log_u32(a: u32);

// Multiple arguments too!
#[wasm_bindgen(js_namespace = console, js_name = log)]
fn log_many(a: &str, b: &str);
}

#[allow(unused_macros)]
macro_rules! console_log {
// Note that this is using the `log` function imported above during
// `bare_bones`
($($t:tt)*) => (log(&format_args!($($t)*).to_string()))
}

#[wasm_bindgen]
pub fn sum(a: f32, b: f32) -> f32 {
a + b
}
11 changes: 11 additions & 0 deletions acrate/src/utils.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
#[allow(dead_code)]
pub fn set_panic_hook() {
// When the `console_error_panic_hook` feature is enabled, we can call the
// `set_panic_hook` function at least once during initialization, and then
// we will get better error messages if our code ever panics.
//
// For more details see
// https://github.com/rustwasm/console_error_panic_hook#readme
#[cfg(feature = "console_error_panic_hook")]
console_error_panic_hook::set_once();
}
Loading

0 comments on commit 2c1405c

Please sign in to comment.