# wasm-module-preprocessor

> A general-purpose module for preprocessing WebAssembly (wasm).

Latest version **0.0.3** (published 2017-12-11) · MIT license · 0 weekly downloads

## Install

```sh
npm install wasm-module-preprocessor
pnpm add wasm-module-preprocessor
yarn add wasm-module-preprocessor
bun add wasm-module-preprocessor
```

## Health

**Score 15/100 (F)** — status: abandoned.

Positive: no vulnerabilities.

Warnings: low downloads; no types; no esm support; pre 1.0.

Negative: abandoned; low maintenance score.

## Facts

| | |
|---|---|
| Version | 0.0.3 |
| Published | 2017-12-11 |
| First published | 2017-12-07 |
| Weekly downloads | 0 |
| License | MIT |
| TypeScript types | none |
| Module format | CommonJS |
| Dependencies | 0 |
| Known vulnerabilities | 0 |
| Install scripts | no |
| GitHub stars | 2 |
| Author | Dylan Frankland |
| Maintainers | dfrankland |
| Keywords | preprocessor, wasm, webassembly, asm, module, bundler |

## Links

- npm: https://www.npmjs.com/package/wasm-module-preprocessor
- Repository: https://github.com/dfrankland/wasm-module-preprocessors
- Homepage: https://github.com/dfrankland/wasm-module-preprocessors/tree/master/packages/wasm-module-preprocessor#readme
- Issues: https://github.com/dfrankland/wasm-module-preprocessors/issues
- npm.io page: https://npm.io/package/wasm-module-preprocessor

## Alternatives

- [raw-loader](https://npm.io/package/raw-loader.md) — 4.3M weekly downloads
- [plop](https://npm.io/package/plop.md) — 1.4M weekly downloads
- [webpack-deadcode-plugin](https://npm.io/package/webpack-deadcode-plugin.md) — 80.3K weekly downloads
- [@storybook/preact-vite](https://npm.io/package/@storybook/preact-vite.md) — 54.2K weekly downloads
- [vite-plugin-transform](https://npm.io/package/vite-plugin-transform.md) — 2.4K weekly downloads

## Recent versions

- 0.0.3 (latest) — 2017-12-11
- 0.0.2 — 2017-12-11
- 0.0.1 — 2017-12-07

## README

# wasm-module-preprocessor

A general-purpose module for preprocessing WebAssembly (wasm).

## Install

```bash
npm install --save-dev wasm-module-preprocessor
```

## Usage

`wasm-module-preprocessor` by default loads wasm modules asynchronously, and
takes an optional `importObject`. All wasm modules are validated on build, so
that there is no need for runtime testing and beyond just validating wasm
modules, the size of the wasm module is checked too.

### Async

**file.wasm**

```
<RANDOM GARBLED BINARY DATA>
```

**test.js**

```js
import wasmModulePreprocessor from 'wasm-module-preprocessor';
import { readFileSync } from 'fs';
import { runInNewContext } from 'vm';

const bufferSource = readFileSync('file.wasm');

const wasmModuleExportString = wasmModulePreprocessor(
  bufferSource,
  {
    // Defaults to:
    // sync: false,
  }
);

const wasmModuleExport = runInNewContext(`(() => ${wasmModuleExportString})()`);

(async () => {
  try {
    const {
      instance: {
        exports: {
          main = () => undefined,
        },
      },
    } = await wasmModuleExport(
      // Add an optional `importObject`:
      // {
      //   global: {},
      //   env: {},
      // }
    );

    console.log(main() === 3); // true
  } catch (err) {
    console.error(err);
  }
})();
```

### Sync


**file.wasm**

```
<RANDOM GARBLED BINARY DATA>
```

**test.js**

```js
import wasmModulePreprocessor from 'wasm-module-preprocessor';
import { readFileSync } from 'fs';
import { runInNewContext } from 'vm';

const bufferSource = readFileSync('file.wasm');

const wasmModuleExportString = wasmModulePreprocessor(
  bufferSource,
  // Only use this for small wasm modules. The max size of the
  // binary will, by default, be restricted to less than 4KiB.
  { sync: true },
);

const wasmModuleExport = runInNewContext(`(() => ${wasmModuleExportString})()`);

const {
  exports: {
    main = () => undefined,
  },
} = wasmModuleExport(
  // Add an optional `importObject`:
  // {
  //   global: {},
  //   env: {},
  // }
);

console.log(main() === 3); // true
```

## Documentation

### wasmModulePreprocessor(bufferSource\[, options\])

*   `bufferSource` `<string>` | `<Buffer>` *Default:* `<Buffer >`

*   `options` `<Object>`

    *   `encoding` `<string>` *Default:* `'binary'`

    *   `maxBufferSourceSize` `<integer>` *Default:* `1073741824`

    *   `maxBufferSourceSizeSync` `<integer>` *Default:* `4096`

    *   `validate` `<boolean>` *Default:* `true`

    *   `sync` `<boolean>` *Default:* `false`

    *   `template` `<Object>`

        *   `importObjectArg` `<string>`

        *   `defaultImportObject` `<Function>`

            *   `templateArgs` `<Object>`

                *   `importObjectArg` `<string>`

        *   `bufferSource` `<Function>`

            *   `templateArgs` `<Object>`

                *   `buffer` `<Buffer>`

        *   `wasmModuleInstance` `<Function>`

            *   `templateArgs` `<Object>`

                *   `bufferSource` `<string>`

                *   `importObject` `<string>`

        *   `wasmModuleInstanceSync` `<Function>`

            *   `templateArgs` `<Object>`

                *   `bufferSource` `<string>`

                *   `importObject` `<string>`

        *   `moduleExport` `<Function>`

            *   `templateArgs` `<Object>`

                *   `bufferSource` `<string>`

                *   `importObject` `<string>`

`bufferSource` is the data from the binary wasm file. `encoding` is ignored if
`bufferSource` is a buffer.

`maxBufferSourceSize` is the maximum size in bytes that the `bufferSource` can
be and defaults to the max allowed by Node/V8 and most browsers.
`maxBufferSourceSizeSync` is the maximum size in bytes that the `bufferSource`
can be if used synchronously and defaults the max allowed by Chrome and most
other browsers (Node/V8 has no such limitation).

`validate` is whether or not `WebAssembly.validate` is used to check the
`bufferSource` before preprocessing.

`sync` is whether or not to preprocess the module as synchronous.

`template` is an object with properties to construct the string returned.

---
_Source: https://npm.io/package/wasm-module-preprocessor · Machine-readable twin of the npm.io package page. Health data is recomputed on every publish._
