Inline web worker in H5WasmLocalFileProvider
#1614
Merged
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
I faced a bundling issue with the worker when I tried to use
H5WasmLocalFileProvider
in myHDF5. The worker was compiled as an asset atdist/assets/worker.<hash>.mjs
, and imported from the main bundle withnew Worker('/assets/worker.<hash>.mjs', ...)
. Obviously, when importing@h5web/h5wasm
fromnode_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 thennew LocalWorker()
). I had to configure Vite to build the worker independently and add anexports
entry to @h5web/h5wasm'spackage.json
. I also had to add aworker
prop toH5WasmLocalFileProvider
. 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 bothH5WasmProvider
andH5WasmLocalFileProvider
are imported. We'll have to wait for Vite to better support thenew 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.