A loader to reduce wasm code size based on Binaryen.
This module requires a minimum of Node v6.9.0 and Webpack v4.0.0.
To begin, you'll need to install binaryen-loader:
$ npm install binaryen-loader --save-devThen add the loader to your webpack config. For example:
file.wasm
import file from 'file.wasm';webpack.config.js
module.exports = {
module: {
rules: [
{
test: /\.wasm$/,
use: [
{
loader: `binaryen-loader`,
options: {...options}
}
]
}
]
}
}And run webpack via your preferred method.
The options in binaryen-loader correspond to module-optimization section in binaryen.js.
- Type:
Boolean - Default:
false - Correspond to:
setDebugInfo
Enables or disables debug information in emitted binaries.
// in your webpack.config.js
{
loader: `binaryen-loader`,
options: {
debug: true
}
}- Type:
Integer - Default:
2 - Expected value: from
0to2 - Correspond to:
setOptimizeLevel
Sets the optimization level that correspond to flag -O0, -O1, -O2, etc.
// in your webpack.config.js
{
loader: `binaryen-loader`,
options: {
optimization: {
level: 0
}
}
}- Type:
Integer - Default:
1 - Expected value: from
0to2 - Correspond to:
setShrinkLevel
Sets the shrink level that correspond to flag -O0, -Os, -Oz.
// in your webpack.config.js
{
loader: `binaryen-loader`,
options: {
optimization: {
shrinkLevel: 2
}
}
}- Type:
String|Array<String> - Default: see
DefaultGlobalOptimizationin pass.cpp - Expected value: see
PassRegistry::registerPassesin pass.cpp - Correspond to:
runPasses
Runs the specified passes on the module.
// in your webpack.config.js
{
loader: `binaryen-loader`,
options: {
transformation: {
passes: 'post-emscripten'
}
}
}or
// in your webpack.config.js
{
loader: `binaryen-loader`,
options: {
transformation: {
passes: [
'post-emscripten',
'remove-memory'
]
}
}
}- Type:
String - Default:
null - Expected value: any valid function name exported from
.wasmfile - Correspond to:
optimizeFunction(if passes undefined) orrunPassesOnFunction(if passes defined)
Optimizes a single function using defined and/or default passes.
// in your webpack.config.js
{
loader: `binaryen-loader`,
options: {
transformation: {
function: 'add'
}
}
}The following examples show how to use binaryen-loader chained with wasm-loader.
webpack.config.js
module.exports = {
module: {
rules: [{
test: /\.wasm$/,
use: [{
loader: 'wasm-loader'
}, {
loader: `binaryen-loader`,
options: {
transformation: {
passes: [
'post-emscripten',
'remove-memory'
]
}
}
}]
}]
}
}arithmatic.wasm (if converted into .wat)
(module
(type $t0 (func (param i32 i32) (result i32)))
(func $add (export "add") (type $t0) (param $0 i32) (param $1 i32) (result i32)
get_local $0
get_local $1
i32.add)
(memory $memory (export "memory") 1))implementation.js
import loadArithmatic from 'arithmatic.wasm';
loadArithmatic.then(wasm =>
const { add } = wasm.instance.exports;
console.log(add(1, 2)); // 3
);Please take a moment to read our contributing guidelines if you haven't yet done so.
This project generated and modified based on webpack-defaults. Default Contribution guideline, Issue and PR template are intentionally left behind, not edited until there is some feedback about that 🙂