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
ijjk
merged 4 commits into
vercel:canary
from
maxproske:convert-with-webassembly-typescript
Dec 12, 2022
Merged
Changes from 3 commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
71eb6ee
Convert `with-webassembly` example to TypeScript
maxproske cc1fd9b
Support @types/react-dom:18
maxproske 47667e0
Filename config doesn't depend on `moduleIds`, so removing
maxproske 6d3f0a4
Merge branch 'canary' into convert-with-webassembly-typescript
ijjk File filter
Filter by extension
Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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 | ||
``` |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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" | ||
} | ||
} |
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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 | ||
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' } |
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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> | ||
) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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"] | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
export interface AddModuleExports { | ||
add_one(number: Number): Number | ||
} |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
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.There was a problem hiding this comment.
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.