Skip to content

Commit

Permalink
Merge pull request #15984 from Snuffleupagus/optional-workerModules
Browse files Browse the repository at this point in the history
Allow, optionally, using worker-modules during local development
  • Loading branch information
Snuffleupagus authored Jan 31, 2023
2 parents 3d35b48 + 5e88228 commit 2d87a2e
Show file tree
Hide file tree
Showing 4 changed files with 19 additions and 8 deletions.
3 changes: 1 addition & 2 deletions docs/contents/getting_started/index.md
Original file line number Diff line number Diff line change
Expand Up @@ -108,8 +108,7 @@ Note that we only mention the most relevant files and folders.
│   ├── display/ - display layer
│   ├── shared/ - shared code between the core and display layers
│   ├── interfaces.js - interface definitions for the core/display layers
│   ├── pdf.*.js - wrapper files for bundling
│   └── worker_loader.js - used for developer builds to load worker files
│   └── pdf.*.js - wrapper files for bundling
├── test/ - unit, font, reference, and integration tests
├── web/ - viewer layer
├── LICENSE
Expand Down
9 changes: 7 additions & 2 deletions src/display/api.js
Original file line number Diff line number Diff line change
Expand Up @@ -2101,7 +2101,12 @@ class PDFWorker {

// Some versions of FF can't create a worker on localhost, see:
// https://bugzilla.mozilla.org/show_bug.cgi?id=683280
const worker = new Worker(workerSrc);
const worker =
(typeof PDFJSDev === "undefined" || !PDFJSDev.test("PRODUCTION")) &&
!workerSrc.endsWith("/build/pdf.worker.js") &&
!workerSrc.endsWith("/src/worker_loader.js")
? new Worker(workerSrc, { type: "module" })
: new Worker(workerSrc);
const messageHandler = new MessageHandler("main", "worker", worker);
const terminateEarly = () => {
worker.removeEventListener("error", onWorkerError);
Expand Down Expand Up @@ -2289,7 +2294,7 @@ class PDFWorker {
return mainWorkerMessageHandler;
}
if (typeof PDFJSDev === "undefined" || !PDFJSDev.test("PRODUCTION")) {
const worker = await import("pdfjs/core/worker.js");
const worker = await import("pdfjs/pdf.worker.js");
return worker.WorkerMessageHandler;
}
if (
Expand Down
6 changes: 4 additions & 2 deletions src/pdf.worker.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,10 @@
import { WorkerMessageHandler } from "./core/worker.js";

/* eslint-disable-next-line no-unused-vars */
const pdfjsVersion = PDFJSDev.eval("BUNDLE_VERSION");
const pdfjsVersion =
typeof PDFJSDev !== "undefined" ? PDFJSDev.eval("BUNDLE_VERSION") : void 0;
/* eslint-disable-next-line no-unused-vars */
const pdfjsBuild = PDFJSDev.eval("BUNDLE_BUILD");
const pdfjsBuild =
typeof PDFJSDev !== "undefined" ? PDFJSDev.eval("BUNDLE_BUILD") : void 0;

export { WorkerMessageHandler };
9 changes: 7 additions & 2 deletions web/app.js
Original file line number Diff line number Diff line change
Expand Up @@ -304,7 +304,12 @@ const PDFViewerApplication = {
const { mainContainer, viewerContainer } = this.appConfig,
params = parseQueryString(hash);

if (params.get("disableworker") === "true") {
if (
(typeof PDFJSDev === "undefined" || !PDFJSDev.test("PRODUCTION")) &&
params.get("workermodules") === "true"
) {
AppOptions.set("workerSrc", "../src/pdf.worker.js");
} else if (params.get("disableworker") === "true") {
try {
await loadFakeWorker();
} catch (ex) {
Expand Down Expand Up @@ -2143,7 +2148,7 @@ async function loadFakeWorker() {
GlobalWorkerOptions.workerSrc ||= AppOptions.get("workerSrc");

if (typeof PDFJSDev === "undefined" || !PDFJSDev.test("PRODUCTION")) {
window.pdfjsWorker = await import("pdfjs/core/worker.js");
window.pdfjsWorker = await import("pdfjs/pdf.worker.js");
return;
}
await loadScript(PDFWorker.workerSrc);
Expand Down

0 comments on commit 2d87a2e

Please sign in to comment.