From 8ea042b5a0801823369eaf402540269c0315eaec Mon Sep 17 00:00:00 2001 From: "radish@MBA2020" Date: Tue, 5 Dec 2023 16:22:06 +0900 Subject: [PATCH 1/6] Create Rust library crate --- momonga/.gitignore | 14 ++++++++++++++ momonga/Cargo.toml | 8 ++++++++ momonga/src/lib.rs | 14 ++++++++++++++ package.json | 2 +- 4 files changed, 37 insertions(+), 1 deletion(-) create mode 100644 momonga/.gitignore create mode 100644 momonga/Cargo.toml create mode 100644 momonga/src/lib.rs diff --git a/momonga/.gitignore b/momonga/.gitignore new file mode 100644 index 0000000..6985cf1 --- /dev/null +++ b/momonga/.gitignore @@ -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 diff --git a/momonga/Cargo.toml b/momonga/Cargo.toml new file mode 100644 index 0000000..944164a --- /dev/null +++ b/momonga/Cargo.toml @@ -0,0 +1,8 @@ +[package] +name = "momonga" +version = "0.1.0" +edition = "2021" + +# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html + +[dependencies] diff --git a/momonga/src/lib.rs b/momonga/src/lib.rs new file mode 100644 index 0000000..7d12d9a --- /dev/null +++ b/momonga/src/lib.rs @@ -0,0 +1,14 @@ +pub fn add(left: usize, right: usize) -> usize { + left + right +} + +#[cfg(test)] +mod tests { + use super::*; + + #[test] + fn it_works() { + let result = add(2, 2); + assert_eq!(result, 4); + } +} diff --git a/package.json b/package.json index 1c183f8..62d1740 100644 --- a/package.json +++ b/package.json @@ -17,7 +17,7 @@ "pre-commit": "npx lint-staged" }, "lint-staged": { - "*": [ + "*.{js,jsx,ts,tsx,css,md,json}": [ "npx prettier --write" ] }, From e47c256c5120ee4fa555bebc81f73136a1c54046 Mon Sep 17 00:00:00 2001 From: "radish@MBA2020" Date: Tue, 5 Dec 2023 16:52:45 +0900 Subject: [PATCH 2/6] Add Rust test process to CI --- .github/workflows/build.yaml | 1 + package.json | 2 ++ 2 files changed, 3 insertions(+) diff --git a/.github/workflows/build.yaml b/.github/workflows/build.yaml index 7ce4737..f15a4ea 100644 --- a/.github/workflows/build.yaml +++ b/.github/workflows/build.yaml @@ -15,4 +15,5 @@ jobs: node-version: 18 - run: npm install - run: npm run lint + - run: npm run test - run: npm run build diff --git a/package.json b/package.json index 62d1740..8fbe2c3 100644 --- a/package.json +++ b/package.json @@ -9,6 +9,8 @@ "scripts": { "dev": "vite", "build": "tsc && vite build", + "test": "npm run test:core", + "test:core": "cd momonga && cargo test", "lint": "eslint . --ext ts,tsx --report-unused-disable-directives --max-warnings 0", "preview": "vite preview", "prepare": "simple-git-hooks" From 78e7ac9ae8679a332b9b9372443e63559a906fa8 Mon Sep 17 00:00:00 2001 From: "radish@MBA2020" Date: Tue, 5 Dec 2023 18:48:18 +0900 Subject: [PATCH 3/6] Add Rust linting --- package.json | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/package.json b/package.json index 8fbe2c3..12ec1ca 100644 --- a/package.json +++ b/package.json @@ -11,7 +11,9 @@ "build": "tsc && vite build", "test": "npm run test:core", "test:core": "cd momonga && cargo test", - "lint": "eslint . --ext ts,tsx --report-unused-disable-directives --max-warnings 0", + "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" }, From 5b28bce788e0acdf97db57fbce6f131b338ec761 Mon Sep 17 00:00:00 2001 From: "radish@MBA2020" Date: Tue, 5 Dec 2023 21:26:07 +0900 Subject: [PATCH 4/6] Integrate Wasm module into frontend --- .eslintrc.cjs | 4 ++-- momonga/Cargo.toml | 4 +++- momonga/src/lib.rs | 12 ++++++++++++ package.json | 4 +++- src/App.tsx | 17 +++++++++++++---- 5 files changed, 33 insertions(+), 8 deletions(-) diff --git a/.eslintrc.cjs b/.eslintrc.cjs index eadf275..5e9ac83 100644 --- a/.eslintrc.cjs +++ b/.eslintrc.cjs @@ -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: { diff --git a/momonga/Cargo.toml b/momonga/Cargo.toml index 944164a..60c6fb1 100644 --- a/momonga/Cargo.toml +++ b/momonga/Cargo.toml @@ -3,6 +3,8 @@ name = "momonga" version = "0.1.0" edition = "2021" -# 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" diff --git a/momonga/src/lib.rs b/momonga/src/lib.rs index 7d12d9a..3140d0c 100644 --- a/momonga/src/lib.rs +++ b/momonga/src/lib.rs @@ -1,7 +1,19 @@ +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::*; diff --git a/package.json b/package.json index 12ec1ca..f4116bc 100644 --- a/package.json +++ b/package.json @@ -8,7 +8,9 @@ }, "scripts": { "dev": "vite", - "build": "tsc && vite build", + "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", diff --git a/src/App.tsx b/src/App.tsx index 52ea980..1229e3f 100644 --- a/src/App.tsx +++ b/src/App.tsx @@ -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 ( <> @@ -18,8 +23,12 @@ function App() {

