|
1 | 1 | # vue-wasm
|
2 | 2 |
|
3 |
| -> Vue web assembly loader |
| 3 | +> A Vue plugin to help expose your Web Assembly functions in your Vue application |
4 | 4 |
|
5 |
| -## Build Setup |
| 5 | + |
6 | 6 |
|
7 |
| -``` bash |
8 |
| -# install dependencies |
9 |
| -npm install |
| 7 | +## Install |
| 8 | +You can use NPM or Yarn to add this plugin to your project |
| 9 | +```bash |
| 10 | +npm install vue-wasm |
| 11 | +# or |
| 12 | +yarn add vue-wasm |
| 13 | +``` |
| 14 | + |
| 15 | +### Webpack Loader |
| 16 | +This package includes a loader to allow you to easily import your `.wasm` files into your webpack project. Just add the following to your webpack config... |
| 17 | + |
| 18 | +```js |
| 19 | +// build/webpack.base.conf.js |
| 20 | +module: { |
| 21 | + rules: [ |
| 22 | + ... |
| 23 | + { |
| 24 | + test: /\.wasm$/, |
| 25 | + loaders: ['wasm-loader'] |
| 26 | + } |
| 27 | + ] |
| 28 | +} |
| 29 | +``` |
| 30 | +### Basic |
| 31 | +Simply import the `vue-wasm` plugin and your Web Assembly modules and then install the plugin whilst passing through an object of `wasm` modules. *The key of the each module will be used to namespace the modules* |
| 32 | + |
| 33 | +```js |
| 34 | +import VueWasm from 'vue-wasm'; |
| 35 | +import addModule from './assets/wasm/add.wasm'; |
10 | 36 |
|
11 |
| -# serve with hot reload at localhost:8080 |
12 |
| -npm run dev |
| 37 | +VueWasm(Vue, { modules: { add: addModule } }); |
| 38 | +``` |
| 39 | +### Await load |
| 40 | +If you need to use your Web Assembly functions in the mounted or created lifecycle hooks, you will need to use a little hack to wait for the plugin to load before initialising your Vue app. |
| 41 | + |
| 42 | +```js |
| 43 | +//main.js |
| 44 | +import VueWasm from 'vue-wasm'; |
| 45 | +import add from './assets/wasm/add.wasm'; |
| 46 | + |
| 47 | +const init = async () => { |
| 48 | + await VueWasm(Vue, { modules: { add } }); |
| 49 | + /* eslint-disable no-new */ |
| 50 | + new Vue({ |
| 51 | + el: '#app', |
| 52 | + template: '<App/>', |
| 53 | + components: { App }, |
| 54 | + }); |
| 55 | +}; |
| 56 | + |
| 57 | +init(); |
| 58 | +``` |
13 | 59 |
|
14 |
| -# build for production with minification |
15 |
| -npm run build |
| 60 | +## Usage |
| 61 | +This will add a `$wasm` object to all of your Vue components. *Note that your functions are nested under the module key that you used whilst installing the plugin.* |
16 | 62 |
|
17 |
| -# build for production and view the bundle analyzer report |
18 |
| -npm run build --report |
| 63 | +```js |
| 64 | +// App.vue |
| 65 | +mounted() { |
| 66 | + console.log(this.$wasm.add.addOne(2)) // 3 |
| 67 | +}, |
19 | 68 | ```
|
20 | 69 |
|
21 |
| -For a detailed explanation on how things work, check out the [guide](http://vuejs-templates.github.io/webpack/) and [docs for vue-loader](http://vuejs.github.io/vue-loader). |
|
0 commit comments