source: frontend/node_modules/@surma/rollup-plugin-off-main-thread/README.md

Last change on this file was 9af201e, checked in by MBK <marija.karapandzova@…>, 12 days ago

Fix frontend appearance

  • Property mode set to 100644
File size: 3.0 KB
Line 
1# rollup-plugin-off-main-thread
2
3Use Rollup with workers and ES6 modules _today_.
4
5```
6$ npm install --save @surma/rollup-plugin-off-main-thread
7```
8
9Workers are JavaScript’s version of threads. [Workers are important to use][when workers] as the main thread is already overloaded, especially on slower or older devices.
10
11This plugin takes care of shimming module support in workers and allows you to use `new Worker()`.
12
13OMT is the result of merging loadz0r and workz0r.
14
15## Usage
16
17I set up [a gist] to show a full setup with OMT.
18
19### Config
20
21```js
22// rollup.config.js
23import OMT from "@surma/rollup-plugin-off-main-thread";
24
25export default {
26 input: ["src/main.js"],
27 output: {
28 dir: "dist",
29 // You _must_ use either “amd” or “esm” as your format.
30 // But note that only very few browsers have native support for
31 // modules in workers.
32 format: "amd"
33 },
34 plugins: [OMT()]
35};
36```
37
38### Auto bundling
39
40In your project's code use a module-relative path via `new URL` to include a Worker:
41
42```js
43const worker = new Worker(new URL("worker.js", import.meta.url), {
44 type: "module"
45});
46```
47
48This will just work.
49
50If required, the plugin also supports plain literal paths:
51
52```js
53const worker = new Worker("./worker.js", { type: "module" });
54```
55
56However, those are less portable: in Rollup they would result in module-relative
57path, but if used directly in the browser, they'll be relative to the document
58URL instead.
59
60Hence, they're deprecated and `new URL` pattern is encouraged instead for portability.
61
62### Importing workers as URLs
63
64If 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:
65
66```js
67import workerURL from "omt:./worker.js";
68import paintWorkletURL from "omt:./paint-worklet.js";
69
70const worker = new Worker(workerURL, { name: "main-worker" });
71CSS.paintWorklet.addModule(paintWorkletURL);
72```
73
74`./worker.js` and `./paint-worklet.js` will be added to the output as chunks.
75
76## Options
77
78```js
79{
80 // ...
81 plugins: [OMT(options)];
82}
83```
84
85- `loader`: A string containing the EJS template for the amd loader. If `undefined`, OMT will use `loader.ejs`.
86- `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_.
87- `workerRegexp`: A RegExp to find `new Workers()` calls. The second capture group _must_ capture the provided file name without the quotes.
88- `amdFunctionName`: Function name to use instead of AMD’s `define`.
89- `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.
90- `urlLoaderScheme`: Scheme to use when importing workers as URLs. If `undefined`, OMT will use `"omt"`.
91
92[when workers]: https://dassur.ma/things/when-workers
93[a gist]: https://gist.github.com/surma/a02db7b53eb3e7870bf539b906ff6ff6
94
95---
96
97License Apache-2.0
Note: See TracBrowser for help on using the repository browser.