worker-plugin icon indicating copy to clipboard operation
worker-plugin copied to clipboard

[question] Support service workers?

Open gpoitch opened this issue 7 years ago • 3 comments

I know this is specific to web workers, however I think this could be adapted to work for service workers. I was looking to create a separate plugin but the code would be mostly the same.

Looks like it scans the AST for a keyword (Worker) and generates an entry for the referenced file.

// web worker
new Worker('./foo.js', { type: 'module' })

// service worker
navigator.serviceWorker.register('./foo.js', { type: 'module' })

Could it be adapted to be more generic or am I missing something specific this does for web workers?

gpoitch avatar Oct 01 '18 17:10 gpoitch

That's a great idea! It's definitely an option. I wonder if there's a larger question here of "script entry points" - *Worklet addModule() would be another that would fit nicely into here.

One tricky bit is actually ensuring .register() is invoked on a real instance of ServiceWorkerContainer, versus just a local variable of that name. Perhaps that's a reason to test out only supporting navigator.serviceWorker.register() and not other module entry points, since it's a pretty specific name that is unlikely to be miscategorized.

developit avatar Oct 08 '18 19:10 developit

Great, good to know! I'd love a plugin (or for webpack proper) to support generating assets for any kind of script url APIs. Figured your name worker-plugin could get away with at least adding service workers too.

Good point about variablizing. I guess the current setup would also fail if someone did const W = Worker; new W('./foo') but that wouldn't be as likely. Could just be a note in the readme about it.

gpoitch avatar Oct 08 '18 20:10 gpoitch

yup yup.

developit avatar Oct 09 '18 02:10 developit