question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Webpack instructions still cause 'fake worker' to load

See original GitHub issue

Configuration:

  • Web browser and its version: Chrome 52
  • Operating system and its version: OS X Yosemite
  • PDF.js version: 1.4.237
  • Is an extension: No

Steps to reproduce the problem: My code is:

import pdflib from 'pdfjs-dist'
pdflib.PDFJS.workerSrc = './node_modules/pdfjs-dist/build/pdf.worker.entry.js'

exactly as described in https://github.com/mozilla/pdf.js/wiki/Setup-pdf.js-in-a-website#with-webpack, yet I get a Warning: Setting up fake worker.' in my console when I actually load a document, which makes it seem like the instructions did not work.

Additionally the wording on the instructions seems wrong as “PDFJS.workerSrc shall be set to point to this file” (the current wording) means that it’s automatically set, whereas “PDFJS.workerSrc should be set to point to this file” means you have to set it yourself. The example code also isn’t extremely helpful as it uses the relative paths into the repository (pdfjsLib.PDFJS.workerSrc = '../../build/webpack/pdf.worker.bundle.js';) that a person importing PDFJS would not be able to do.

I’m also confused as I searched through issues/PRs that are < 1 year old like https://github.com/mozilla/pdf.js/pull/6595 that do some automatic loading of the worker script, but that code seems to no longer exist in the repo, so both setting and not setting the workerSrc cause the fake worker to load for me… The fake worker knows where to find the worker script built by webpack (e.g. 1.bundle.js is the worker when bundle.js is the script), so I’m confused why a real worker couldn’t use this logic as well. I’ve tried pointing workerSrc to the 1.bundle.js file created and even using webpack’s worker-loader to instantiate and replace PDFWorker (pdflib.PDFJS.PDFWorker = require('worker!pdfjs-dist/build/pdf.worker.entry.js')), but that didn’t work either, so I’m completely lost as to how the worker is supposed to work with webpack

Issue Analytics

  • State:closed
  • Created 7 years ago
  • Reactions:8
  • Comments:32 (11 by maintainers)

github_iconTop GitHub Comments

35reactions
ctowlercommented, Nov 7, 2016

I ran into the same issue with my webpack project, but I solved it differently. Instead of relying on webpack’s bundling or loaders, I made use of the CopyWebpackPlugin to copy the worker source into my build directory.

In my viewer:

import pdfjsLib from 'pdfjs-dist';

if (process.env.NODE_ENV !== 'production') {
   //in dev, get it from the node_modules directory
   //NOTE: don't use the "entry" file -- the script will fail and the web worker will not be used
   pdfjsLib.PDFJS.workerSrc = `${process.env.APP_ROOT}/node_modules/pdfjs-dist/build/pdf.worker.js`;
} else {
   //in prod, get it from the build directory
   pdfjsLib.PDFJS.workerSrc = `${process.env.APP_ROOT}/build/pdf.worker.js`;
}

In webpack.config.js:

const CopyWebpackPlugin = require('copy-webpack-plugin');

return {
   //... rest of config omitted
   plugins: [{
      new CopyWebpackPlugin([{
         from: 'node_modules/pdfjs-dist/build/pdf.worker.js',
         to: 'pdf.worker.js'
      }])
   }]
}
18reactions
yurydelendikcommented, Jul 13, 2017

@wojtekmaj we added pdfjs-dist/webpack for zero-configuration for webpack users. You can see its usage at https://github.com/yurydelendik/pdfjs-react

Read more comments on GitHub >

github_iconTop Results From Across the Web

worker-loader - webpack - JS.ORG
This scenario can commonly occur if you are hosting your assets under a CDN domain. Even downloads from the webpack-dev-server could be blocked....
Read more >
Worker Script Failing to Load for Vue Webpack Built App
I have a small problem still: I am deploying my app fromy /dist via couchdb, so basically all of the urls need to...
Read more >
How I solved and debugged my Webpack issue through trial ...
I tried to use webpack.SourceMapDevToolPlugin but it didn't work with my setup, even when I deleted devtools or set them to false; I...
Read more >
Webpack 5 Adoption - Next.js
We've spent a lot of effort into ensuring the transition from webpack 4 to 5 will be as smooth as possible. Your application...
Read more >
worker-loader - webpack 3 documentation
Note: Inline mode will also create chunks for browsers without support for inline workers, to disable this behavior just set fallback parameter as...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found