https://github.com/drsensor/binaryen-loader
webpack loader for Binaryen
https://github.com/drsensor/binaryen-loader
my-study webassembly webpack-loader
Last synced: 11 months ago
JSON representation
webpack loader for Binaryen
- Host: GitHub
- URL: https://github.com/drsensor/binaryen-loader
- Owner: DrSensor
- License: mit
- Created: 2018-06-06T18:22:34.000Z (about 8 years ago)
- Default Branch: master
- Last Pushed: 2023-02-27T23:52:21.000Z (over 3 years ago)
- Last Synced: 2024-10-13T18:18:38.989Z (over 1 year ago)
- Topics: my-study, webassembly, webpack-loader
- Language: JavaScript
- Size: 370 KB
- Stars: 3
- Watchers: 2
- Forks: 0
- Open Issues: 32
-
Metadata Files:
- Readme: README.md
- Changelog: CHANGELOG.md
- Contributing: .github/CONTRIBUTING.md
- License: LICENSE
- Codeowners: .github/CODEOWNERS
Awesome Lists containing this project
README
[![npm][npm]][npm-url]
[![node][node]][node-url]
[![deps][deps]][deps-url]
[![tests][tests]][tests-url]
[![size][size]][size-url]
# 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`:
```console
$ npm install binaryen-loader --save-dev
```
Then add the loader to your `webpack` config. For example:
**file.wasm**
```js
import file from 'file.wasm';
```
**webpack.config.js**
```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](https://github.com/AssemblyScript/binaryen.js/#module-optimization) section in [binaryen.js][].
### `debug`
- Type: `Boolean`
- Default: `false`
- Correspond to: `setDebugInfo`
Enables or disables debug information in emitted binaries.
```js
// in your webpack.config.js
{
loader: `binaryen-loader`,
options: {
debug: true
}
}
```
### `optimization.level`
- Type: `Integer`
- Default: `2`
- Expected value: from `0` to `2`
- Correspond to: `setOptimizeLevel`
Sets the optimization level that correspond to flag `-O0`, `-O1`, `-O2`, etc.
```js
// in your webpack.config.js
{
loader: `binaryen-loader`,
options: {
optimization: {
level: 0
}
}
}
```
### `optimization.shrinkLevel`
- Type: `Integer`
- Default: `1`
- Expected value: from `0` to `2`
- Correspond to: `setShrinkLevel`
Sets the shrink level that correspond to flag `-O0`, `-Os`, `-Oz`.
```js
// in your webpack.config.js
{
loader: `binaryen-loader`,
options: {
optimization: {
shrinkLevel: 2
}
}
}
```
### `transformation.passes`
- Type: `String|Array`
- Default: see [`DefaultGlobalOptimization` in pass.cpp](https://github.com/WebAssembly/binaryen/blob/master/src/passes/pass.cpp#L190-L204)
- Expected value: see [`PassRegistry::registerPasses` in pass.cpp](https://github.com/WebAssembly/binaryen/blob/master/src/passes/pass.cpp#L67-L126)
- Correspond to: `runPasses`
Runs the specified passes on the module.
```js
// in your webpack.config.js
{
loader: `binaryen-loader`,
options: {
transformation: {
passes: 'post-emscripten'
}
}
}
```
or
```js
// 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) or `runPassesOnFunction` (if passes defined)
Optimizes a single function using defined and/or default passes.
```js
// 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**
```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`)
```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**
```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](./.github/CONTRIBUTING.md)
## License
#### [MIT](./LICENSE)
---
This project generated and modified based on [webpack-defaults](https://github.com/webpack-contrib/webpack-defaults).
Default Contribution guideline, Issue and PR template are intentionally left behind, not edited until there is some feedback about that 🙂
[Binaryen]: https://github.com/WebAssembly/binaryen
[binaryen.js]: https://github.com/AssemblyScript/binaryen.js
[npm]: https://img.shields.io/npm/v/binaryen-loader.svg
[npm-url]: https://npmjs.com/package/binaryen-loader
[node]: https://img.shields.io/node/v/binaryen-loader.svg
[node-url]: https://nodejs.org
[deps]: https://david-dm.org/DrSensor/binaryen-loader.svg
[deps-url]: https://david-dm.org/DrSensor/binaryen-loader
[tests]: https://img.shields.io/circleci/project/github/DrSensor/binaryen-loader.svg
[tests-url]: https://circleci.com/gh/DrSensor/binaryen-loader
[cover]: https://codecov.io/gh/DrSensor/binaryen-loader/branch/master/graph/badge.svg
[cover-url]: https://codecov.io/gh/DrSensor/binaryen-loader
[size]: https://packagephobia.now.sh/badge?p=binaryen-loader
[size-url]: https://packagephobia.now.sh/result?p=binaryen-loader