Support Web Workers coming from node_modules
See original GitHub issueChoose one: is this a 🐛 bug report or 🙋 feature request?
Both, I guess?
🤔 Expected Behavior
- node_modules should be also checked when looking for a file
😯 Current Behavior
- trying to refer to a Service Worker file from node_modules fails with the following error:
× C:\Users\wojtekmaj\Projekty\react-pdf\sample\parcel\node_modules\react-pdf\build\entry.parcel.js: Cannot resolve dependency './pdfjs-dist\build\pdf.worker.js' (...)
what it is trying to do is look for the path in ./
but not in node_modules
.
🔦 Context
💻 Code Sample
I’m currently working on support for Parcel in React-PDF. That requires me to handle loading a service worker which is not mine.
In Webpack, the following code does the job:
var pdfjs = require('react-pdf/build/pdf.js');
var PdfjsWorker = require('worker-loader!react-pdf/build/pdf.worker.js');
if (typeof window !== 'undefined' && 'Worker' in window) {
pdfjs.PDFJS.workerPort = new PdfjsWorker();
}
I was trying to reproduce the same using Parcel, and here’s what I got so far:
const pdfjs = require('pdfjs-dist');
if (typeof window !== 'undefined' && 'Worker' in window) {
pdfjs.PDFJS.workerPort = new Worker('pdfjs-dist/build/pdf.worker.js');
}
This results in an error described below. Changing the line to
pdfjs.PDFJS.workerPort = new Worker('../node_modules/pdfjs-dist/build/pdf.worker.js');
stops the error, but:
- I’m not comfortable with relative path in a package like that
- This caused… a 404 error! Parcel starts referring to a file it did not produced.
🌍 Your Environment
Software | Version(s) |
---|---|
Parcel | 1.5.0 |
Node | 9.3.0 |
npm/Yarn | npm, newest |
Operating System | Windows 10 |
Issue Analytics
- State:
- Created 6 years ago
- Reactions:9
- Comments:11 (6 by maintainers)
Top Results From Across the Web
How to use a web-worker from an npm library into main ...
If I use this in my main project and modify the web workers in the dist output from this: new URL("my.worker-8e6a623b.js", ({ url: ......
Read more >Using Web Workers - Web APIs - MDN Web Docs
Web Workers are a simple means for web content to run scripts in background threads. The worker thread can perform tasks without interfering ......
Read more >Seamless web workers & worker threads - threads.js
This code will run in any node.js version that comes with worker threads support, so node 12+ or node 10+ with a feature...
Read more >Supporting Web Workers API in Node.js vs Just Using Deno
Node.js already provides support for this in the form of Worker Threads, but because it's a different implementation than the browser API, ...
Read more >Multithreading in JavaScript with Web Workers - LeanyLabs
Web workers are handy for doing long computations in the background. Typically, where everything runs in one thread, those computations would block everything ......
Read more >
Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free
Top Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
@mischnic yep. Had to reject using that library
@devongovett Inspired by the new resolver:
"~/node_modules/pdfjs-dist/build/pdf.worker.js"
for “[…/]node_modules/pdfjs-dist/build/pdf.worker.js”"lib/worker.js"
="./lib/worker.js"
for relative paths