-
Notifications
You must be signed in to change notification settings - Fork 55
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
How can I use an ES6 module in a worker? #4
Comments
To create a Web Worker and being able to use ESM add a 2nd argument with new Worker(url, { type: 'module '}) |
You can load an ES module directly in a Worker if browser compatibility is not an issue for you, but the point of this FAQ item is that currently bundling is required to work across all WASM-capable browsers (i.e. including Firefox) and this shows a workaround for a bundling problem. |
This comment was marked as resolved.
This comment was marked as resolved.
This comment was marked as outdated.
This comment was marked as outdated.
This comment was marked as outdated.
This comment was marked as outdated.
If anyone comes here trying to put
And at the very least, it seems to work for me. As mentioned above, an empty string probably also works. |
The wa-sqlite build produces ES6 modules, which is nice for development. But most applications should put time-consuming operations in a Worker, and support for ES6 modules in a Worker isn't universal yet. Worker code generally needs to be bundled.
Emscripten modules need to load their associated .wasm file, and the generated code for ES6 uses
import.meta.url
as its default location.import.meta.url
is not valid outside an ES6 module, however, so the bundler has to translate this to something else. When the target context is a Worker, that something else needs to be valid in a Worker, so something likedocument.currentScript.src
, which is the substitution my bundler chooses, causes a runtime exception.The fix I use is to configure a bundler string replacement plug-in (@rollup/plugin-replace in my case) to replace
import.meta.url
with an empty string. There may be better approaches out there but this is sufficient for me so far.The text was updated successfully, but these errors were encountered: