brandonocasey/rollup-plugin-worker-factory(新しいタブで開きます)
主要言語: JavaScript
Bundle web workers that work in nodejs and the browser, without a separate build target.
- star 数
- 2
- watcher 数
- 1
- fork 数
- 0
- issue 数
- 0
- 最終更新
- 2021/07/03
主要言語: JavaScript
Bundle web workers that work in nodejs and the browser, without a separate build target.
npm i -D rollup-plugin-worker-factory
--experimental-workersinclude rollup-plugin-worker-factory in your rollup plugins array for a build:
const worker = require('rollup-plugin-worker-factory'); ... plugins: [ worker() ] ...
When you want to include a worker in your code prefix the file with worker!:
import SomeWorker from "worker!./src/some-worker.js";
Your worker should run without exporting any code and use the global self variable to interact with the worker API. worker_threads.parentPort for node has been slightly modified so that their API more closely matches web workers in the browser and will also be available on the global self.
const someDependency = require('../some-dep'); const whoKnows = require('who-knows'); self.addEventListener('message', function(e) { const foo = someDependency(e); self.postMessage(whoKnows(foo)); });
on to addEventListener on worker objectsoff to removeEventListener on worker objectspostMessage so that the message sent is included as a value for a data key on an object as this is how the browser works with postMessage.addEventListener to on on worker objectsremoveEventListener to on on worker objectsobjURL propertyAn array of rollup plugins to bundle the web workers with, you will probably want:
Default: 'universal'
The type of factory to build, can be 'node', 'mock', 'browser', or 'universal'.
browser will only work in the browser
node will only work in node
mock will work everywhere but runs on the same thread
universal will work on the browser or node depending on available apis