Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What is the preferred way to publish a wasm library on npm?

Is there a good way to publish a library that uses a wasm binary on npm ?

So far, I encountered several issues. Ideally, I would like something that would be :

  • Totally transparent to the user. They can simply npm install the package, then import {my_function} from my_package, without even having to worry about whether the package uses a wasm binary or not.

  • Compatible with all major asset bundlers. Whether the user uses webpack, rollup, parcel, or something else, he should not have to configure anything to have it pack my wasm file, distribute it together with other assets, and make it accessible from my library.

  • Efficient. Ideally, the wasm file would not be inlined inside a js file, so that it can be parsed and compiled in a streaming fashion. This point is less important than the others, but it would be nice to be able to take advantage of all the speed wasm offers.

like image 437
lovasoa Avatar asked Jul 28 '19 15:07

lovasoa


People also ask

How does npm publishing work?

Publishes a package to the registry so that it can be installed by name. By default npm will publish to the public registry. This can be overridden by specifying a different default registry or using a scope in the name, combined with a scope-configured registry (see package. json ).

Does npm publish Run build?

So, whenever you run npm publish command, the following scripts will run sequentially: npm test then npm run lint then npm run build and finally npm publish .


1 Answers

I think this largely depends on the ecosystem and language you are starting in. One path that I'm familiar with is a project to port Rust to wasm. From their documentation here it appears their process achieves your first bullet point of transparent to the user.

Also it appears their process provides bindings to access wasm files through JavaScript as opposed to inlining it; which would take care of your bullet for efficiency.

Their workflow creates only essential files for npm (package.json and details for building/dependencies). I could see there being issues with asset bundlers though if an application were to import the wasm file directly (import wasm from 'library/src/library.wasm'). But a bit of googling turns up plugins for all of the bundlers you've listed. I'm not sure if you would want to go as far as add those plugins within the library itself or what the right approach would be. So I'm not as sure on your second bullet here.

like image 69
GenericUser Avatar answered Nov 16 '22 03:11

GenericUser