Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/mys1024/vite-plugin-wat
A Vite plugin for WebAssembly Text Format.
https://github.com/mys1024/vite-plugin-wat
plugin vite vitejs wasm wat webassembly
Last synced: about 1 month ago
JSON representation
A Vite plugin for WebAssembly Text Format.
- Host: GitHub
- URL: https://github.com/mys1024/vite-plugin-wat
- Owner: mys1024
- License: mit
- Created: 2022-09-30T14:02:54.000Z (about 2 years ago)
- Default Branch: main
- Last Pushed: 2022-10-15T08:32:30.000Z (about 2 years ago)
- Last Synced: 2024-11-15T03:09:59.967Z (about 1 month ago)
- Topics: plugin, vite, vitejs, wasm, wat, webassembly
- Language: TypeScript
- Homepage: https://www.npmjs.com/package/vite-plugin-wat
- Size: 85.9 KB
- Stars: 1
- Watchers: 1
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# vite-plugin-wat
A [Vite](https://vitejs.dev/) plugin for [WebAssembly Text Format](https://webassembly.github.io/spec/core/text/index.html).
## Install
```shell
npm install -D vite-plugin-wat
```## Usage
`vite.config.js`:
```javascript
import { defineConfig } from 'vite'
import Wat from 'vite-plugin-wat'export default defineConfig({
plugins: [Wat()]
})
````src/add.wat`:
```wat
(module
(func $add (param $p0 i32) (param $p1 i32) (result i32)
local.get $p0
local.get $p1
i32.add
)
(export "add" (func $add))
)
````src/index.js`:
```javascript
import initAddModule from './add.wat?init'const { add } = (await initAddModule({})).exports
console.log(add(1, 2)) // 3
```**NOTE**: See [this](https://vitejs.dev/guide/features.html#webassembly) for more information about `?init`.
## TypeScript Support
Create `src/shims.d.ts` with the following content:
```typescript
///declare module '*.wat?init' {
export { default } from '*.wasm?init'
}
```Use type casting when importing from WASM module:
```typescript
import initAddModule from './add.wat?init'interface AddModuleExports {
add(a: number, b: number): number
}const { add } = (await initAddModule({}))
.exports as unknown as AddModuleExports
console.log(add(1, 2)) // 3
```## License
MIT