Search code examples
typescriptwebpackwebassemblyemscripten

How to automatically bundle wasm used by a dependency?


My website uses a package.
This package is a Typescript port of a low level networking library written in C. The porting was done with emscripten, which gives us a networking.wasm and networking.js files. We added some manual TS code to use the package in a mode TS-idiomatic way.

I want my react website to depend on that package, but I don't want to have to manually copy the networking.wasm file into my public/static/js directory for the underlying 'networking' package to work. I want the 'networking' package to work out of the box with a simple npm install.

How can I make the 'networking' package my site depends on tell webpack to add the networking.wasm file where it needs to be to be GET correctly ?
If this is not possible, what would be the cleanest solution to expose the .wasm file for the user of the package to copy in the right place to get GET ?


Solution

  • For emscripten user, you can build with the flag

    -s SINGLE_FILE=1
    

    so the entire wasm binary content gets written in the generated .js file as a literal string. Therefore all you need is the generated javascript file, no need to worry about the webassembly file.