简体   繁体   中英

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. Eg 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!

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()

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM