pdf.js/examples/webpack
Alex Plumley 3b9031f6a3 Fix pdfjs-dist/webpack causing errors with certain configs
Using `require.resolve("worker-loader")` to check if `worker-loader` is installed causes webpack to include `worker-loader` in the output bundle, which is not the intended effect. Aside from increasing the bundle size unnecessarily, it also causes errors for webpack configs with targets that don't have node's built-in modules.

These errors can be fixed by configuring webpack `externals` to exclude `worker-loader`, but it's more difficult to figure out this solution than to figure out that `worker-loader` needs to be installed (even without this explicit error message).

To solve this, the explicit check for `worker-loader` has been removed. An alternative solution would be to use webpack's `resolveWeak`. Documentation has also been added in `examples/webpack` to help users.
2020-06-03 14:50:41 -04:00
..
.eslintrc Manually fix remaining ESLint errors 2018-12-11 15:23:26 +01:00
.gitignore Adds/modifies examples for node.js and webpack. 2015-12-21 13:46:50 -06:00
index.html Removing "entry-loader" dependency from webpack. 2016-04-13 08:24:25 -05:00
main.js Update Prettier to version 2.0 2020-04-14 12:28:14 +02:00
package.json Update versions in webpack example 2018-11-12 11:15:17 +01:00
README.md Fix pdfjs-dist/webpack causing errors with certain configs 2020-06-03 14:50:41 -04:00
webpack.config.js Enable auto-formatting of the entire code-base using Prettier (issue 11444) 2019-12-26 12:34:24 +01:00

Overview

Example to demonstrate PDF.js library usage with Webpack.

Getting started

Install the example dependencies and build the project:

$ gulp dist-install
$ cd examples/webpack
$ npm install
$ ./node_modules/webpack/bin/webpack.js

You can observe the build results by running gulp server and navigating to http://localhost:8888/examples/webpack/index.html.

Refer to the main.js and webpack.config.js files for the source code. Note that PDF.js packaging requires packaging of the main application and the worker code, and the workerSrc path shall be set to the latter file.

Worker loading

If you are getting the Setting up fake worker warning, make sure you are importing pdfjs-dist/webpack which is the zero-configuration method for Webpack users. You will need to install worker-loader as a dependency in your project in order to use pdfjs-dist/webpack (configuring worker-loader is not necessary; just installing it is sufficient).

import pdfjsLib from 'pdfjs-dist/webpack';

For a full working example refer to this repository.