Skip to content

Commit

Permalink
Checkpoint: use vite with vite-plugin-wasm to load rust wasm
Browse files Browse the repository at this point in the history
  • Loading branch information
neilsarkar committed Oct 30, 2022
1 parent 7542f78 commit 655ed62
Show file tree
Hide file tree
Showing 8 changed files with 473 additions and 6 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
node_modules/
dist/
.parcel-cache/
target/
126 changes: 126 additions & 0 deletions Cargo.lock

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

13 changes: 13 additions & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
[package]
name = "hellowasm"
version = "0.1.0"
edition = "2021"
authors = ["Neil Sarkar <[email protected]>"]

# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html

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

[dependencies]
wasm-bindgen = "0.2"
15 changes: 11 additions & 4 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -6,16 +6,23 @@
"license": "MIT",
"type": "module",
"scripts": {
"dev": "parcel index.html",
"demo": "parcel input.html",
"test": "jest"
"dev": "vite",
"build": "tsc && vite build",
"preview": "vite preview",
"parceldev": "parcel index.html",
"parceldemo": "parcel input.html",
"test": "jest",
"wasmpack": "wasm-pack build"
},
"devDependencies": {
"@types/jest": "^29.1.2",
"jest": "^29.2.0",
"parcel": "^2.7.0",
"ts-jest": "^29.0.3",
"typescript": "^4.8.4"
"typescript": "^4.8.4",
"vite": "^3.2.1",
"vite-plugin-top-level-await": "^1.2.1",
"vite-plugin-wasm": "^3.1.0"
},
"dependencies": {
"pixi.js": "^6.5.6"
Expand Down
11 changes: 11 additions & 0 deletions src/lib.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
use wasm_bindgen::prelude::*;

#[wasm_bindgen]
pub fn add(a: i32, b: i32) -> i32 {
return a + b;
}

#[wasm_bindgen]
pub fn subtract(a: i32, b: i32) -> i32 {
return a - b;
}
3 changes: 3 additions & 0 deletions src/pong.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,9 @@ import { MarshalInput } from './MarshalInput';
import { GamepadSnapshot } from './GamepadSnapshot';
import {clamp} from './lib';
import { Pog } from './types';
import {add, subtract } from '../pkg/hellowasm_bg.wasm'

console.log('nice', add(1,1), subtract(400, 100));

type GameState = {
leftPaddle: BoundingBox,
Expand Down
10 changes: 10 additions & 0 deletions vite.config.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
import { defineConfig } from "vite";
import wasm from 'vite-plugin-wasm';
import topLevelAwait from "vite-plugin-top-level-await"

export default defineConfig({
plugins: [
wasm(),
topLevelAwait()
]
})
Loading

0 comments on commit 655ed62

Please sign in to comment.