PepsRyuu/nollup

SharedWorker doesn't work

Closed this issue · 5 comments

I tried using nollup to build a sharedworker (using an array of configs in rollup.config.js). Nollup wraps the code in eval and overwrites default variables that are required to function properly. Is there a way I can either tell nollup to not wrap it in eval (I don't need HMR for the shared worker), or disable nollup completely for it (so it just bundles normally like rollup does) while it still works for the normal site?

I tried using both globalThis and self with same results. this can't be used as typescript thinks it's undefined (and this can't be declared like globalThis or self)?

image
image

Do you have a minimal reproducible example?

Let me create one quickly

Here it is: https://github.com/danbulant/nollup-bug

Simply run npm start, go to chrome://inspect/#workers and look at the message (weirdly enough, in this minimal example source maps do work but not in the routify app, but source maps aren't needed that much. It's possible that it's just because I use typescript there).

Thanks for the example.

The example provided is not using SharedWorker correctly. SharedWorkerGlobalScope doesn't expose a .port property. Remember that a SharedWorker is shared across several tabs, so a singular port property doesn't work. Instead, you have to listen for connections to the SharedWorker.

self.onconnect = function (e) {
    let port = e.ports[0];
    
    port.addEventListener('message', (...args) => {
        console.log('message', ...args);
    })
    
    port.start();
}

This works for me.

Oh I didn't know that. I was just trying to make a minimal sharedworker to buildon based on docs from mozilla. In hindsight, it makes sense that there needs to be onconnect handler.

Thanks!