Théo Champion
Théo Champion

Reputation: 1988

Embed Emscripten WebAssembly module in npm package

I'm building an npm package that contains some WebAssembly loaded from Emscripten module "glue code".

For now, the WASM is fetched from the glue code via a static specified URL

// emscripten glue code
import rppgLoader from './set_asm.js';

async load() {
  // load webassembly code
  this.instance = rppgLoader({
  locateFile(path) {
    return `${process.env.PUBLIC_URL}/wasm/set_asm.wasm`;
  }
});

This URL is application-specific and therefore not compatible with an npm module where everything has to be included and compatible with most build systems (webpack, browserify, ...)

I tried following a gist by google engineer @surma that aims at making wasm/emscripten and webpack work together but got no luck (see last comment on the gist)

What I'm trying to achieve is a npm module transparent for the user. E.g this:

npm install x
import { y } from "x";

should work. That include the wasm code and is compatible with most bundlers.

Is this possible? And if so, is there any examples of npm package that made it work ?

Cheers!

Upvotes: 0

Views: 1516

Answers (1)

anthumchris
anthumchris

Reputation: 9072

I bundled a WASM module into the opus-stream-decoder NPM package. package.json uses the main property to declare the WASM entry point. Also have a look at the test-* files that show import it, using the new ES Modules import syntax or the older require()

Upvotes: 0

Related Questions