Remove most build-time require-calls from the src/display/-folder

By leveraging import maps we can get rid of *most* of the remaining `require`-calls in the `src/display/`-folder, since we should strive to use modern `import`-statements wherever possible.
The only remaining cases are Node.js-specific dependencies, since those seem very difficult to convert unless we start producing a bundle *specifically* for Node.js environments.
This commit is contained in:
Jonas Jenwald 2023-07-13 11:58:16 +02:00
parent e81c084a92
commit d022912719
11 changed files with 122 additions and 54 deletions

View File

@ -42,7 +42,7 @@
"import/no-mutable-exports": "error",
"import/no-self-import": "error",
"import/no-unresolved": ["error", {
"ignore": ["pdfjs", "pdfjs-lib", "pdfjs-web", "web"]
"ignore": ["display", "pdfjs", "pdfjs-lib", "pdfjs-web", "web"]
}],
"mozilla/avoid-removeChild": "error",
"mozilla/use-includes-instead-of-indexOf": "error",

View File

@ -239,6 +239,14 @@ function createWebpackConfig(
"pdfjs-web": "web",
"pdfjs-lib": "web/pdfjs",
};
const libraryAlias = {
"display-fetch_stream": "src/display/stubs.js",
"display-l10n_utils": "src/display/stubs.js",
"display-network": "src/display/stubs.js",
"display-node_stream": "src/display/stubs.js",
"display-node_utils": "src/display/stubs.js",
"display-svg": "src/display/stubs.js",
};
const viewerAlias = {
"web-annotation_editor_params": "web/annotation_editor_params.js",
"web-com": "",
@ -256,9 +264,19 @@ function createWebpackConfig(
"web-toolbar": "web/toolbar.js",
};
if (bundleDefines.CHROME) {
libraryAlias["display-fetch_stream"] = "src/display/fetch_stream.js";
libraryAlias["display-network"] = "src/display/network.js";
viewerAlias["web-com"] = "web/chromecom.js";
viewerAlias["web-print_service"] = "web/pdf_print_service.js";
} else if (bundleDefines.GENERIC) {
libraryAlias["display-fetch_stream"] = "src/display/fetch_stream.js";
libraryAlias["display-l10n_utils"] = "web/l10n_utils.js";
libraryAlias["display-network"] = "src/display/network.js";
libraryAlias["display-node_stream"] = "src/display/node_stream.js";
libraryAlias["display-node_utils"] = "src/display/node_utils.js";
libraryAlias["display-svg"] = "src/display/svg.js";
viewerAlias["web-com"] = "web/genericcom.js";
viewerAlias["web-print_service"] = "web/pdf_print_service.js";
} else if (bundleDefines.MOZCENTRAL) {
@ -274,7 +292,7 @@ function createWebpackConfig(
}
viewerAlias["web-com"] = "web/firefoxcom.js";
}
const alias = { ...basicAlias, ...viewerAlias };
const alias = { ...basicAlias, ...libraryAlias, ...viewerAlias };
for (const key in alias) {
alias[key] = path.join(__dirname, alias[key]);
}
@ -1575,6 +1593,12 @@ function buildLibHelper(bundleDefines, inputStream, outputDir) {
defines: bundleDefines,
map: {
"pdfjs-lib": "../pdf",
"display-fetch_stream": "./fetch_stream",
"display-l10n_utils": "../web/l10n_utils",
"display-network": "./network",
"display-node_stream": "./node_stream",
"display-node_utils": "./node_utils",
"display-svg": "./svg",
},
};
const licenseHeaderLibre = fs

View File

@ -39,6 +39,7 @@ import {
} from "./display_utils.js";
import { AnnotationStorage } from "./annotation_storage.js";
import { ColorConverters } from "../shared/scripting_utils.js";
import { NullL10n } from "display-l10n_utils";
import { XfaLayer } from "./xfa_layer.js";
const DEFAULT_TAB_INDEX = 1000;
@ -2872,7 +2873,6 @@ class AnnotationLayer {
typeof PDFJSDev !== "undefined" &&
PDFJSDev.test("GENERIC && !TESTING")
) {
const { NullL10n } = require("pdfjs-web/l10n_utils.js");
this.l10n ||= NullL10n;
}
if (typeof PDFJSDev !== "undefined" && PDFJSDev.test("TESTING")) {

View File

@ -58,12 +58,22 @@ import {
StatTimer,
} from "./display_utils.js";
import { FontFaceObject, FontLoader } from "./font_loader.js";
import {
NodeCanvasFactory,
NodeCMapReaderFactory,
NodeFilterFactory,
NodeStandardFontDataFactory,
} from "display-node_utils";
import { CanvasGraphics } from "./canvas.js";
import { GlobalWorkerOptions } from "./worker_options.js";
import { MessageHandler } from "../shared/message_handler.js";
import { Metadata } from "./metadata.js";
import { OptionalContentConfig } from "./optional_content_config.js";
import { PDFDataTransportStream } from "./transport_stream.js";
import { PDFFetchStream } from "display-fetch_stream";
import { PDFNetworkStream } from "display-network";
import { PDFNodeStream } from "display-node_stream";
import { SVGGraphics } from "display-svg";
import { XfaText } from "./xfa_text.js";
const DEFAULT_RANGE_CHUNK_SIZE = 65536; // 2^16 = 65536
@ -72,54 +82,21 @@ const DELAYED_CLEANUP_TIMEOUT = 5000; // ms
const DefaultCanvasFactory =
typeof PDFJSDev !== "undefined" && PDFJSDev.test("GENERIC") && isNodeJS
? require("./node_utils.js").NodeCanvasFactory
? NodeCanvasFactory
: DOMCanvasFactory;
const DefaultCMapReaderFactory =
typeof PDFJSDev !== "undefined" && PDFJSDev.test("GENERIC") && isNodeJS
? require("./node_utils.js").NodeCMapReaderFactory
? NodeCMapReaderFactory
: DOMCMapReaderFactory;
const DefaultFilterFactory =
typeof PDFJSDev !== "undefined" && PDFJSDev.test("GENERIC") && isNodeJS
? require("./node_utils.js").NodeFilterFactory
? NodeFilterFactory
: DOMFilterFactory;
const DefaultStandardFontDataFactory =
typeof PDFJSDev !== "undefined" && PDFJSDev.test("GENERIC") && isNodeJS
? require("./node_utils.js").NodeStandardFontDataFactory
? NodeStandardFontDataFactory
: DOMStandardFontDataFactory;
let createPDFNetworkStream;
if (typeof PDFJSDev === "undefined") {
const streamsPromise = Promise.all([
import("./network.js"),
import("./fetch_stream.js"),
]);
createPDFNetworkStream = async params => {
const [{ PDFNetworkStream }, { PDFFetchStream }] = await streamsPromise;
return isValidFetchUrl(params.url)
? new PDFFetchStream(params)
: new PDFNetworkStream(params);
};
} else if (PDFJSDev.test("GENERIC || CHROME")) {
if (PDFJSDev.test("GENERIC") && isNodeJS) {
const { PDFNodeStream } = require("./node_stream.js");
createPDFNetworkStream = params => {
return new PDFNodeStream(params);
};
} else {
const { PDFNetworkStream } = require("./network.js");
const { PDFFetchStream } = require("./fetch_stream.js");
createPDFNetworkStream = params => {
return isValidFetchUrl(params.url)
? new PDFFetchStream(params)
: new PDFNetworkStream(params);
};
}
}
/**
* @typedef { Int8Array | Uint8Array | Uint8ClampedArray |
* Int16Array | Uint16Array |
@ -448,6 +425,19 @@ function getDocument(src) {
if (typeof PDFJSDev !== "undefined" && PDFJSDev.test("MOZCENTRAL")) {
throw new Error("Not implemented: createPDFNetworkStream");
}
const createPDFNetworkStream = params => {
if (
typeof PDFJSDev !== "undefined" &&
PDFJSDev.test("GENERIC") &&
isNodeJS
) {
return new PDFNodeStream(params);
}
return isValidFetchUrl(params.url)
? new PDFFetchStream(params)
: new PDFNetworkStream(params);
};
networkStream = createPDFNetworkStream({
url,
length,
@ -3454,5 +3444,6 @@ export {
PDFWorker,
PDFWorkerUtil,
RenderTask,
SVGGraphics,
version,
};

View File

@ -31,14 +31,10 @@ if (typeof PDFJSDev !== "undefined" && PDFJSDev.test("MOZCENTRAL")) {
);
}
const fs = __non_webpack_require__("fs");
const http = __non_webpack_require__("http");
const https = __non_webpack_require__("https");
const url = __non_webpack_require__("url");
const fileUriRegex = /^file:\/\/\/[a-zA-Z]:\//;
function parseUrl(sourceUrl) {
const url = __non_webpack_require__("url");
const parsedUrl = url.parse(sourceUrl);
if (parsedUrl.protocol === "file:" || parsedUrl.host) {
return parsedUrl;
@ -344,11 +340,13 @@ class PDFNodeStreamFullReader extends BaseFullReader {
this._request = null;
if (this._url.protocol === "http:") {
const http = __non_webpack_require__("http");
this._request = http.request(
createRequestOptions(this._url, stream.httpHeaders),
handleResponse
);
} else {
const https = __non_webpack_require__("https");
this._request = https.request(
createRequestOptions(this._url, stream.httpHeaders),
handleResponse
@ -391,11 +389,13 @@ class PDFNodeStreamRangeReader extends BaseRangeReader {
this._request = null;
if (this._url.protocol === "http:") {
const http = __non_webpack_require__("http");
this._request = http.request(
createRequestOptions(this._url, this._httpHeaders),
handleResponse
);
} else {
const https = __non_webpack_require__("https");
this._request = https.request(
createRequestOptions(this._url, this._httpHeaders),
handleResponse
@ -420,6 +420,7 @@ class PDFNodeStreamFsFullReader extends BaseFullReader {
path = path.replace(/^\//, "");
}
const fs = __non_webpack_require__("fs");
fs.lstat(path, (error, stat) => {
if (error) {
if (error.code === "ENOENT") {
@ -449,6 +450,7 @@ class PDFNodeStreamFsRangeReader extends BaseRangeReader {
path = path.replace(/^\//, "");
}
const fs = __non_webpack_require__("fs");
this._setReadableStream(fs.createReadStream(path, { start, end: end - 1 }));
}
}

36
src/display/stubs.js Normal file
View File

@ -0,0 +1,36 @@
/* Copyright 2023 Mozilla Foundation
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
const NodeCanvasFactory = null;
const NodeCMapReaderFactory = null;
const NodeFilterFactory = null;
const NodeStandardFontDataFactory = null;
const NullL10n = null;
const PDFFetchStream = null;
const PDFNetworkStream = null;
const PDFNodeStream = null;
const SVGGraphics = null;
export {
NodeCanvasFactory,
NodeCMapReaderFactory,
NodeFilterFactory,
NodeStandardFontDataFactory,
NullL10n,
PDFFetchStream,
PDFNetworkStream,
PDFNodeStream,
SVGGraphics,
};

View File

@ -50,6 +50,7 @@ import {
getDocument,
PDFDataRangeTransport,
PDFWorker,
SVGGraphics,
version,
} from "./display/api.js";
import {
@ -78,11 +79,6 @@ const pdfjsVersion =
const pdfjsBuild =
typeof PDFJSDev !== "undefined" ? PDFJSDev.eval("BUNDLE_BUILD") : void 0;
const SVGGraphics =
typeof PDFJSDev !== "undefined" && PDFJSDev.test("GENERIC")
? require("./display/svg.js").SVGGraphics
: null;
export {
AbortException,
AnnotationEditorLayer,

View File

@ -39,6 +39,7 @@ import {
getDocument,
PDFDataRangeTransport,
PDFWorker,
SVGGraphics,
version,
} from "../../src/display/api.js";
import {
@ -110,10 +111,7 @@ describe("pdfjs_api", function () {
renderTextLayer,
setLayerDimensions,
shadow,
SVGGraphics:
typeof PDFJSDev !== "undefined" && PDFJSDev.test("LIB")
? require("../../display/svg.js").SVGGraphics
: null,
SVGGraphics,
UnexpectedResponseException,
updateTextLayer,
Util,

View File

@ -17,6 +17,13 @@
"pdfjs-web/": "../../web/",
"pdfjs-test/": "../",
"display-fetch_stream": "../../src/display/fetch_stream.js",
"display-l10n_utils": "../../src/display/stubs.js",
"display-network": "../../src/display/network.js",
"display-node_stream": "../../src/display/stubs.js",
"display-node_utils": "../../src/display/stubs.js",
"display-svg": "../../src/display/stubs.js",
"web-annotation_editor_params": "../../web/annotation_editor_params.js",
"web-com": "../../web/genericcom.js",
"web-pdf_attachment_viewer": "../../web/pdf_attachment_viewer.js",

View File

@ -49,6 +49,13 @@ See https://github.com/adobe-type-tools/cmap-resources
"pdfjs-lib": "../src/pdf.js",
"pdfjs-web/": "./",
"display-fetch_stream": "../src/display/fetch_stream.js",
"display-l10n_utils": "../src/display/stubs.js",
"display-network": "../src/display/network.js",
"display-node_stream": "../src/display/stubs.js",
"display-node_utils": "../src/display/stubs.js",
"display-svg": "../src/display/stubs.js",
"web-annotation_editor_params": "./stubs-geckoview.js",
"web-com": "./genericcom.js",
"web-pdf_attachment_viewer": "./stubs-geckoview.js",

View File

@ -60,6 +60,13 @@ See https://github.com/adobe-type-tools/cmap-resources
"pdfjs-lib": "../src/pdf.js",
"pdfjs-web/": "./",
"display-fetch_stream": "../src/display/fetch_stream.js",
"display-l10n_utils": "../src/display/stubs.js",
"display-network": "../src/display/network.js",
"display-node_stream": "../src/display/stubs.js",
"display-node_utils": "../src/display/stubs.js",
"display-svg": "../src/display/stubs.js",
"web-annotation_editor_params": "./annotation_editor_params.js",
"web-com": "./genericcom.js",
"web-pdf_attachment_viewer": "./pdf_attachment_viewer.js",