Pdf.js: How to embed pdf.js into another module intended for use in webpack projects?

Created on 1 Dec 2018  路  5Comments  路  Source: mozilla/pdf.js

I created a simple module for converting PDFs to image data URLs within React apps here:

https://github.com/marcaaron/react-pdf-to-image

If I import this into a webpack project e.g. create-react-app and then build / serve this project all goes well. But in development mode I receive the following error message in JS console.

bootstrap:1 Uncaught ReferenceError: window is not defined

And webpack dev server complains about this:

./node_modules/pdfjs-dist/build/pdf.js Critical dependency: require function is used in a way in which dependencies cannot be statically extracted

Steps to reproduce the problem:

  1. Install package as dependency
  2. Attempt to use in a create-react-app project

What is the expected behavior?
There would be some sort of way to add pdf.js with a worker as a dependency for other projects in development out of the box. Is this a limitation of web workers in general? Or can changes be made to the current distribution to allow this to work in all projects?

1-other

Most helpful comment

I believe this is not an issue with nesting in the package, but instead with the requireEnsure config being turned off in create-react-app.

See https://github.com/mozilla/pdf.js/issues/10253. Unfortunately the only way to adjust this config currently is to eject from CRA. We have our own config where we turned this off. I wonder if PDFjs could support dynamic imports instead going forward as they are now a standard.

All 5 comments

I believe this is not an issue with nesting in the package, but instead with the requireEnsure config being turned off in create-react-app.

See https://github.com/mozilla/pdf.js/issues/10253. Unfortunately the only way to adjust this config currently is to eject from CRA. We have our own config where we turned this off. I wonder if PDFjs could support dynamic imports instead going forward as they are now a standard.

Ah I see... Well, thanks @pk-nb! I did see some discussion over on the react repo for potentially adding web workers, but not sure if this ever panned out or would improve things for pdfjs and CRA users.

So is it correct to say that you must eject from CRA in order for your project to use PDFJS?

/cc @timvandermeij Given the example added in PR #11220, and that the Critical dependency: ... warnings should have been removed in PR #11418, should this issue be closed now?

Yes, indeed.

Was this page helpful?
0 / 5 - 0 ratings

Related issues

Snuffleupagus picture Snuffleupagus  路  28Comments

timvandermeij picture timvandermeij  路  28Comments

AliND picture AliND  路  29Comments

kaymes picture kaymes  路  62Comments

snorp picture snorp  路  95Comments