Reputation: 30576
Web worker scripts are loaded when a Worker
is instantiated but how can I use a script that is a module so I don't get an error? (assume the browser supports modules).
const worker = new Worker('my-worker.js')
In my-worker.js
import {foo} from 'foo.js'
console.log(foo)
Upvotes: 2
Views: 2890
Reputation: 53119
Use the type
option:
const worker = new Worker('my-worker.js', {type:"module"});
DOMString
specifying the type of worker to create. The value can be classic
or module
. If not specified, the default used is classic
.Upvotes: 8