Allow, optionally, using worker-modules during local development

Until PR 12563 is deemed safe to land, I'd still like to be able to use worker-modules in the viewer during local development.
Hence this patch which *temporarily* adds a new `workerModules` hash-parameter, only available in non-PRODUCTION mode, that allows using worker-modules in the development viewer.

To enable this functionality, simply use http://localhost:8888/web/viewer.html#workerModules=true
This commit is contained in:
Jonas Jenwald 2023-01-31 12:00:15 +01:00
parent 3d35b48f46
commit 5e88228767
4 changed files with 19 additions and 8 deletions

View File

@ -108,8 +108,7 @@ Note that we only mention the most relevant files and folders.
│   ├── display/ - display layer │   ├── display/ - display layer
│   ├── shared/ - shared code between the core and display layers │   ├── shared/ - shared code between the core and display layers
│   ├── interfaces.js - interface definitions for the core/display layers │   ├── interfaces.js - interface definitions for the core/display layers
│   ├── pdf.*.js - wrapper files for bundling │   └── pdf.*.js - wrapper files for bundling
│   └── worker_loader.js - used for developer builds to load worker files
├── test/ - unit, font, reference, and integration tests ├── test/ - unit, font, reference, and integration tests
├── web/ - viewer layer ├── web/ - viewer layer
├── LICENSE ├── LICENSE

View File

@ -2101,7 +2101,12 @@ class PDFWorker {
// Some versions of FF can't create a worker on localhost, see: // Some versions of FF can't create a worker on localhost, see:
// https://bugzilla.mozilla.org/show_bug.cgi?id=683280 // 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 messageHandler = new MessageHandler("main", "worker", worker);
const terminateEarly = () => { const terminateEarly = () => {
worker.removeEventListener("error", onWorkerError); worker.removeEventListener("error", onWorkerError);
@ -2289,7 +2294,7 @@ class PDFWorker {
return mainWorkerMessageHandler; return mainWorkerMessageHandler;
} }
if (typeof PDFJSDev === "undefined" || !PDFJSDev.test("PRODUCTION")) { 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; return worker.WorkerMessageHandler;
} }
if ( if (

6
src/pdf.worker.js vendored
View File

@ -16,8 +16,10 @@
import { WorkerMessageHandler } from "./core/worker.js"; import { WorkerMessageHandler } from "./core/worker.js";
/* eslint-disable-next-line no-unused-vars */ /* 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 */ /* 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 }; export { WorkerMessageHandler };

View File

@ -304,7 +304,12 @@ const PDFViewerApplication = {
const { mainContainer, viewerContainer } = this.appConfig, const { mainContainer, viewerContainer } = this.appConfig,
params = parseQueryString(hash); 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 { try {
await loadFakeWorker(); await loadFakeWorker();
} catch (ex) { } catch (ex) {
@ -2143,7 +2148,7 @@ async function loadFakeWorker() {
GlobalWorkerOptions.workerSrc ||= AppOptions.get("workerSrc"); GlobalWorkerOptions.workerSrc ||= AppOptions.get("workerSrc");
if (typeof PDFJSDev === "undefined" || !PDFJSDev.test("PRODUCTION")) { if (typeof PDFJSDev === "undefined" || !PDFJSDev.test("PRODUCTION")) {
window.pdfjsWorker = await import("pdfjs/core/worker.js"); window.pdfjsWorker = await import("pdfjs/pdf.worker.js");
return; return;
} }
await loadScript(PDFWorker.workerSrc); await loadScript(PDFWorker.workerSrc);