name: WASM Build
on:
push:
branches: [main]
paths:
- 'src/**'
- 'Cargo.toml'
- 'wasm/**'
- '.github/workflows/wasm.yml'
pull_request:
branches: [main]
paths:
- 'src/**'
- 'Cargo.toml'
- 'wasm/**'
workflow_dispatch:
permissions:
contents: read
id-token: write
jobs:
build:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v4
- name: Install Rust
uses: dtolnay/rust-toolchain@stable
with:
targets: wasm32-unknown-unknown
- name: Install wasm-pack
run: cargo install wasm-pack
- name: Build WASM
run: |
wasm-pack build --target web -- --no-default-features --features wasm
rm -rf wasm/pkg
mv pkg wasm/
- name: Run WASM tests (Node)
run: wasm-pack test --node -- --no-default-features --features wasm
- name: Run npm tests
run: |
cd wasm
npm test
- name: Upload artifact
uses: actions/upload-artifact@v4
with:
name: wasm-pkg
path: wasm/pkg/
publish:
needs: build
runs-on: ubuntu-latest
if: github.ref == 'refs/heads/main' && github.event_name == 'push'
steps:
- uses: actions/checkout@v4
- uses: actions/download-artifact@v4
with:
name: wasm-pkg
path: wasm/pkg/
- uses: actions/setup-node@v4
with:
node-version: '20'
registry-url: 'https://registry.npmjs.org'
- name: Sync version from Cargo.toml
run: |
VERSION=$(grep '^version' Cargo.toml | head -1 | sed 's/.*"\(.*\)".*/\1/')
echo "Syncing npm package version to $VERSION"
cd wasm
npm version $VERSION --no-git-tag-version --allow-same-version
- name: Publish to npm
run: |
cd wasm
npm publish --access public --provenance || echo "Version already published or publish failed"