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

How can I use an ES6 module in a worker? #4

Closed
rhashimoto opened this issue May 8, 2021 · 0 comments
Closed

How can I use an ES6 module in a worker? #4

rhashimoto opened this issue May 8, 2021 · 0 comments
Labels
faq

Comments

@rhashimoto
Copy link
Owner

@rhashimoto rhashimoto commented May 8, 2021

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 like document.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.

@rhashimoto rhashimoto added the faq label May 8, 2021
@rhashimoto rhashimoto closed this May 8, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
faq
Projects
None yet
Development

No branches or pull requests

1 participant