From ec0ab91a2b8b11d95e8000a6d2db33afd9239221 Mon Sep 17 00:00:00 2001 From: Jonas Jenwald Date: Thu, 14 May 2020 15:55:11 +0200 Subject: [PATCH] Reduce the usage of `require` statements in code-paths not protected by pre-processor and/or run-time checks This replaces some additional `require`/`exports` usage with standard `import`/`export` statements instead. Hence another, small, part in the effort to reduce the reliance on SystemJS-specific functionality in the development viewer. --- src/core/worker.js | 2 +- src/display/api_compatibility.js | 7 ++++--- src/shared/compatibility.js | 17 ++++++++++------- web/viewer_compatibility.js | 3 ++- 4 files changed, 17 insertions(+), 12 deletions(-) diff --git a/src/core/worker.js b/src/core/worker.js index c50aa248c..91a70db63 100644 --- a/src/core/worker.js +++ b/src/core/worker.js @@ -678,7 +678,7 @@ function isMessagePort(maybePort) { ); } -// Worker thread (and not node.js)? +// Worker thread (and not Node.js)? if ( typeof window === "undefined" && !isNodeJS && diff --git a/src/display/api_compatibility.js b/src/display/api_compatibility.js index 810cf90b6..01c185b99 100644 --- a/src/display/api_compatibility.js +++ b/src/display/api_compatibility.js @@ -13,10 +13,10 @@ * limitations under the License. */ +import { isNodeJS } from "../shared/is_node.js"; + const compatibilityParams = Object.create(null); if (typeof PDFJSDev === "undefined" || PDFJSDev.test("GENERIC")) { - const { isNodeJS } = require("../shared/is_node.js"); - const userAgent = (typeof navigator !== "undefined" && navigator.userAgent) || ""; const isIE = /Trident/.test(userAgent); @@ -41,5 +41,6 @@ if (typeof PDFJSDev === "undefined" || PDFJSDev.test("GENERIC")) { } })(); } +const apiCompatibilityParams = Object.freeze(compatibilityParams); -exports.apiCompatibilityParams = Object.freeze(compatibilityParams); +export { apiCompatibilityParams }; diff --git a/src/shared/compatibility.js b/src/shared/compatibility.js index 6c3c6e50a..f3d208899 100644 --- a/src/shared/compatibility.js +++ b/src/shared/compatibility.js @@ -14,6 +14,8 @@ */ /* eslint no-var: error */ +import { isNodeJS } from "./is_node.js"; + // Skip compatibility checks for modern builds and if we already ran the module. if ( (typeof PDFJSDev === "undefined" || !PDFJSDev.test("SKIP_BABEL")) && @@ -27,8 +29,6 @@ if ( } globalThis._pdfjsCompatibilityChecked = true; - const { isNodeJS } = require("./is_node.js"); - const hasDOM = typeof window === "object" && typeof document === "object"; const userAgent = (typeof navigator !== "undefined" && navigator.userAgent) || ""; @@ -248,15 +248,18 @@ if ( // Support: IE (function checkURL() { - if (typeof PDFJSDev !== "undefined" && PDFJSDev.test("IMAGE_DECODERS")) { - // The current image decoders don't use the `URL` constructor, so it - // doesn't need to be polyfilled for the IMAGE_DECODERS build target. + if (typeof PDFJSDev === "undefined" || !PDFJSDev.test("PRODUCTION")) { + // Prevent "require is not a function" errors in development mode, + // since the `URL` constructor should be available in modern browers. return; - } - if (typeof PDFJSDev !== "undefined" && !PDFJSDev.test("GENERIC")) { + } else if (!PDFJSDev.test("GENERIC")) { // The `URL` constructor is assumed to be available in the extension // builds. return; + } else if (PDFJSDev.test("IMAGE_DECODERS")) { + // The current image decoders don't use the `URL` constructor, so it + // doesn't need to be polyfilled for the IMAGE_DECODERS build target. + return; } globalThis.URL = require("core-js/web/url.js"); })(); diff --git a/web/viewer_compatibility.js b/web/viewer_compatibility.js index c0c169d32..db8421a79 100644 --- a/web/viewer_compatibility.js +++ b/web/viewer_compatibility.js @@ -35,5 +35,6 @@ if (typeof PDFJSDev === "undefined" || PDFJSDev.test("GENERIC")) { } })(); } +const viewerCompatibilityParams = Object.freeze(compatibilityParams); -exports.viewerCompatibilityParams = Object.freeze(compatibilityParams); +export { viewerCompatibilityParams };