wasm-from-emscripten-result-loader
v1.0.0
Published
Webpack loader that transforms wasm and glue js source files into javascript bundles.
Downloads
19
Maintainers
Readme
WASM Webpack Loader
中文文档
Load wasm and glue js source files directly into javascript with a zero bloat.
The module refers to the cpp-wasm-loader, thanks for those source code.
Installation
- Run
npm i wasm-from-emscripten-result-loader --save-dev
. - Add this object to the
rules
section of your webpack build
{
test: /\.wasm$/,
type: 'javascript/auto',
use: [
{
loader: 'wasm-from-emscripten-result-loader'
},
]
}
- Make sure
.wasm
are in the webpack resolve object
resolve: {
extensions: ['.js', '.wasm']
}
Minimal Example
You can also view a complete working example on github here.
add.c
#include <emscripten.h>
extern "C"
{
/* Declare Javascript function for use in C/C++ */
extern int sub(int a, int b);
EMSCRIPTEN_KEEPALIVE /* <= Needed to export this function to javascript "module.exports" */
int add(int a, int b)
{
return a + b;
}
}
Build c++ source code
emcc ./add.cc -s WASM=1 -o ./add.js -std=c++11 --bind
note: -s WASM=1 -o {path}/{name}.js
is Required
main.js
import wasm from './add.wasm';
wasm.init((imports) => {
// custom javascript function that can be called from C;
imports._sub = (a, b) => a - b;
return imports;
}).then((module) => {
console.log(module.exports.add(1, 2)); // 3
console.log(module.memory) // Raw ArrayBuffer Memory object
console.log(module.memoryManager) // Memory Manager Class
console.log(module.raw) // The complete unmodified return value of the webassembly init promise.
}).catch((err) => {
console.error(err);
})
MIT License
Copyright 2018 zhi.zhou
Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.