0.0.3 • Published 6 years ago

wasm-module-loader v0.0.3

Weekly downloads
2
License
MIT
Repository
github
Last release
6 years ago

wasm-module-loader

A Webpack loader that wraps wasm-module-preprocessor.

Install

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

Usage

wasm-module-loader 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.js

import wasmModule from 'factorial.wasm';

export default async () => {
  const {
    instance: {
      exports: {
        factorial = () => undefined,
      },
    },
  } = await wasmModule(
    // Add an optional `importObject`:
    // {
    //   global: {},
    //   env: {},
    // }
  );

  return factorial(number);
}

webpack.config.js

module.exports = {
  module: {
    rules: [
      {
        test: /\.wasm?$/,
        use: [
          {
            loader: 'wasm-module-loader',
            options: {
              // Defaults to:
              // sync: false,
            },
          },
        ],
      },
    ],
  },
};

Sync

file.js

import wasmModule from 'factorial.wasm';

export default () => {
  const {
    exports: {
      factorial = () => undefined,
    },
  } = wasmModule(
    // Add an optional `importObject`:
    // {
    //   global: {},
    //   env: {},
    // }
  );

  return factorial(number);
}

webpack.config.js

module.exports = {
  module: {
    rules: [
      {
        test: /\.wasm?$/,
        use: [
          {
            loader: 'wasm-module-loader',
            options: {
              // Only use this for small wasm modules. The max size of the
              // binary will, by default, be restricted to less than 4KiB.
              sync: false,
            },
          },
        ],
      },
    ],
  },
};

Options

All options are defined by and are passed directly to wasm-module-preprocessor; check its documentation for more a in-depth explanation.