binaryen-loader
v0.1.1
Published
A webpack loader to reduce WebAssembly file binary size, based on Binaryen
Downloads
14
Readme
binaryen-loader
A loader to reduce wasm code size based on Binaryen.
Requirements
This module requires a minimum of Node v6.9.0 and Webpack v4.0.0.
Getting Started
To begin, you'll need to install binaryen-loader
:
$ npm install binaryen-loader --save-dev
Then 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.
Options
The options in binaryen-loader correspond to module-optimization section in binaryen.js.
debug
- 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
}
}
optimization.level
- Type:
Integer
- Default:
2
- Expected value: from
0
to2
- 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
}
}
}
optimization.shrinkLevel
- Type:
Integer
- Default:
1
- Expected value: from
0
to2
- 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
}
}
}
transformation.passes
- Type:
String|Array<String>
- Default: see
DefaultGlobalOptimization
in pass.cpp - Expected value: see
PassRegistry::registerPasses
in 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'
]
}
}
}
transformation.function
(still experimental)
- Type:
String
- Default:
null
- Expected value: any valid function name exported from
.wasm
file - 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'
}
}
}
Examples
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
);
Contributing
Please take a moment to read our contributing guidelines if you haven't yet done so.
CONTRIBUTING
License
MIT
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 🙂