Webpack should handle loading worker instead of setting workerSrc
See original GitHub issueWhen 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:
- Created 4 years ago
- Reactions:18
- Comments:23 (4 by maintainers)
Top GitHub Comments
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 theworkerSrc
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 inpdfjs-dist
).Here is how I imported everything pdfjs and pdfjsworker
import * as pdfjsLib from ‘pdfjs-dist’; import { pdfjsWorker } from ‘pdfjs-dist/webpack’