The Import Statement With an Emscripten-Generated WebAssembly Module in Vue.js
Join the DZone community and get the full member experience.
Join For FreeIn my liveBook for WebAssembly in Action, I was recently asked how to use an Emscripten-generated module in Vue.js. In the book, I showed examples using standard JavaScript but didn’t dig into JavaScript frameworks, so I thought this would be an interesting question to look into, especially because I’ve never used Vue.js before.
This article will walk you through the solution that I found. The first thing that’s needed is a WebAssembly module.
The WebAssembly Module
The module is kept simple with just an Add
function that accepts two integer values, sums them, and returns the result. The following snippet shows the C code that’s saved to a file called add.c.
EMSCRIPTEN_KEEPALIVE
int Add(int value1, int value2)
{
return (value1 + value2);
}
With the C code created, the next step is to compile it into a WebAssembly module.
Generating the WebAssembly Module
To import the generated module into Vue.js, we want to use an import statement similar to the following:
xxxxxxxxxx
import Module from './TestImport';
By default, the Emscripten-generated JavaScript is not configured to be imported in this fashion. To tell Emscripten to create the JavaScript so that it can be imported using the import statement, you need to include the -s EXPORT_ES6=1
and -s MODULARIZE=1
flags.
The MODULARIZE
flag will wrap the generated JavaScript code’s Module
object in a function. Ordinarily, just including the JavaScript file in a webpage triggers the automatic download and instantiation of the module. When using this flag, however, you’ll need to create an instance of the Module object to trigger the download and instantiation.
The EXPORT_ES6
flag will include the necessary export object expected by the import statement.
You may also like: WebAssembly vs. JavaScript.
As we tried to import the module, however, we received an error from Vue.js about the var _scriptDir = import.meta.url;
line of code in the generated JavaScript file. To get around this error, we included the -s USE_ES6_IMPORT_META=0
flag to tell Emscripten to use the older form of the import.meta.url
line of code for systems that don’t recognize that code.
Bringing it all together, the following command creates a module that can be imported into Vue.js using an import statement:
xxxxxxxxxx
emcc add.c -s EXTRA_EXPORTED_RUNTIME_METHODS=['ccall'] -s EXPORT_ES6=1 -s MODULARIZE=1 -s USE_ES6_IMPORT_META=0 -o TestImport.js
Now that you have a WebAssembly module, the next step is to import it into your Vue.js application.
The Vue.js Application
The following are two approaches that I came up with for loading a module in Vue.js:
- Create an object on the Vue instance that is null by default. When the first component that needs the module is loaded, it creates the module instance and all components created afterward have access to the module.
- The other approach is to have a local object in the component, where only that component has access to the module.
The first thing that you need to do is copy the TestImport.js and TestImport.wasm files to your Vue.js solution. I placed them in the src folder.
The first approach that I’ll show you is where the module instance is placed on the Vue instance.
1. Module Instance Placed on the Vue Instance
In the main.js file, create a variable on the Vue object called $myModule
so that the module is only downloaded and initialized once. The object is null until instantiated.
The following snippet shows the change in the main.js file:
xxxxxxxxxx
import Vue from 'vue';
import App from './App.vue';
Vue.config.productionTip = true;
Vue.prototype.$myModule = null; // Will hold the module's instance
new Vue({
render: h => h(App)
}).$mount('#app');
The next area that needs to be adjusted is the component.
Adjust the Component
Adjust the Home.vue component with a template that has a button that will call the callAdd
function. The result of the function call will be placed below the button, as shown in the following snippet:
xxxxxxxxxx
<template>
<div>
<button @click="callAdd">Add</button>
<p>Result: {{ result }}</p>
</div>
</template>
Within the Script tag, include the import statement for the module, as shown in the following snippet:
xxxxxxxxxx
import Module from '../TestImport';
Because the module was created using the -s MODULARIZE=1
flag, the module isn’t downloaded and instantiated until you create an instance of the Module object.
In the component’s export object, create a beforeCreate
hook that checks to see if the $myModule
object has been created yet. If not, create a new instance of the Module
object. The download and instantiation of the module is asynchronous, so a Promise is returned. When the Promise resolves, the module instance is assigned to the $myModule
object as shown in the following snippet:
xxxxxxxxxx
beforeCreate() {
if (this.$myModule === null) {
new Module().then(myModule => {
this.$myModule = myModule;
});
}
}
The callAdd
function calls into the module using Emscripten’s ccall
helper function, as shown in the following snippet:
xxxxxxxxxx
callAdd() {
this.result = this.$myModule.ccall('Add',
'number',
['number', 'number'],
[2, 3]);
}
Putting it all together, the following is the content of the Home.vue file:
x
<template>
<div>
<button @click="callAdd">Add</button>
<p>Result: {{ result }}</p>
</div>
</template>
<script>
import Module from '../TestImport';
export default {
beforeCreate() {
if (this.$myModule === null) {
new Module().then(myModule => {
this.$myModule = myModule;
});
}
},
data() {
return {
result: null
}
},
methods: {
callAdd() {
this.result = this.$myModule.ccall('Add',
'number',
['number', 'number'],
[2, 3]);
}
}
};
</script>
<style scoped>
</style>
The source code for the example above can be found here: VuejsGlobalInstance
If you don’t want the module available to all components, the following is how you can use a local variable to hold the module instance instead.
2. Using a Local Module Instance in Your Component
In this case, no changes are needed to your main.js file.
In the Home.vue file, create a variable called moduleInstance
after the import statement, as shown in the following snippet:
xxxxxxxxxx
import Module from '../TestImport';
let moduleInstance = null;
In the beforeCreate
hook, you don’t need to check to see if the object exists yet. All you need to do is create an instance of the Module
object and, when the Promise resolves, assign the module instance to the moduleInstance
variable, as shown in the following snippet:
xxxxxxxxxx
beforeCreate() {
new Module().then(myModule => {
moduleInstance = myModule;
});
}
The callAdd
function calls into the module using Emscripten’s ccall
helper function and the moduleInstance
object, as shown in the following snippet:
xxxxxxxxxx
callAdd() {
this.result = moduleInstance.ccall('Add',
'number',
['number', 'number'],
[2, 3]);
}
Putting it all together, the following is the content of the Home.vue file:
x
<template>
<div>
<button @click="callAdd">Add</button>
<p>Result: {{ result }}</p>
</div>
</template>
<script>
import Module from '../TestImport';
let moduleInstance = null;
export default {
beforeCreate() {
new Module().then(myModule => {
moduleInstance = myModule;
});
},
data() {
return {
result: null
}
},
methods: {
callAdd() {
this.result = moduleInstance.ccall('Add',
'number',
['number', 'number'],
[2, 3]);
}
}
};
</script>
<style scoped>
</style>
The source code for the example above can be found here: VuejsLocalInstance
When I was trying to run this on my machine, I was getting a content-type error in the console window of my browser’s developer tools. For some reason, my Vue.js dev server isn’t using the proper media type for a WebAssembly module. It should be application/wasm.
The developer I was helping didn’t have an issue with this so it’s probably a configuration issue with my computer (Windows with Visual Studio as the IDE). I’ve included this just in case anyone else runs into this issue.
To get around this issue, I needed to modify the vue.config.js file by adding the following:
xxxxxxxxxx
const path = require('path');
const contentBase = path.resolve(__dirname, '..', '..');
module.exports = {
configureWebpack: config => {
config.devServer = {
before(app) {
// use proper mime-type for wasm files
app.get('*.wasm', function (req, res, next) {
var options = {
root: contentBase,
dotfiles: 'deny',
headers: {
'Content-Type': 'application/wasm'
}
};
res.sendFile(req.url, options, function (err) {
if (err) { next(err); }
});
});
}
}
}
}
Summary
In this article, you saw that it’s possible to load an Emscripten-generated WebAssembly module using the import statement if you use the -s EXPORT_ES6=1
and -s MODULARIZE=1
flags when creating the module.
If the tool you’re using has an issue with the import.meta.url
line, you can tell Emscripten to use a different set of code for that line by including the -s USE_ES6_IMPORT_META=0
flag when creating the module.
When using the -s MODULARIZE=1
flag, importing the Emscripten-generated JavaScript file won’t automatically download and instantiate the module. Instead, you need to create an instance of the Module
object. The download and instantiation is asynchronous, so you need to either wait for the Promise to resolve, as was done in this article, or implement a callback function for the onRuntimeInitialized
Emscripten function.
In Vue.js, you can add an object to the Vue instance by adding it to the prototype. When adding something to the prototype, it will be available to all components.
If you don’t want your module available to all components, you can place the instance in a variable local to the component.
For this article, Emscripten 1.39.5 was used to create the WebAssembly module. Visual Studio 2019 was used to create the Vue.js application with the following devDependencies
:
xxxxxxxxxx
"@vue/cli-plugin-babel": "3.0.4",
"@vue/cli-plugin-eslint": "3.0.4",
"@vue/cli-service": "3.0.4",
"eslint": "5.6.0",
"eslint-plugin-vue": "4.7.1",
"vue-template-compiler": "2.5.17"
Disclaimer: I was not paid to write this article, but I am paid royalties on the sale of the book “WebAssembly in Action,” which I mentioned in this article.
Further Reading
Opinions expressed by DZone contributors are their own.
Comments