| 1 | # rollup-plugin-off-main-thread
|
|---|
| 2 |
|
|---|
| 3 | Use Rollup with workers and ES6 modules _today_.
|
|---|
| 4 |
|
|---|
| 5 | ```
|
|---|
| 6 | $ npm install --save @surma/rollup-plugin-off-main-thread
|
|---|
| 7 | ```
|
|---|
| 8 |
|
|---|
| 9 | Workers 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 |
|
|---|
| 11 | This plugin takes care of shimming module support in workers and allows you to use `new Worker()`.
|
|---|
| 12 |
|
|---|
| 13 | OMT is the result of merging loadz0r and workz0r.
|
|---|
| 14 |
|
|---|
| 15 | ## Usage
|
|---|
| 16 |
|
|---|
| 17 | I set up [a gist] to show a full setup with OMT.
|
|---|
| 18 |
|
|---|
| 19 | ### Config
|
|---|
| 20 |
|
|---|
| 21 | ```js
|
|---|
| 22 | // rollup.config.js
|
|---|
| 23 | import OMT from "@surma/rollup-plugin-off-main-thread";
|
|---|
| 24 |
|
|---|
| 25 | export 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 |
|
|---|
| 40 | In your project's code use a module-relative path via `new URL` to include a Worker:
|
|---|
| 41 |
|
|---|
| 42 | ```js
|
|---|
| 43 | const worker = new Worker(new URL("worker.js", import.meta.url), {
|
|---|
| 44 | type: "module"
|
|---|
| 45 | });
|
|---|
| 46 | ```
|
|---|
| 47 |
|
|---|
| 48 | This will just work.
|
|---|
| 49 |
|
|---|
| 50 | If required, the plugin also supports plain literal paths:
|
|---|
| 51 |
|
|---|
| 52 | ```js
|
|---|
| 53 | const worker = new Worker("./worker.js", { type: "module" });
|
|---|
| 54 | ```
|
|---|
| 55 |
|
|---|
| 56 | However, those are less portable: in Rollup they would result in module-relative
|
|---|
| 57 | path, but if used directly in the browser, they'll be relative to the document
|
|---|
| 58 | URL instead.
|
|---|
| 59 |
|
|---|
| 60 | Hence, they're deprecated and `new URL` pattern is encouraged instead for portability.
|
|---|
| 61 |
|
|---|
| 62 | ### Importing workers as URLs
|
|---|
| 63 |
|
|---|
| 64 | 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:
|
|---|
| 65 |
|
|---|
| 66 | ```js
|
|---|
| 67 | import workerURL from "omt:./worker.js";
|
|---|
| 68 | import paintWorkletURL from "omt:./paint-worklet.js";
|
|---|
| 69 |
|
|---|
| 70 | const worker = new Worker(workerURL, { name: "main-worker" });
|
|---|
| 71 | CSS.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 |
|
|---|
| 97 | License Apache-2.0
|
|---|