• Stars
    star
    305
  • Rank 136,879 (Top 3 %)
  • Language
    JavaScript
  • License
    Apache License 2.0
  • Created over 5 years ago
  • Updated 5 months ago

Reviews

There are no reviews yet. Be the first to send feedback to the community and the maintainers!

Repository Details

Use Rollup with workers and ES6 modules today.

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 use a module-relative path via new URL to include a Worker:

const worker = new Worker(new URL("worker.js", import.meta.url), {
  type: "module"
});

This will just work.

If required, the plugin also supports plain literal paths:

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

However, those are less portable: in Rollup they would result in module-relative path, but if used directly in the browser, they'll be relative to the document URL instead.

Hence, they're deprecated and new URL pattern is encouraged instead for portability.

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

More Repositories

1

tinderforbananas.com

It’s like Tinder, but for 🍌
JavaScript
369
star
2

gobox

Something like busybox in pure Go
Go
238
star
3

underdash

Collection of JS snippets for collection manipulation
JavaScript
227
star
4

use-workerized-reducer

Like useReducer, but runs in a worker.
JavaScript
226
star
5

jsxx

Rust
169
star
6

observables-with-streams

A collection of observables built with streams.
TypeScript
133
star
7

ishoudinireadyyet.com

Tracker for Houdini APIs in different browsers
HTML
119
star
8

rollup-plugin-loadz0r

An ill-named rollup plugin that makes code splitting “just work”, even with workers.
JavaScript
98
star
9

jxl-art

Web app to make art using JPEG XL.
JavaScript
93
star
10

wasmphobia

Rust
91
star
11

rollup-plugin-assemblyscript

A Rollup plugin that allows you to import AssemblyScript files and compiles them on-the-fly.
JavaScript
74
star
12

httptools

Augmenting the basic net/http package with functionality found in web frameworks without breaking the original API.
Go
57
star
13

surma.dev

Personal Homepage
JavaScript
48
star
14

rollup-plugin-comlink

Use workers seamlessly with Rollup
JavaScript
38
star
15

s3put

Push a number of folders to a S3 bucket
Go
31
star
16

gopin

Tool-less version pinning for Go - surpassed by gopkg.in
Go
31
star
17

dof-tool

A web app to calculate your depth of field when doing photography.
JavaScript
30
star
18

silly-wat-linker

A tool that makes writing WebAssembly Text files easier.
Rust
28
star
19

streaming-dot

doT-based streaming templating engine for Node and the web
JavaScript
27
star
20

as-inliner

Inlines files into your AssemblyScript
JavaScript
26
star
21

rollup-plugin-workz0r

An ill-named rollup plugin that adds worker bundling support
JavaScript
26
star
22

bfwasm

A non-optimizing Brainf_ck to WebAssembly compiler
JavaScript
25
star
23

stacksignal

Make a go program print a stacktrace of all goroutines on SIGUSR1
Go
22
star
24

spreadsheet

TypeScript
18
star
25

rollup-plugin-wasm-esm

JavaScript
15
star
26

rollup-plugin-entrypoint-hashmanifest

A rollup plugin that generates a hash manifest for each entry point.
JavaScript
13
star
27

silly-alloc

A collection of very basic allocators. Written with WebAssembly in mind.
Rust
13
star
28

gocpio

A Go package for cpio archives
Go
13
star
29

webscribble

A simple, no-fluff web scratchpad.
JavaScript
7
star
30

miniqoi

A decoder for QOI in hand-written WebAssembly
WebAssembly
7
star
31

http2-push-detect

Lists HTTP/2 pushes when requesting an URL
JavaScript
7
star
32

rm2cal

A simple calendar/TODO list for Remarkable 2.
JavaScript
6
star
33

peasant

TypeScript
6
star
34

polymer-reddit-api

A simple element wrapping Reddit’s JSON API for my talk at the Polymer Summit 2015 in Amsterdam.
HTML
5
star
35

lurkk.it

TypeScript
4
star
36

platinum-god

JavaScript
3
star
37

emsettings

HTML
3
star
38

letterboxer

JavaScript
3
star
39

bikeshed-docker

Dockerfile for Bikeshed
3
star
40

javascript-pong

My very first JavaScript project from 2004
2
star
41

importalias

Go
2
star
42

osci

Emulator and tools for osci, a one-instruction set CPU derivatived from subleq
Rust
2
star
43

eatoutandstaysane

A web app to remove any mention of calories from food menu PDFs.
JavaScript
2
star
44

xmos-virtualport

Virtual ports to emulate arbitrary-sized GPIO ports on the XMOS architecture
C
1
star
45

shaderscribble

A simple, no-fluff shader scratchpad.
JavaScript
1
star
46

diplomaenhancer

Daemon to manipulate your local host file to block webpages which you consider distracting
JavaScript
1
star