Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Web Workers - How To Import Modules

I am using ES2015 Import / Export modules.

In my worker file, when I try to import functions like I normally do:

worker.js

import { a, b, c } from "./abc.js";

I get the error: SyntaxError: import declarations may only appear at top level of a module

As I am exporting functions in my module 'abc.js', I am not sure how to use them using the old (& apparently on its way out) syntax:

self.importScripts( "/app/abc.js" );

So, my question is, how do we use the new import module syntax with workers?

Second question is, what does importScripts import into when it imports from a module in where is there is no global object parent being exported?

like image 448
Kayote Avatar asked May 22 '17 17:05

Kayote


People also ask

How do I import a module into a service worker?

ES modules can be imported in one of two ways: either statically, using the import ... from '...' syntax, or dynamically, using the import() method. Inside of a service worker, only the static syntax is currently supported. This limitation is analogous to a similar restriction placed on importScripts() usage.

How do I import a module into a browser?

Safari, Chrome, Firefox and Edge all support the ES6 Modules import syntax. Here's what they look like. Simply add type="module" to your script tags and the browser will load them as ES Modules. The browser will follow all import paths, downloading and executing each module only once.

Can web workers fetch?

Comlink-fetch allows you to use Fetch in a web worker that is exposed through Comlink.

What is importScripts?

importScripts() The importScripts() method of the WorkerGlobalScope interface synchronously imports one or more scripts into the worker's scope.


3 Answers

ES2015 modules in Workers are available in Safari and in Chromium browsers.

If other browsers / versions are your target, you still need to use importScripts().

When available, you create a module-worker like this:

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

See: https://html.spec.whatwg.org/#module-worker-example

These are the bug-reports for each browser:

  • Firefox: In development (almost done) 🛑 please vote!
  • Chromium Browsers:
    Dedicated Workers: Available since version 80 ✔️
    Shared Workers: Available since version 83 ✔️
    Service Workers: Available since version 91 ✔️
  • Webkit:
    Safari Desktop: Available since Safari 14.1 ✔️
    Safari Mobile (iOS): Available since Safari 15 ✔️
like image 61
Tobias Buschor Avatar answered Oct 19 '22 17:10

Tobias Buschor


2020:

Chrome 80 has shipped module workers in February 2020 (and Chrome 82 will ship modules for shared workers). Firefox/Safari do not support those features for now: tests

You may want to use import-from-worker lib to do the heavy lifting for you (for now, you need to check the support for modules in workers and do the fallback by yourself).

like image 43
jakub.g Avatar answered Oct 19 '22 15:10

jakub.g


ES Modules in workers are already available in Chrome, enabling Experimental Web Platform Features, using the proper flag when launching chrome:

chrome.exe --enable-experimental-web-platform-features

This is the syntax you need to use to load the worker script as a module :

new Worker( 'my-worker.js', { type : 'module' } );

This feature has been in development for almost ayear, and should be available soon, without the need of special flags, however, there is no official release date yet.

like image 3
colxi Avatar answered Oct 19 '22 15:10

colxi