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 should handle loading worker instead of setting workerSrc

See original GitHub issue

When following the Webpack example and importing pdfjs-dist with import * as pdfjsLib from 'pdfjs-dist'; Webpack will create a pdfjsWorker.js and also automatically load it in the browser. The file may be named differently(hashed names, prefixes, etc.)

Still pdf.js requires to set a absolute path: pdfjsLib.GlobalWorkerOptions.workerSrc = 'pdfjsWorker.js';

This will let Webpack load the worker, and then pdf.js will also load the worker itself. Why is that the case? Instead we could just create a Worker and let Webpack do the loading?

Also i am having heavy trouble setting the workerSrc: The filename might be pdfjsWorker.js in development, but in production it has hashes and differential loading prefixes. I could use an external worker.js but then the worker(1.5mb) will be loaded twice and should not be included in Webpack bundling.

Issue Analytics

  • State:closed
  • Created 4 years ago
  • Reactions:18
  • Comments:23 (4 by maintainers)

github_iconTop GitHub Comments

4reactions
timvandermeijcommented, Aug 23, 2020

Do you see a way I could amend the …/…/build/webpack/pdf.worker.bundle.js path to make it usable in my case?

That path is indeed only valid for the example itself when the steps from the README at https://github.com/mozilla/pdf.js/blob/master/examples/webpack/README.md are followed. The gulp dist-install line makes that work.

If you use pdfjs-dist you don’t need that since the required Webpack bits are distributed along with it as outlined in https://github.com/mozilla/pdf.js/blob/master/examples/webpack/README.md#worker-loading. Looking at that in more detail, you indeed shouldn’t have to set the workerSrc at all because the zero-configuration Webpack file already does that for you; see https://github.com/mozilla/pdfjs-dist/blob/master/webpack.js#L27-L31 (this is distributed in pdfjs-dist).

3reactions
morgan4080commented, Sep 4, 2021

Here is how I imported everything pdfjs and pdfjsworker

import * as pdfjsLib from ‘pdfjs-dist’; import { pdfjsWorker } from ‘pdfjs-dist/webpack’

Read more comments on GitHub >

github_iconTop Results From Across the Web

Looking for help to make npm/pdfjs-dist work with Webpack ...
Which is attempting to load pdf.worker.js (which worker-loader should be packaging) and then tries to instantiate the class: pdfjs.
Read more >
worker-loader - webpack - JS.ORG
webpack is a module bundler. Its main purpose is to bundle JavaScript files for usage in a browser, yet it is also capable...
Read more >
CSP: worker-src - HTTP - MDN Web Docs
The HTTP Content-Security-Policy (CSP) worker-src directive specifies valid sources for Worker, SharedWorker, or ServiceWorker scripts.
Read more >
New WebKit Features in Safari 15.5
The worker-src directive provides web developers a way to restrict which URLs are allowed to be sources for worker scripts (Worker, SharedWorker ......
Read more >
Using Content Security Policy (CSP) to Secure Web Applications
In Level 3, frame-src and worker-src directives can be used instead to control embedded content and worker processes respectively.
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