FredKSchott / pika-pack-builders

🏗 A collection of official & community @pika/pack build plugins
MIT License
162 stars 31 forks source link

Bundling web workers #15

Open matthewp opened 5 years ago

matthewp commented 5 years ago

I have a package that uses a web worker. The code looks something like this:

src/index.js

let url = new URL('./worker.js', import.meta.url).toString();
const worker = new Worker(url);

When I run a build this index.js file is built into the dist-web folder, but the worker.js is not.

I don't think pika needs to automatically detect these workers or anything, but there needs to be a way to specify a second entry point so that worker.js can be built and placed alongside src/index.js.

FredKSchott commented 5 years ago

+1, I also wonder if bundling each distribution should be toggleable, so that dist-web/ would have all individual files in it and this code would work that way. I like the idea of bundling being the default, but agreed that this should be supported.

For web workers specifically, I'm curious how this would work with tooling. From the little that I do know (having never used them myself) don't they have their own special things going on like the global importScripts function? I wonder if Rollup even supports that today?

matthewp commented 5 years ago

How would you see this as being configured?

FredKSchott commented 5 years ago

‘“bundled”: true/false’ option for each of these build-* plugins, maybe? Defaulting to true so that it’s optional

matthewp commented 5 years ago

Wait, I think I'm confused now :) I'm not suggesting that anything not be bundled... I definite want things to be bundled. I just want to be able to specify a separate entry point, so that ['src/index.js', 'src/worker.js'] both of those files are bundled separate and put into their own files in dist-web. Does that make sense?

pocesar commented 5 years ago

workers can be loaded using Blob / createObjectURL, so that's the best bet. https://gist.github.com/nolanlawson/23eff93d27ad09ff44b7e4d56ffd1d54

webpack does this with a loader (of course) https://github.com/webpack-contrib/worker-loader#inline

matthewp commented 5 years ago

That method includes the worker's source in the main script. All we need here is a way to specify that there are multiple entry points. rollup should then write out both files to dist-web and the references will already be correct.