I use pdfjs-dist, from version 2 to 3 they changed their webpack integration used to load their worker, and use the new Worker()
syntax that Webpack 5 is supposed to support. Before they used worker-loader and it worked fine, now it doesn't.
The contents of the library looks like this:
pdfjs.GlobalWorkerOptions.workerPort = new Worker(
new URL("./build/pdf.worker.js", import.meta.url)
);
Now, I tried this with hand-made files too in my own project, with the same result (so the library is not at fault), the problem being:
Uncaught DOMException: Failed to construct 'Worker': Script at 'file:///myprojectpath/node_modules/pdfjs-dist/build/pdf.worker.js' cannot be accessed from origin 'http://localhost:9003'.
Upon further examination it appears to me that Webpack doesn't bundle or transform anything. For example import.meta.url
is an absolute local path to the file loading the worker, and no worker chunk is emitted either.
I use Angular and I suspect it may be to blame here since they seem to have a custom way to handle webworkers. But I don't know if that's right or why.
The only similar question (non-Angular) I found is this : How do you use Web Workers with Webpack 5?
But logging the Webpack configuration produced by Angular shows that my output.publicPath
value is ''
which therefore seems right according to the aforementioned post.
So does anybody knows why Webpack ignores my new Worker
constructs and if it's an Angular thing?