After migrating an application from Webpack 4 and worker-loader
to Webpack 5, I'm now using Webpacks built-in solution for importing Web Worker as worker-loader is no longer supported in Webpack 5.
The solution provided by Webpack works, except when I use local module imports inside my Workers. Just to be clear:
Following the Webpack documentation I import the Worker like so:
const worker = new Worker( new URL( "path/to/worker.js", import.meta.url ));
And an example of a Worker:
import { convenientFunction } from "./local/and/also/used/in/main/app-bundle";
self.addEventListener( "message", e => {
// whatever Worker should do
});
When I bundle the app and load the Worker I get the following error in the console:
1.js:604 Uncaught DOMException: Failed to execute 'importScripts' on 'WorkerGlobalScope': The script at 'http://localhost:8080/js/js/node_modules_core-js_modules_es_error_cause_js.js' failed to load.
Which disappears when I remove the import. For the record I can import libraries from node_modules just fine, but for some reason it looks like local files also used by the main application bundle outside of the Worker lead to this error.
I feel like I'm overlooking something very obvious but from the spartan documentation on Workers I'm unsure where to look.