Vite + React

-

Edit src/App.tsx and save to test HMR From ec313a5a06415fa5fa67793c0621e537315abb8d Mon Sep 17 00:00:00 2001 From: "radish@MBA2020" Date: Tue, 5 Dec 2023 21:30:07 +0900 Subject: [PATCH 5/6] Setup Rust tools for GitHub Actions --- .github/workflows/build.yaml | 11 +++++++++++ 1 file changed, 11 insertions(+) diff --git a/.github/workflows/build.yaml b/.github/workflows/build.yaml index f15a4ea..cb64c85 100644 --- a/.github/workflows/build.yaml +++ b/.github/workflows/build.yaml @@ -13,6 +13,17 @@ jobs: - 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/install@v0.1 + with: + crate: wasm-pack + version: 0.12.1 + use-tool-cache: true - run: npm install - run: npm run lint - run: npm run test From e7576e72ab88d9ec758675a24bdfc1b916e00660 Mon Sep 17 00:00:00 2001 From: "radish@MBA2020" Date: Tue, 5 Dec 2023 21:43:17 +0900 Subject: [PATCH 6/6] Integrate GitHub Actions workflows --- .github/workflows/build.yaml | 30 ------------------ .github/workflows/cicd.yaml | 58 +++++++++++++++++++++++++++++++++++ .github/workflows/deploy.yaml | 34 -------------------- 3 files changed, 58 insertions(+), 64 deletions(-) delete mode 100644 .github/workflows/build.yaml create mode 100644 .github/workflows/cicd.yaml delete mode 100644 .github/workflows/deploy.yaml diff --git a/.github/workflows/build.yaml b/.github/workflows/build.yaml deleted file mode 100644 index cb64c85..0000000 --- a/.github/workflows/build.yaml +++ /dev/null @@ -1,30 +0,0 @@ -name: build - -on: - push: - branches-ignore: - - main - -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/install@v0.1 - 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 diff --git a/.github/workflows/cicd.yaml b/.github/workflows/cicd.yaml new file mode 100644 index 0000000..37e2d11 --- /dev/null +++ b/.github/workflows/cicd.yaml @@ -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/install@v0.1 + 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 diff --git a/.github/workflows/deploy.yaml b/.github/workflows/deploy.yaml deleted file mode 100644 index 4a50996..0000000 --- a/.github/workflows/deploy.yaml +++ /dev/null @@ -1,34 +0,0 @@ -name: deploy - -on: - push: - branches: ["main"] - -permissions: - contents: read - pages: write - id-token: write - -concurrency: - group: "pages" - cancel-in-progress: true - -jobs: - deploy: - environment: - name: github-pages - url: ${{ steps.deployment.outputs.page_url }} - runs-on: ubuntu-latest - steps: - - uses: actions/checkout@v4 - - uses: actions/setup-node@v4 - with: - node-version: 18 - - run: npm install - - run: npm run build - - uses: actions/configure-pages@v3 - - uses: actions/upload-pages-artifact@v2 - with: - path: "./dist" - - uses: actions/deploy-pages@v2 - id: deployment