trunk icon indicating copy to clipboard operation
trunk copied to clipboard

Set an environment variable with the JS file that needs to be loaded for workers

Open ranile opened this issue 2 years ago • 5 comments

Trunk supports web workers as assets and can generate a loader shim:

<link data-trunk rel="rust" href="Cargo.toml" data-bin="worker" data-type="worker" data-weak-refs data-loader-shim />

When using these workers, it is mandatory to know the path. For example, with gloo-worker:

let mut bridge = MarkdownWorker::spawner().spawn_with_loader("/example_markdown_worker_loader.js");

This can be solved by trunk if an environment variable is set which names the loader file. That would make the loading:

let mut bridge = MarkdownWorker::spawner().spawn_with_loader(env!("WORKER_LOADER"));

The name of the environment variable will be inferred by the asset configuration. It will also be possible to override the environment variable name

ranile avatar Aug 29 '23 16:08 ranile

This issue is stale because it has been open 30 days with no activity. Remove stale label or comment or this will be closed in 5 days.

github-actions[bot] avatar Sep 29 '23 00:09 github-actions[bot]

This issue was closed because it has been stalled for 5 days with no activity.

github-actions[bot] avatar Oct 04 '23 00:10 github-actions[bot]

Can this be reopened?

ranile avatar Oct 04 '23 15:10 ranile

Example project: https://github.com/rustwasm/gloo/tree/master/examples/markdown

ctron avatar Dec 07 '23 13:12 ctron

I've started to take a look at this. Here's a thing:

  • The env-var value (and name) is required during the compilation of the wasm code
  • If the env-var value should be hashed (see #405) it's hash value would need to be known upfront

This would mean two things: a) it cannot be processes in parallel (which it might likely can't anyway because of cargo build locks) and b) there must be an order of imports (worker first, then main), which might get tricky and problematic if there's a circular dependency situation (not sure that worth supporting).

We would resolve this by having fixed shim names, and then fetching with no-cache, as suggested here: https://github.com/trunk-rs/trunk/issues/405#issuecomment-1237042106

That would of course mean that shim loaders would because a requirement for solving #405.

ctron avatar Dec 18 '23 12:12 ctron