Irina Glushko 2389e7160b HW1 done преди 3 години
..
tests 2389e7160b HW1 done преди 3 години
.travis.yml 2389e7160b HW1 done преди 3 години
CODEOWNERS 2389e7160b HW1 done преди 3 години
CONTRIBUTING 2389e7160b HW1 done преди 3 години
CONTRIBUTORS 2389e7160b HW1 done преди 3 години
Dockerfile 2389e7160b HW1 done преди 3 години
LICENSE 2389e7160b HW1 done преди 3 години
README.md 2389e7160b HW1 done преди 3 години
index.js 2389e7160b HW1 done преди 3 години
karma.conf.js 2389e7160b HW1 done преди 3 години
loader.ejs 2389e7160b HW1 done преди 3 години
package.json 2389e7160b HW1 done преди 3 години
renovate.json 2389e7160b HW1 done преди 3 години
run_tests.js 2389e7160b HW1 done преди 3 години

README.md

rollup-plugin-off-main-thread

Use Rollup with workers and ES6 modules today.

$ npm install --save @surma/rollup-plugin-off-main-thread

Workers are JavaScript’s version of threads. Workers are important to use as the main thread is already overloaded, especially on slower or older devices.

This plugin takes care of shimming module support in workers and allows you to use new Worker().

OMT is the result of merging loadz0r and workz0r.

Usage

I set up a gist to show a full setup with OMT.

Config

// rollup.config.js
import OMT from "@surma/rollup-plugin-off-main-thread";

export default {
  input: ["src/main.js"],
  output: {
    dir: "dist",
    // You _must_ use either “amd” or “esm” as your format.
    // But note that only very few browsers have native support for
    // modules in workers.
    format: "amd"
  },
  plugins: [OMT()]
};

Auto bundling

In your project's code:

const worker = new Worker("./worker.js", { type: "module" });

This will just work.

Importing workers as URLs

If your worker constructor doesn't match workerRegexp (see options below), you might find it easier to import the worker as a URL. In your project's code:

import workerURL from "omt:./worker.js";
import paintWorkletURL from "omt:./paint-worklet.js";

const worker = new Worker(workerURL, { name: "main-worker" });
CSS.paintWorklet.addModule(paintWorkletURL);

./worker.js and ./paint-worklet.js will be added to the output as chunks.

Options

{
  // ...
  plugins: [OMT(options)];
}
  • loader: A string containing the EJS template for the amd loader. If undefined, OMT will use loader.ejs.
  • useEval: Use fetch() + eval() to load dependencies instead of <script> tags and importScripts(). This is not CSP compliant, but is required if you want to use dynamic imports in ServiceWorker.
  • workerRegexp: A RegExp to find new Workers() calls. The second capture group must capture the provided file name without the quotes.
  • amdFunctionName: Function name to use instead of AMD’s define.
  • prependLoader: A function that determines whether the loader code should be prepended to a certain chunk. Should return true if the load is suppsoed to be prepended.
  • urlLoaderScheme: Scheme to use when importing workers as URLs. If undefined, OMT will use "omt".

License Apache-2.0