Coder Social home page Coder Social logo

wasm-loader's Introduction

Build Status Package Quality

⚠️ ⚠️ This loader is DEPRECATED. Use NATIVE Webpack 5 Support for WebAssembly as described here or follow a tiny demo example here. ⚠️ ⚠️

WASM Binary Module loader for Webpack

A simple .wasm binary file loader for Webpack. Import your wasm modules directly into your bundle as Constructors which return WebAssembly.Instance. This avoids the need to use fetch and parse for your wasm files. Imported wasm files are converted to Uint8Arrays and become part of the full JS bundle!

Install

Install package: npm install --save wasm-loader

Usage

Edit webpack.config.js:

  loaders: [
    {
      test: /\.wasm$/,
      loaders: ['wasm-loader']
    }
  ]

Optimizations

Dead code elemination

This is an experimental feature and thus not activated by default.

You can activate it by passing dce=1 to the import and by specifying manually (for now) the exported elements you use, like the following example:

import createInstance from "./add.wasm?dce=1&add&test"

createInstance()
.then(m => {
  console.log(m.instance.exports.add(1, 2));
  console.log(m.instance.exports.test());
});

Everything else in the add.wasm binary will be removed.

Include wasm from your code

Grab your pre-built wasm file. For demo purposes we will use the excellent WasmExplorer. factorial.wasm file exports a function returning a factorial for a given number.

With the loader you can import this file directy

import makeFactorial from 'wasm/factorial';

The default export from the loader is a function returning native Promise. The promise resolves to a WebAssembly.Instance.

makeFactorial().then(instance => {
  // What is with the weird exports._Z4facti function?
  // This is how the function name is encoded by the C++ to wasm compiler
  const factorial = instance.exports._Z4facti;

  console.log(factorial(1)); // 1
  console.log(factorial(2)); // 2
  console.log(factorial(3)); // 6
});

deps can be passed in to override defaults. For example

makeFactorial({
  'global': {},
  'env': {
    'memory': new WebAssembly.Memory({initial: 100, limit: 1000}),
    'table': new WebAssembly.Table({initial: 0, element: 'anyfunc'})
  }
}).then(instance => { /* code here */ });

Default deps are:

{
  'global': {},
  'env': {
    'memory': new Memory({initial: 10, limit: 100}),
    'table': new Table({initial: 0, element: 'anyfunc'})
  }
}

A note about default deps(importsObject)

Default importsObject is meant to be used for a very basic wasm module. Most likely it will not suffice for something not dead simple compiled with emscripten. This is intentional. Supply your own imports to match the requirements of your wasm module(s). Some options are compiling your source code into S-syntax(.wast) examining that output, checking the imports. Compile the s-syntax file with asm2wasm into the final wasm module.

wasm-loader's People

Contributors

ballercat avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.