wasm-bindgen icon indicating copy to clipboard operation
wasm-bindgen copied to clipboard

Importing npm modules in JavaScript snippets

Open maxcountryman opened this issue 3 years ago • 6 comments

I'm curious if there are examples of importing packages within the JavaScript snippet that'll be bound via wasm-bindgen?

For example, say I have a JavaScript snippet which imports lodash and makes use of it in a function I later expose with wasm_bindgen in the hopes of using it in my Rust code. While this will happily compile, in the browser I'll see some error such as: TypeError: Module specifier, 'lodash' does not start with "/", "./", or "../". when I go to actually run this.

Is there a way to import packages like this into my scripts? If not, if I wanted to use lodash in my Rust code, is there some way of doing that?

maxcountryman avatar Jan 18 '22 00:01 maxcountryman

There were historical plans of handling this but they never came to fruition, so for now this won't work really automatically per-se but you'll need to ensure that whichever NPM package eventually builds the wasm or bundles it will have lodash (or equivalent) as a dependency.

alexcrichton avatar Jan 18 '22 15:01 alexcrichton

...you'll need to ensure that whichever NPM package eventually builds the wasm or bundles it will have lodash (or equivalent) as a dependency.

I think what you mean here is that the wasm would be used by some npm package; in my case, I'm not doing that. I'm using the wasm directly in the browser (i.e. built via Yew).

I've been able to pull in some things with unpkg.com, but this doesn't work for everything and seems like a bit of a hack. I wonder if I could have a separate npm + webpack build pipeline that created a build with e.g. lodash, load that via the script tag (a la unpkg), and then reference the exports in my JavaScript snippet?

It seems like a convoluted path to interoperation. :)

maxcountryman avatar Jan 18 '22 15:01 maxcountryman

Er sorry basically this is unimplemented and the story here isn't great. I don't really know what could easily be done to make it better without larger reworks.

alexcrichton avatar Jan 18 '22 20:01 alexcrichton

hi @maxcountryman, I have the same problem as you. did you discover a workaround or attempt the workaround you described?

I wonder if I could have a separate npm + webpack build pipeline that created a build with e.g. lodash, load that via the script tag (a la unpkg), and then reference the exports in my JavaScript snippet?

mhuesch avatar Jul 21 '22 17:07 mhuesch

hi @maxcountryman, I have the same problem as you. did you discover a workaround or attempt the workaround you described?

I can't remember the exact details of what I was doing when I ran into this now, but I think I was able to sequence my browser imports such that I did manage to eventually get it to work in practice.

maxcountryman avatar Jul 21 '22 18:07 maxcountryman

@maxcountryman got it, thx :pray: I am fumbling around tho think I am progressing towards something workable.

mhuesch avatar Jul 21 '22 18:07 mhuesch