I've been using Web Workers for a while, but only now have I found the need to import a module into them. Upon attempting this, I was somewhat shocked to find that this is not yet possible in Firefox Dev 76. Having reviewed this solution to the problem, I am somewhat at a loss. Here's what I'm doing:
I have a worker that needs to import a NodeJS module that works in the browser. This worker is generated from a given string of code. So, inside the generated worker, I'm running this:
//worker
let { foo } = importScripts("http://localhost:3000/some-blob-url");
Which imports anything else code successfully. I'm also, upon detecting an import in the given code string, running this code to generate another URL with the import code that exports the part of the module I want.
let blobURL = URL.createObjectURL(new Blob([importingCode], { type: 'application/javascript' }));
Where importingCode
is:
import { foo } from 'module';
export { foo };
However, upon importing the Blob URL in worker.js
, I am told that imports can only occur at the top-level of a module, implying that this created Blob is not a module (importingCode).
How would I initialise createObjectURL
, given a Blob of code, to be a module that supports imports. I am aware that the answer I linked to uses Webpack require
statements, but I would like to avoid Webpack here. Is this possible to do?
Sorry if my question is vague, please tell me and I'm happy to clarify.