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 · 6 comments
Closed

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

rhashimoto opened this issue May 8, 2021 · 6 comments
Labels
faq Frequently asked question

Comments

@rhashimoto
Copy link
Owner

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 Frequently asked question label May 8, 2021
@jimmywarting
Copy link

To create a Web Worker and being able to use ESM add a 2nd argument with { type: 'module' }

new Worker(url, { type: 'module '})

@rhashimoto
Copy link
Owner Author

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.

@jimmywarting

This comment was marked as resolved.

@pauleveritt

This comment was marked as outdated.

@rhashimoto

This comment was marked as outdated.

@AntonOfTheWoods
Copy link

AntonOfTheWoods commented Jul 22, 2023

If anyone comes here trying to put wa-sqlite in a service worker (which as of this posting still has terrible support for modules...) and is using vite-plugin-pwa, then you can put something like this in your VitePWAOptions config:

import replace from "@rollup/plugin-replace";
...
  injectManifest: {
    ...
    // @ts-ignore - this is a valid option somehow... and is required for wa-sqlite to work
    plugins: [replace({ "import.meta.url": mode === "development" ? "import.meta.url" : "undefined" })],
  },

And at the very least, it seems to work for me. As mentioned above, an empty string probably also works.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
faq Frequently asked question
Projects
None yet
Development

No branches or pull requests

4 participants