I noticed there is heavy traffic in my repository with minimal code needed to reproduce a Can not use web-assembly rust impl error. I decided to create this gist to show how I am connecting WASM-Rust with TypeScript.
- Initialize npm project
npm init -y
- Install the following dependencies
npm i -D webpack webpack-cli webpack-dev-server html-webpack-plugin ts-loader ypescript
- Create
index.js
in and export your App (this will be an entry point of your application) - Create
bootstrap.ts
file in which you will import yourindex.js
asynchronously. We have to load the app asynchronously because.wasm
files have to be loaded asynchronously - Create
webpack.config.js
. Here we have to useexperimetns: syncWebAssembly
to load our.wasm
files - Add
serve
andbuild
script to yourpackage.json
- In root of your project create wasm project using
wasm-pack
wasm-pack new name-of-package
- Go to package directory
cd ./name-of-package
- Run
wasm-pack build
to build your wasm package
- Install your wasm package (if you publish your wasm package then you can install it via
npm
)npm install wasm-package-name/pkg
- Make sure that you can find this dependecy in your
package.json
"dependencies": { "wasm-package-name": "file:./wasm-package-name/pkg" },
- Make sure you have
"moduleResolution": "node"
in yourtsconfig.json
If you want to see ready ready example I preapread a reposityry which you can find here: web-assembly-rust-typescript-template