Skip to content

Commit

Permalink
Merge pull request #4 from radish19/feature/setup
Browse files Browse the repository at this point in the history
Integrate Wasm module into frontend with Rust CI/CD
  • Loading branch information
derarion authored Dec 5, 2023
2 parents b71f6b7 + e7576e7 commit 480a8be
Show file tree
Hide file tree
Showing 9 changed files with 132 additions and 61 deletions.
4 changes: 2 additions & 2 deletions .eslintrc.cjs
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,9 @@ module.exports = {
"eslint:recommended",
"plugin:@typescript-eslint/recommended",
"plugin:react-hooks/recommended",
"prettier"
"prettier",
],
ignorePatterns: ["dist", ".eslintrc.cjs"],
ignorePatterns: ["dist", ".eslintrc.cjs", "momonga/"],
parser: "@typescript-eslint/parser",
plugins: ["react-refresh"],
rules: {
Expand Down
18 changes: 0 additions & 18 deletions .github/workflows/build.yaml

This file was deleted.

58 changes: 58 additions & 0 deletions .github/workflows/cicd.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
name: cicd

on: push

permissions:
contents: read
pages: write
id-token: write

concurrency:
group: "pages"
cancel-in-progress: true

jobs:
build:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v4
- uses: actions/setup-node@v4
with:
node-version: 18
- uses: actions-rs/toolchain@v1
with:
toolchain: 1.72
target: wasm32-unknown-unknown
override: true
components: clippy
- uses: actions-rs/[email protected]
with:
crate: wasm-pack
version: 0.12.1
use-tool-cache: true
- run: npm install
- run: npm run lint
- run: npm run test
- run: npm run build
- uses: actions/upload-artifact@v2
with:
name: build-artifact
path: ./dist

deploy:
needs: build
if: github.ref == 'refs/heads/main'
environment:
name: github-pages
url: ${{ steps.deployment.outputs.page_url }}
runs-on: ubuntu-latest
steps:
- uses: actions/download-artifact@v2
with:
name: build-artifact
path: ./dist
- uses: actions/upload-pages-artifact@v2
with:
path: ./dist
- uses: actions/deploy-pages@v2
id: deployment
34 changes: 0 additions & 34 deletions .github/workflows/deploy.yaml

This file was deleted.

14 changes: 14 additions & 0 deletions momonga/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
# Generated by Cargo
# will have compiled files and executables
debug/
target/

# Remove Cargo.lock from gitignore if creating an executable, leave it for libraries
# More information here https://doc.rust-lang.org/cargo/guide/cargo-toml-vs-cargo-lock.html
Cargo.lock

# These are backup files generated by rustfmt
**/*.rs.bk

# MSVC Windows builds of rustc generate these, which store debugging information
*.pdb
10 changes: 10 additions & 0 deletions momonga/Cargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
[package]
name = "momonga"
version = "0.1.0"
edition = "2021"

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

[dependencies]
wasm-bindgen = "0.2"
26 changes: 26 additions & 0 deletions momonga/src/lib.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
use wasm_bindgen::prelude::*;

pub fn add(left: usize, right: usize) -> usize {
left + right
}

#[wasm_bindgen]
extern {
pub fn alert(s: &str);
}

#[wasm_bindgen]
pub fn greet(name: &str) {
alert(&format!("Hello, {}!", name));
}

#[cfg(test)]
mod tests {
use super::*;

#[test]
fn it_works() {
let result = add(2, 2);
assert_eq!(result, 4);
}
}
12 changes: 9 additions & 3 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -8,16 +8,22 @@
},
"scripts": {
"dev": "vite",
"build": "tsc && vite build",
"lint": "eslint . --ext ts,tsx --report-unused-disable-directives --max-warnings 0",
"build": "npm run build:core && npm run build:web",
"build:core": "cd ./momonga && wasm-pack build --release --target web",
"build:web": "tsc && vite build",
"test": "npm run test:core",
"test:core": "cd momonga && cargo test",
"lint": "npm run lint:web && npm run lint:core",
"lint:core": "cd momonga && cargo clippy -- -D warnings",
"lint:web": "eslint . --ext ts,tsx --report-unused-disable-directives --max-warnings 0",
"preview": "vite preview",
"prepare": "simple-git-hooks"
},
"simple-git-hooks": {
"pre-commit": "npx lint-staged"
},
"lint-staged": {
"*": [
"*.{js,jsx,ts,tsx,css,md,json}": [
"npx prettier --write"
]
},
Expand Down
17 changes: 13 additions & 4 deletions src/App.tsx
Original file line number Diff line number Diff line change
@@ -1,10 +1,15 @@
import { useState } from "react";
import reactLogo from "./assets/react.svg";
import viteLogo from "/vite.svg";
import "./App.css";

import { useEffect } from "react";

import init, { greet } from "../momonga/pkg/momonga";

function App() {
const [count, setCount] = useState(0);
useEffect(() => {
init();
}, []);

return (
<>
Expand All @@ -18,8 +23,12 @@ function App() {
</div>
<h1>Vite + React</h1>
<div className="card">
<button onClick={() => setCount((count) => count + 1)}>
count is {count}
<button
onClick={() => {
greet("WebAssembly");
}}
>
greet
</button>
<p>
Edit <code>src/App.tsx</code> and save to test HMR
Expand Down

0 comments on commit 480a8be

Please sign in to comment.