Modern.js supports import WebAssembly assets in code.
WebAssembly (Wasm) is a portable, high-performance binary format designed to execute CPU-intensive computing tasks in modern web browsers, bringing performance and reliability similar to native compiled code to the web platform.
You can import a WebAssembly module directly in a JavaScript file:
WebAssembly modules can also be imported via dynamic import:
You can also get the path of a WebAssembly module using the new URL
syntax:
When a .wasm
asset is imported, it will be output by Modern.js to the dist/static/wasm
directory by default.
You can change the output directory of .wasm
files via output.distPath config.
When you import a Wasm file in TypeScript code, you usually need to add the corresponding type declaration.
For example, the add.wasm
file exports an add()
method, then you can create an add.wasm.d.ts
file in the same directory and add the corresponding type declaration: