Print more warnings about potential problems in Node.js environments

- Warn if the "regular" PDF.js build is used in Node.js environments, since that won't load any of the relevant polyfills.

 - Warn if the `require` function cannot be accessed, since currently we're just "swallowing" any errors.
This commit is contained in:
Jonas Jenwald 2024-12-07 14:01:00 +01:00
parent b870c5d2ad
commit c60a6d1ebd

View File

@ -26,43 +26,45 @@ if (typeof PDFJSDev !== "undefined" && PDFJSDev.test("MOZCENTRAL")) {
); );
} }
if ( if (isNodeJS) {
typeof PDFJSDev !== "undefined" && if (typeof PDFJSDev === "undefined" || PDFJSDev.test("SKIP_BABEL")) {
!PDFJSDev.test("SKIP_BABEL") && warn("Please use the `legacy` build in Node.js environments.");
isNodeJS } else {
) { let canvas;
let canvas;
try {
const require = process
.getBuiltinModule("module")
.createRequire(import.meta.url);
try { try {
canvas = require("@napi-rs/canvas"); const require = process
} catch (ex) { .getBuiltinModule("module")
warn(`Cannot load "@napi-rs/canvas" package: "${ex}".`); .createRequire(import.meta.url);
}
} catch {}
if (!globalThis.DOMMatrix) { try {
if (canvas?.DOMMatrix) { canvas = require("@napi-rs/canvas");
globalThis.DOMMatrix = canvas.DOMMatrix; } catch (ex) {
} else { warn(`Cannot load "@napi-rs/canvas" package: "${ex}".`);
warn("Cannot polyfill `DOMMatrix`, rendering may be broken."); }
} catch (ex) {
warn(`Cannot access the \`require\` function: "${ex}".`);
} }
}
if (!globalThis.ImageData) { if (!globalThis.DOMMatrix) {
if (canvas?.ImageData) { if (canvas?.DOMMatrix) {
globalThis.ImageData = canvas.ImageData; globalThis.DOMMatrix = canvas.DOMMatrix;
} else { } else {
warn("Cannot polyfill `ImageData`, rendering may be broken."); warn("Cannot polyfill `DOMMatrix`, rendering may be broken.");
}
} }
} if (!globalThis.ImageData) {
if (!globalThis.Path2D) { if (canvas?.ImageData) {
if (canvas?.Path2D) { globalThis.ImageData = canvas.ImageData;
globalThis.Path2D = canvas.Path2D; } else {
} else { warn("Cannot polyfill `ImageData`, rendering may be broken.");
warn("Cannot polyfill `Path2D`, rendering may be broken."); }
}
if (!globalThis.Path2D) {
if (canvas?.Path2D) {
globalThis.Path2D = canvas.Path2D;
} else {
warn("Cannot polyfill `Path2D`, rendering may be broken.");
}
} }
} }
} }