| FazBrowse GitHub Viewer | Trending | | Home |
| Tools: [Download Repo ZIP] [Original HTTPS Page] |
| Name | Name | Last commit date | ||
|---|---|---|---|---|
A Vite plugin that adds support for the "use wasm" directive in TypeScript files, enabling seamless integration and compilation of WebAssembly modules.
It uses the AssemblyScript compiler to compile TypeScript code into WebAssembly. AssemblyScript is a subset of TypeScript, it allows developers to write high-performance code that can be executed in a WebAssembly environment. So not every TypeScript code can be compiled to WebAssembly. For more info on that, check the AssemblyScript documentation.
npm install unplugin-use-wasm --save-devThis package also have the AssemblyScript portable types, so you do not get warnings when you try to use the types i32, f64, bool, etc.
In your tsconfig.json, add the following to the compilerOptions:
{
"compilerOptions": {
"typeRoots": ["./node_modules/unplugin-use-wasm/dist/types"],
"types": ["assemblyscript"]
}
}First, add the plugin to your bundler configuration file, here I will use Vite for example (vite.config.ts or vite.config.js):
import { defineConfig } from "vite";
import useWasm from "unplugin-use-wasm/vite";
export default defineConfig({
plugins: [
useWasm({
browser: true,
}),
],
});The import path can be changed to unplugin-use-wasm/rollup or unplugin-use-wasm/rolldown depending on the bundler you are using.
Then, you can use the "use wasm" directive in your TypeScript files to indicate that the file should be compiled to WebAssembly:
// sum.ts
"use wasm";
export function sum(a: i32, b: i32): i32 {
return a + b;
}You can then import and use the WebAssembly module in your application:
import { sum } from "./sum.ts";
const result = sum(1, 2);
console.log(result); // 3| Back | FazBrowse Home | New Git URL |