trunk-rs / trunk

Build, bundle & ship your Rust WASM application to the web.

Home Page:https://trunkrs.dev/

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

`data-bindgen-target` not working for worker type

SAdams601 opened this issue · comments

Hello I'm building a webapp in yew and serving it with trunk.

My app has a few NPM dependencies which I'm bundling using this method with esbuild. This works great but now I'm trying to add a webworker so I defined an additional data-bin with the worker data-type. However when I tried to serve the app I get the following error:

NPM dependencies have been specified in '<dir>/package.json' but this is incompatible with the 'no-modules' target

Reading the assets docs made me think that I should include data-bindgen-target="web" in the worker's link tag. However when I serve with verbose logging on I see that wasm-bindgen is still being called with --target=no-modules for the worker.

To be clear the main app is the only place that depends on NPM backages the worker code is only rust so I don't need those dependencies available in the worker bin.

Any help solving this would be much appreciated.

It's hard to tell without a reproducer. I would recommend taking a look at the two existing webworker examples.

I have looked at the webworker examples but none of them include NPM dependencies. I will work on making a reproducer.

A good way to include JS dependencies seems like this pattern (not my idea, just using that too): https://github.com/ctron/popper-rs/tree/main/popper-rs-sys

Here's a minimal example of the issue I'm dealing with: https://github.com/SAdams601/webworker_npm_replicator

bonnie run generates the appropriate src/package.js file and starts trunk. This logs the error I mention in my initial post.

I'm not sure if this setup is something trunk you want trunk to support but I do find it useful to manage some NPM dependencies as part of my project directly rather than in adjacent crates.

Thanks for providing the reproducer. There's a bunch of things wrong with. Unfortunately, I don't have the time to fix it up.

Just a few quick pointers:

  • You're including the JS both through a script tag, as well as through wasm_bindgen, that won't work as these are two distinct modules later on
  • JS imported through wasm-bindgen must be a JS module, yours seems to the common js, so that also won't work

My recommendation would be:

  • If you're creating custom JS code, create it as a ESM (JS module), that can be used right away
  • If you're importing other JS code, calling it from Rust: turn it into an ESM. rollup works fine for this in many cases. that's what the example I provided uses. You might also be able to get there using esbuild, but I don't know.
  • if you want to call into JS, wasm_bindgen attributes alone are enough, there's no need to adding a script tag

I am sorry I can't help more right now.

I am sorry I can't help more right now.

Don't worry about it! Thank you for the tips and the link to your example project I'll be able figure it out.