Using react-pdf with Parcel 2 the "right" way

Viewed 255

So I've just upgraded my project to Parcel 2 and I encountered some issues with react-pdf (https://github.com/wojtekmaj/react-pdf) which I've fixed. My question is actually what's the "right" way to have done.

So what I did was the following...

In entry.parcel.js I changed from:

if (typeof window !== 'undefined' && 'Worker' in window) {
  pdfjs.GlobalWorkerOptions.workerPort = new Worker('./pdf.worker.entry.js');
}

to Parcel 2's new Worker syntax

if (typeof window !== 'undefined' && 'Worker' in window) {
  pdfjs.GlobalWorkerOptions.workerPort = new Worker(
    new URL('./pdf.worker.entry.js', import.meta.url),
    { type: 'module' },
  );
}

For the moment that change is in my own fork of react-pdf which I pull in via tarball from github.

Additionally certain imports expected that the /dist folder in ./node_modules/react-pdf would exist and it didn't. I added a script in my package.json:

"react-pdf-build": "cd ./node_modules/react-pdf && yarn && yarn build && cd ../../"

and added it to my yarn build command.

So my question remains: is the the "right" way to have done this? if not, please advise what is. Thanks so much!

0 Answers
Related