GoogleChromeLabs / worker-plugin

👩‍🏭 Adds native Web Worker bundling support to Webpack.
https://npm.im/worker-plugin
Apache License 2.0
1.92k stars 79 forks source link

[question] Support service workers? #7

Open gpoitch opened 6 years ago

gpoitch commented 6 years ago

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?

developit commented 6 years ago

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.

gpoitch commented 6 years ago

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.

developit commented 6 years ago

yup yup.