Important: This documentation covers Yarn 1 (Classic).
For Yarn 2+ docs and migration guide, see yarnpkg.com.

Package detail

rollup-plugin-worker-factory

brandonocasey747Apache-2.00.5.7

Bundle web workers that work in nodejs and the browser, without a separate build target.

rollup, rollup-plugin

readme

Install

npm i -D rollup-plugin-worker-factory

Requirements

  • browsers with web worker support (tested in ie 11)
  • nodejs >= 10 when node is run with --experimental-workers
  • natively in nodejs >= 12 without any arguments to node

Usage

include 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));
});

How it works

  1. We use rollup to rollup all dependencies for your web worker file into a bundle.
  2. We wrap that rollup in a function so that it can be minified and included normally in your code without doing anything.
  3. We return a web worker constructor for your function that will run in the web worker with slight modifications to unify the API (nothing that you, the user should have to worky about).

Modifications to node worker_threads

  • We mirror on to addEventListener on worker objects
  • We mirror off to removeEventListener on worker objects
  • We wrap postMessage 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.

Modifictaion to browser WebWorkers

  • We automagically create a blob url of your code to run in the worker that will be revoked on worker terminate
  • We mirror addEventListener to on on worker objects
  • We mirror removeEventListener to on on worker objects
  • We add the created blob url to the worker as an objURL property

Options

plugins

An array of rollup plugins to bundle the web workers with, you will probably want:

type

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

changelog

0.5.7 (2021-07-02)

Bug Fixes

  • mock worker dispatchEvent should use onmessage, add type_ debugging aide (3afd219)

0.5.6 (2021-06-24)

Bug Fixes

  • slight parentPort API change workaround for node >= 14 (31d3902)

0.5.5 (2021-04-09)

Bug Fixes

  • support dispatchEvent on mock worker. Rename FakeWorker to MockWorker (0050391)

0.5.4 (2021-03-11)

Bug Fixes

0.5.3 (2021-03-11)

Bug Fixes

  • mock worker coverage support (9ebfea6)

0.5.2 (2021-03-11)

Bug Fixes

  • mock worker terminate cleanup (d4a264b)

0.5.1 (2021-02-17)

Bug Fixes

  • make sure get-worker-string can be transcoded (f5d732c)

0.5.0 (2021-02-16)

Bug Fixes

  • should work with minifiers and platforms without workers (4685d39)

0.4.1 (2021-01-25)

Bug Fixes

  • do not eval mock workers, prevents break point issues (63fe865)

0.4.0 (2021-01-25)

Features

  • support mock workers, document type option (b1596c6)

0.3.0 (2020-12-18)

Features

  • support nodee, browser, or universal workers via a type option (831a205)

0.2.0 (2020-12-17)

Features

  • comments before and after, better rollup config (53e6d3f)

Bug Fixes

  • trycatch worker_threads require and warn on error (fd0d5de)

0.1.0 (2020-12-10)

Features

6.1.0 (2020-12-09)

Bug Fixes

6.0.2 (2020-10-22)

Bug Fixes

  • prevent warning on module build (33f6c6c)

6.0.1 (2020-10-19)

Bug Fixes

  • minifier plugin, babel-config update (#36) (da2ae25)

6.0.0 (2020-10-05)

Features

Chores

BREAKING CHANGES

  • requires rollup@^2

5.0.2 (2020-03-24)

Chores

  • package: update to babel 7.9 and enable bugfixes (71c71db)

5.0.1 (2019-08-16)

Bug Fixes

5.0.0 (2019-08-16)

Features

  • BREAKING: Use [@babel](https://github.com/babel)/plugin-transform-runtime to turn babel helpers into requires (#25) (9a64e3d)

4.0.0 (2019-07-15)

Features

Bug Fixes

  • Make defaults immutable, so watch does not continuously change them (#23) (3113a40)

Chores

  • package: update node-resolve and commonjs (#20) (5237b77)

Performance Improvements

  • Faster builds by combining similar builds, and using env vars (#22) (7d83aea)

BREAKING CHANGES

  • externals are now merged with globals before being passed to the user externals function.
  • istanbul is removed from plugin list before use function on --watch or coverage false
  • defaults are now immutable as intended.
  • minBrowser is now part of browser and both module builds are under the same name
  • package: require rollup ^1.12.0

3.2.1 (2019-06-28)

Bug Fixes

  • check if istanbul is in plugins first before removing (#19) (6b3bffc)

3.2.0 (2019-04-24)

Features

3.1.1 (2019-04-24)

Bug Fixes

  • more strict rollup watch check regex (#18) (01de5a2)

3.1.0 (2019-03-06)

Features

Bug Fixes

  • security: fix security issues by updating package-lock.json (7bbf448)

Chores

3.0.2 (2019-01-08)

Documentation

  • update readme about rollup support (79310bd)

Reverts

  • the previous terser fix and update to a new version (#14) (8db61e6), closes #12

3.0.1 (2019-01-08)

Bug Fixes

3.0.0 (2019-01-08)

Features

BREAKING CHANGES

  • This is considered a breaking change because it is not expected to work with pre-1.0 versions of Rollup.

2.3.1 (2018-10-18)

Bug Fixes

  • Merge excludeCoverage option creating istanbul plugin (#9) (451dac9)

2.3.0 (2018-10-11)

Features

2.2.0 (2018-09-06)

Features

  • Allow babel customization through a babel option (#5) (a98daac)

Chores

2.1.0 (2018-08-30)

Features

2.0.1 (2018-08-28)

Bug Fixes

  • rollup-plugin-istanbul must be a dependency (fb5419b)

2.0.0 (2018-08-28)

Features

Chores

BREAKING CHANGES

  • babel package names have changed
  • Adds a dependency and options.

1.0.0 (2018-08-17)

Features

Chores

  • add travis, greenkeeper, changelog, and toc tools (0905caa)