Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Inline web worker in H5WasmLocalFileProvider #1614

Merged
merged 1 commit into from
Apr 18, 2024
Merged

Inline web worker in H5WasmLocalFileProvider #1614

merged 1 commit into from
Apr 18, 2024

Conversation

axelboc
Copy link
Contributor

@axelboc axelboc commented Apr 17, 2024

I faced a bundling issue with the worker when I tried to use H5WasmLocalFileProvider in myHDF5. The worker was compiled as an asset at dist/assets/worker.<hash>.mjs, and imported from the main bundle with new Worker('/assets/worker.<hash>.mjs', ...). Obviously, when importing @h5web/h5wasm from node_modules in myHDF5, this absolute path was problematic...

I found a workaround by letting the consumer instantiate the worker (import LocalWorker from '@h5wasm/h5wasm/worker' and then new LocalWorker()). I had to configure Vite to build the worker independently and add an exports entry to @h5web/h5wasm's package.json. I also had to add a worker prop to H5WasmLocalFileProvider. As I said, it worked, but obviously this added complexity for consumers, which I wasn't happy with...

So instead I decided to just inline the worker into the main bundle of the @h5web/h5wasm package to avoid generating that extra dist/assets/worker.<hash>.mjs module in the first place. At least consumers don't have to do anything for the worker to work 🥁.

Unfortunately, with both these workarounds, the h5wasm package ends up duplicated in the consumer app's bundle if both H5WasmProvider and H5WasmLocalFileProvider are imported. We'll have to wait for Vite to better support the new Worker() syntax in library mode, I guess. Not great, but I'll try to mitigate this issue by importing the providers lazily/dynamically in myHDF5.

@axelboc axelboc merged commit bd8d836 into main Apr 18, 2024
8 checks passed
@axelboc axelboc deleted the inline-worker branch April 18, 2024 07:05
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants