npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

wasm-loader

v1.3.0

Published

Webpack loader for WASM

Downloads

18,049

Readme

Build Status Package Quality

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.