Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix with-webassembly example and convert to Typescript #43677

Merged
merged 4 commits into from Dec 12, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
2 changes: 2 additions & 0 deletions examples/with-webassembly/README.md
Expand Up @@ -32,4 +32,6 @@ To compile `src/add.rs` to `add.wasm` run:
npm run build-rust
# or
yarn build-rust
# or
pnpm build-rust
```
24 changes: 24 additions & 0 deletions examples/with-webassembly/components/RustComponent.tsx
@@ -0,0 +1,24 @@
import type { AddModuleExports } from '../wasm'
import dynamic from 'next/dynamic'

interface RustComponentProps {
number: Number
}

const RustComponent = dynamic({
loader: async () => {
// Import the wasm module
// @ts-ignore
const exports = (await import('../add.wasm')) as AddModuleExports
const { add_one: addOne } = exports

// Return a React component that calls the add_one method on the wasm module
return ({ number }: RustComponentProps) => (
<div>
<>{addOne(number)}</>
</div>
)
},
})

export default RustComponent
13 changes: 10 additions & 3 deletions examples/with-webassembly/next.config.js
@@ -1,11 +1,18 @@
/** @type {import('next').NextConfig} */
module.exports = {
webpack(config) {
config.output.webassemblyModuleFilename = 'static/wasm/[modulehash].wasm'
const nextConfig = {
webpack(config, { isServer, dev }) {
// Use the client static directory in the server bundle and prod mode
// Fixes `Error occurred prerendering page "/"`
config.output.webassemblyModuleFilename =
isServer && !dev
? '../static/wasm/[modulehash].wasm'
: 'static/wasm/[modulehash].wasm'

// Since Webpack 5 doesn't enable WebAssembly by default, we should do it manually
config.experiments = { ...config.experiments, asyncWebAssembly: true }

return config
},
}

module.exports = nextConfig
16 changes: 11 additions & 5 deletions examples/with-webassembly/package.json
@@ -1,14 +1,20 @@
{
"private": true,
"dependencies": {
"next": "latest",
"react": "^18.2.0",
"react-dom": "^18.2.0"
},
"scripts": {
"dev": "next",
"build": "next build",
"build-rust": "rustc --target wasm32-unknown-unknown -O --crate-type=cdylib src/add.rs -o add.wasm",
"start": "next start"
},
"dependencies": {
"next": "latest",
"react": "^18.2.0",
"react-dom": "^18.2.0"
},
"devDependencies": {
"@types/node": "^18.11.10",
"@types/react": "^18.0.26",
"@types/react-dom": "^18.0.9",
"typescript": "^4.9.3"
}
}
11 changes: 0 additions & 11 deletions examples/with-webassembly/pages/api/edge.js

This file was deleted.

16 changes: 16 additions & 0 deletions examples/with-webassembly/pages/api/edge.ts
@@ -0,0 +1,16 @@
import type { AddModuleExports } from '../../wasm'
// @ts-ignore
import addWasm from '../../add.wasm?module'

const module$ = WebAssembly.instantiate(addWasm)

export default async function handler() {
const instance = (await module$) as any
Comment on lines +5 to +8
Copy link
Contributor Author

@maxproske maxproske Dec 3, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
const module$ = WebAssembly.instantiate(addWasm)
export default async function handler() {
const instance = (await module$) as any
export default async function handler() {
const instance = (await WebAssembly.instantiate(addWasm)) as any

I'm not aware what the advantage of initiating the wasm module$ outside the API handler like this is. But I've left it as-is, unless you want to commit this suggestion.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Instantiating outside of the handler allows re-using the same instance which is a bit more performant so I think it's good we leave that as is.

const exports = instance.exports as AddModuleExports
const { add_one: addOne } = exports
const number = addOne(10)

return new Response(`got: ${number}`)
}

export const config = { runtime: 'experimental-edge' }
24 changes: 0 additions & 24 deletions examples/with-webassembly/pages/index.js

This file was deleted.

15 changes: 15 additions & 0 deletions examples/with-webassembly/pages/index.tsx
@@ -0,0 +1,15 @@
import { useRouter } from 'next/router'
import Link from 'next/link'
import RustComponent from '../components/RustComponent'

export default function Page() {
const { query } = useRouter()
const number = parseInt(query.number as string) || 30

return (
<div>
<RustComponent number={number} />
<Link href={`/?number=${number + 1}`}>+</Link>
</div>
)
}
20 changes: 20 additions & 0 deletions examples/with-webassembly/tsconfig.json
@@ -0,0 +1,20 @@
{
"compilerOptions": {
"target": "es5",
"lib": ["dom", "dom.iterable", "esnext"],
"allowJs": true,
"skipLibCheck": true,
"strict": true,
"forceConsistentCasingInFileNames": true,
"noEmit": true,
"esModuleInterop": true,
"module": "esnext",
"moduleResolution": "node",
"resolveJsonModule": true,
"isolatedModules": true,
"jsx": "preserve",
"incremental": true
},
"include": ["next-env.d.ts", "**/*.ts", "**/*.tsx", "wasm.d.ts"],
"exclude": ["node_modules"]
}
3 changes: 3 additions & 0 deletions examples/with-webassembly/wasm.d.ts
@@ -0,0 +1,3 @@
export interface AddModuleExports {
add_one(number: Number): Number
}